All files / json-ml walk.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 3/3
100% Lines 10/10

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 161x     1x 3x 3x 12x 12x 9x 6x 4x       3x  
import {iter, type UndefIterator} from '../util/iterator';
import type {JsonMlNode} from './types';
 
export const walk0 = (node: JsonMlNode): UndefIterator<JsonMlNode> => {
  const stack: JsonMlNode[] = [node];
  return () => {
    const node = stack.pop();
    if (!node) return;
    if (typeof node === 'string') return node;
    for (let i = node.length - 1; i >= 2; i--) stack.push(node[i] as JsonMlNode);
    return node;
  };
};
 
export const walk = (node: JsonMlNode) => iter(walk0(node));