Coder Social home page Coder Social logo

eliancordoba / traversal-map Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 391 KB

Iterate through all elements in a object or array calling a callback function.

License: BSD 3-Clause "New" or "Revised" License

JavaScript 0.68% TypeScript 99.32%
map for-each deep iterate recursive key tree colection flow-control

traversal-map's Introduction

Build Status Coverage Status CodeFactor Known Vulnerabilities install size

Traversal map

Iterate through all elements in a object or array calling a callback function.

Features

  • Blazing fast
  • Types out of the box
  • No dependencies
  • Tiny

Installation Using npm

npm install traversal-map

Example Usage

import traversalMap from 'traversal-map';

const data = {
  a: 1,
  b: 2,
  c:  {
    d: 3,
    e: [ 'a', 'b', 'c' ]
  }
};

traversalMap(data, (value: any, keyOrIndex: string | number, path: string) => {
  console.log(`${value} / ${keyOrIndex} / ${path}`);
});

/*
  1 / a / a
  2 / b / b
  [object Object] / c / c
  3 / d / c.d
  a.b.c / e / c.e
  a / 0 / c.e[0]
  b / 1 / c.e[1]
  c / 2 / c.e[2]
*/

Api

  traversalMap(target, callbackFn, options?);
Parameter Type Description
target Object or Array or ArrayLike* The object which will be traversed
callbackFn (value: any, keyOrIndex?: string or number, path?: string) => any - value: Value of the current position. - keyOrIndex optional: Identifier of the current position, key for object and index for arrays. - path optional: Path to the current position.
options? useDotNotationOnKeys: boolean Default true. When set to false insted of this: a.b.c You will get ['a']['b']['c']

? = Optional paramter.

* = ArrayLike is an object whitch is neither null nor a function (Definition of function in ./util/typeCheckers/isFunction) and it has a lenght property of type number.

Flow Control

You can modify the flow of the iteration returning special numbers:

10: Break the iteration at the current depth skyping remaining sibling elements.

11: Break the iteration.

20: Skip child elements of the current element.

Testing

npm run test            // Run tests once
npm run test-with-cover // Run tests once and get coverage information
npm run test-watch      // Tests in watch mode 

Debugging:

If you want to debug then code, putting you have to launch Debug Jest Tests from vscode. You can uncoment the debug test in ./src/index.test.ts if you don't know where to start. If the breakpoints aren't hit try putting a debugger on the test first and once it gets hit you can start debuggin the code.

About

I needed to iterate over a deeply nested object and gather the path to each property, so I looked around and found for-each-safe. I liked the idea so I clonned, migrated it to Typescript, added tests and made it faster and smaller (mine is less safe than the original as it checks for less edgy cases, but for my purpose and the majority of people out there, is more than enough).

traversal-map's People

Contributors

dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar eliancordoba avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

santiq

traversal-map's Issues

Missing dependencies

es-abstract-has-property es-abstract-is-callable es-abstract-to-length lodash.isplainobject lodash.merge are not listed in package.json / dependencies.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.