All files / json-walk index.ts

100% Statements 21/21
100% Branches 6/6
100% Functions 2/2
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 271x 17x 17x 8x 6x   2x 2x 8x 2x     2x 2x 2x       2x   3x 2x          
export const walk = (value: unknown, callback: (value: unknown) => void): void => {
  callback(value);
  if (typeof value === 'object') {
    if (!value) return;
    switch (value.constructor) {
      case Array: {
        const arr = value as unknown[];
        const length = arr.length;
        for (let i = 0; i < length; i++) walk(arr[i], callback);
        break;
      }
      case Object: {
        const obj = value as Record<string, unknown>;
        for (const key in obj) walk(obj[key], callback);
        break;
      }
      case Map:
      case Set: {
        const mapOrSet = value as Set<unknown> | Map<unknown, unknown>;
        // biome-ignore lint: .forEach() is the fastest way to iterate over a Set or Map
        mapOrSet.forEach((val) => walk(val, callback));
        break;
      }
    }
  }
};