keypather
Get or set a object values from a keypath string. Supports bracket notation, dot notation, and functions.
Ignores errors for deep-keypaths by default.
Safely handles string expressions - No eval
or new Function
code here!
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");
keypath.get(obj, "['foo']['bar']['baz']");
var keypath = require('keypather')();
var obj = {
foo: function () {
return function () {
return function () {
return 'val';
};
};
}
};
keypath.get(obj, "foo()()()");
var keypath = require('keypather')();
var obj = {
foo: function () {
return {
bar: {
baz: 'val'
}
};
}
};
keypath.get(obj, "foo()['bar'].baz");
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']);
keypath.get(obj, "create(%).get(%)", {foo:1, bar:2}, 'foo');
Get returns null for keypaths that do not exist by default,
but can also throw errors with { force: false }
var keypath = require('keypather')();
var obj = {};
keypath.get(obj, "foo.bar.baz");
var keypath = require('keypather')({ force: false });
var obj = {};
keypath.get(obj, "foo.bar.baz");
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');
keypath.set(obj, "foo.bar.baz", 'value');
keypath.set(obj, "['foo']['bar']['baz']", 'value');
Set forces creation by default:
var keypath = require('keypather')();
var obj = {};
keypath.set(obj, "foo.bar.baz", 'val');
var keypath = require('keypather')({ force: false });
var obj = {};
keypath.set(obj, "foo.bar.baz", 'val');
IN
Equivalent to key in obj
var keypath = require('keypather')();
var obj = {
foo: {
bar: {
baz: 'val'
}
}
}
};
keypath.in(obj, "foo['bar'].baz");
keypath.in(obj, "foo.bar.baz");
keypath.in(obj, "['foo']['bar']['baz']");
HAS
Equivalent to obj.hasOwnProperty
var keypath = require('keypather')();
var obj = {
foo: {
bar: {
baz: 'val'
}
}
}
};
keypath.has(obj, "foo['bar'].baz");
keypath.has(obj, "foo.bar.baz");
keypath.has(obj, "['foo']['bar']['baz']");
DEL
Equivalent to delete obj.key
var keypath = require('keypather')();
var obj = {
foo: {
bar: {
baz: 'val'
}
}
}
};
keypath.del(obj, "foo['bar'].baz");
keypath.del(obj, "foo.bar.baz");
keypath.del(obj, "['foo']['bar']['baz']");
FLATTEN
Flatten an object or array into a keypath object
var keypath = require('keypather')();
keypath.flatten({
foo: {
qux: 'hello'
},
bar: [
1,
{
yolo: [1]
}
]
});
keypath.flatten({
foo: {
qux: 'hello'
}
}, '__');
EXPAND
Expand a flattened object back into an object or array
var keypath = require('keypather')();
keypath.expand({
'foo.qux': 'hello',
'bar[0]': 1,
'bar[1].yolo[0]': 1
});
keypath.expand({
'[0]': 1,
'[1].yolo[0]': 1
});
keypath.flatten({
foo: {
qux: 'hello'
}
}, '__');
License
MIT