1.0.2 • Published 3 years ago

deep-walk v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

dep min minZip

traverse objects by every node on a recursive.

usage

npm i deep-walk -S

apis

each

execute fn for each node in the object.

import { walk } from "deep-walk";

walk({ a: { b: "" }, c: ["A", "B"] }).each((ctx) => {...});

reduce

each node in the object, perform a left-fold with the return value of function.

import { walk } from "deep-walk";

const result = walk({ a: { b: "" }, c: ["A", "B"] }).reduce<string[]>(
  (ctx, acc) => {
    if(ctx.isLeaf){
      acc.push(ctx.value);
    }
  },
  []
);

console.log(result); // ["", "A", "B"]

ctx(context)

The value that comes in the callback argument.

{
  isRoot?: boolean;
  isNotRoot?: boolean;
  isLeaf?: boolean;
  isNotLeaf?: boolean;
  value: any; // current value
};