Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

keypather

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

keypather

Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21K
decreased by-21.6%
Maintainers
1
Weekly downloads
 
Created
Source

keypather Build Status

Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions

installation

npm install keypather

Examples

GET

dot notation, bracket notation, and functions (even with arguments) all supported:

var keypath = require('keypather')();
var obj = {
  foo: {
    bar: {
      baz: 'val'
    }
  }
};
keypath.get(obj, "foo.bar.baz"); // val
keypath.get(obj, "['foo']['bar']['baz']"); // val
var keypath = require('keypather')();
var obj = {
  foo: function () {
    return function () {
      return function () {
        return 'val';
      };
    };
  }
};
keypath.get(obj, "foo()()()"); // val
var keypath = require('keypather')();
var obj = {
  foo: function () {
    return {
      bar: {
        baz: 'val'
      }
    };
  }
};
keypath.get(obj, "foo()['bar'].baz"); // val

functions with arguments

var keypath = require('keypather')();
var obj = {
  create: function (data) {
    var data = data;
    return {
      get: function (key) {
        return data[key];
      }
    };
  }
};
keypath.get(obj, "create(%).get(%)", [{foo:1, bar:2}], ['foo']); // 1
keypath.get(obj, "create(%).get(%)", {foo:1, bar:2}, 'foo'); // 1, single args are automatically placed in arrays
// technically you can use anything (except dots, parens, brackets, or empty string)
// between the parens of functions that accept args (in place of %)

Get returns null for keypaths that do not exist by default, but can also throw errors with { force: false }

var keypath = require('keypather')(); // equivalent to { force:true }
var obj = {};
keypath.get(obj, "foo.bar.baz"); // null

var keypath = require('keypather')( force: false );
var obj = {};
keypath.get(obj, "foo.bar.baz");
// throw's an error
// Error: Cannot get 'foo' of undefined

SET

mixed notation, dot notation, and bracket notation all supported:

var keypath = require('keypather')();
var obj = {
  foo: {
      bar: {
        baz: 'val'
      }
    }
  }
};
keypath.set(obj, "foo['bar'].baz", 'value'); // value
keypath.set(obj, "foo.bar.baz", 'value'); // value
keypath.set(obj, "['foo']['bar']['baz']", 'value'); // value

Set forces creation by default:

var keypath = require('keypather')(); // equivalent to { force:true }
var obj = {};
keypath.set(obj, "foo.bar.baz", 'val'); // value
// obj = {
//   foo: {
//     bar: {
//       baz: 'val'
//     }
//   }
// };

var keypath = require('keypather')( force: false );
var obj = {};
keypath.set(obj, "foo.bar.baz", 'val');
// throw's an error
// Error: Cannot get 'foo' of undefined

DEL

mixed notation, dot notation, and bracket notation all supported:

var keypath = require('keypather')();
var obj = {
  foo: {
      bar: {
        baz: 'val'
      }
    }
  }
};
keypath.del(obj, "foo['bar'].baz");        // true
keypath.del(obj, "foo.bar.baz");           // true
keypath.del(obj, "['foo']['bar']['baz']"); // true
// obj:
// {
//   foo: {
//     bar: {}
//   }
// }

License

MIT

Keywords

FAQs

Package last updated on 17 Mar 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc