Naked Panda Meditations

    object-traverse

    1.1.1 • Public • Published

    object-traverse

    ============== Build Status view on npm

    A very simple utility on traversing object graphs using a string.

    Changelog

    v1.1.1

    • added create and push

    v1.1.0

    • used "." instead of slash

    v1.0.2

    • added has which check if a path is available by checking if its undefined

    How to use

       npm install --save-dev object-traverse
       
    

    If using via normal javascript include, then it is exported as window.objectTraverse.

    API

    var traverse = require('object-traverse');
     
    var graph = {  
       field : {
          deep : {
             x : 1,
             y: 2
          }
       },
       field2: [],
       deepFunc : { someFunc: function(arg) {} }
       
    };
    #get a value
    traverse(graph).get('field/deep');
    #get with a default
    traverse(graph).get('field/deep','default return val if not found'); 
     
    #check if something is defined
    traverse(graph).has('field/deep/missing');
     
    #sets a value
    traverse(graph).set('field/deep/z',5);
       
    #helpers
    traverse(graph).isArray('field2');
    traverse(graph).isString('field2');
    traverse(graph).isObject('field2');
    traverse(graph).isNumber('field2');
              
    #exec
    traverse(graph).exec('deepFunc/someFunc','arg');         
     
    #delete
    traverse(graph).delete('field/deep/x');
     
    #push - add vall to the array, will create if it doesn't exists
    traverse(graph).push('a.b.c',val);
     
    #create create the given path if it doesn't exists, will skip none object types
    traverse(graph).create('a.b.c');
    traverse(graph).get('a.b.c'); //{ a : { b : { c: {} } }

    Install

    npm i object-traverse

    DownloadsWeekly Downloads

    26

    Version

    1.1.1

    License

    MIT

    Last publish

    Collaborators

    • wmira