0.1.0 • Published 3 years ago

path-crawler v0.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

path-crawler

An utility to crawl generic objects paths.

import {crawl} from 'path-crawler';
// const {crawl} = require('path-crawler');

crawl(
  ['test?.some.number[]'],
  ({id, field, hint, optional}, value) => {
    // id is the unique path index, 0 in this case
    // field is the last field of the path: "number"
    // hint can be empty string, ?, or []
    // optional is true if the field was optional
    // if non undefined value is returned,
    // the original field is overwritten with the new value.
    return value.map(x => x * x);
  },
  {
    test: {
      some: [
        // will become [1, 4, 9]
        {number: [1, 2, 3]},
        // will become [16, 25]
        {number: [4, 5]}
      ]
    }
  }
);