
keypather

Get, set, or delete a deep value using a keypath string.
A collection of keypath utilities: get, set, delete, in, has, flatten, expand, and immutable set/delete.
Lightweight and parses keypaths using vanilla JS - No eval
or new Function
hacks!
Installation
npm install keypather
Usage
Examples
Import
const get = require('keypather/get')
const set = require('keypather/set')
const del = require('keypather/del')
const immutableSet = require('keypather/immutable-set')
const immutableDel = require('keypather/immutable-del')
const keypathIn = require('keypather/in')
const hasKeypath = require('keypather/has')
const expand = require('keypather/expand')
const flatten = require('keypather/flatten')
GET, SET, DEL Example
const get = require('keypather/get')
const set = require('keypather/set')
const del = require('keypather/del')
let obj
obj = { foo: { bar: 100 } }
get(obj, 'foo.bar')
del(obj, '["foo"]["bar"]')
set(obj, 'foo.bar.qux', 200)
get(obj, 'foo["bar"].qux')
obj = {}
set(obj, 'foo[0]', 100)
Immutable SET, DEL Example
const set = require('keypather/immutable-set')
const del = require('keypather/immutable-del')
let obj
let out
obj = { foo: { bar: 100 } }
out = set(obj, 'foo.bar', 100)
out = set(obj, 'foo.bar.qux', 200)
out = del(obj, 'one.two.three')
out = del(obj, 'foo.bar.qux')
obj = {}
out = set(obj, 'foo[0]', 100)
HAS, IN Example
const hasKeypath = require('keypather/has')
const keypathIn = require('keypather/in')
const obj = { foo: Object.create({ bar: 100 }) }
hasKeypath(obj, 'foo.bar')
keypathIn(obj, 'foo.bar')
hasKeypath(obj, 'foo')
FLATTEN, EXPAND Example
const expand = require('keypather/expand')
const flatten = require('keypather/flatten')
const obj = expand({
'foo.bar': 1,
'foo.qux[0]': 100,
'foo["qux"][1]': 200,
'foo.qux.wut': 'val'
})
const flat = flatten(obj)
Errors Example
get({}, 'foo.bar', { force: false })
set({}, 'foo.bar', 100, { force: false })
del({}, 'foo.bar', { force: false })
immutableSet({}, 'foo.bar', 100, { force: false })
immutableDel({}, 'foo.bar', { force: false })
get({ foo: {} }, 'foo.bar', { force: false })
set({ foo: {} }, 'foo.bar', 100, { force: false })
del({ foo: {} }, 'foo.bar', { force: false })
immutableSet({ foo: {} }, 'foo.bar', 100, { force: false })
immutableDel({ foo: {} }, 'foo.bar', { force: false })
hasKeypath({}, 'foo.bar', { force: false })
keypathIn({}, 'foo.bar', { force: false })
keypathIn({}, 'foo.bar.qux', { force: false })
hasKeypath({}, 'foo.bar.qux', { force: false })
set({}, '[0]', 'val', { overwritePrimitives: false })
set([], 'key', 'val', { overwritePrimitives: false })
set({ foo: 1 }, 'foo.qux', 'val', { overwritePrimitives: false })
set({ foo: 1 }, 'foo[0]', 'val', { overwritePrimitives: false })
set({ foo: 'str' }, 'foo.bar', 'val', { overwritePrimitives: false })
set({ foo: {} }, 'foo[0]', 'val', { overwritePrimitives: false })
get({}, 'foo.1bar')
get({}, 'foo[]')
get({}, 'foo["]')
get({}, 'foo.')
get({}, 'foo[')
get({}, "foo['")
Documentation
GET
Returns value at keypath in obj
const get = require('keypather/get');
const obj = {
foo: {
bar: {
baz: 'val'
}
}
};
get(obj, "foo.bar.baz");
get(obj, "foo['bar'].baz");
get(obj, "['foo']['bar']['baz']");
get({}, 'foo.two.three', { force: false })
SET
Sets a value in obj at keypath. If force=true, set will create objects at non-existant keys in the
keypath. If the non-existant key is a number, its value will be initialized as an array.
- @param {any} obj - context to read keypath from
- @param {string} keypath - bracket and/or dot notation keypath string to read from obj
- @returns {any} value - value to set at keypath
- @param {?object} opts - optional, defaults to { force: true, overwritePrimitives: true, warn: true }
- opts.force - whether non-existant keys in keypath should be created, defaults to true.
-
if false, `set` will error when reading a key on a non-existant keypath.
- opts.overwritePrimitives - whether primitive keys (booleans, strings, numbers) should be overwritten.
-
setting a key on a primitive will convert it to an object or array (if key is string or number).
-
if false, `set` will log a warning when setting keys on primitives.
- opts.silent - specifies whether warning logs should be enabled, defaults to false.
- @returns {any} value set at keypath
const set = require('keypather/set');
let obj = {
foo: {
bar: {
baz: 'val'
}
}
};
set(obj, "foo['bar'].baz", 'val');
set(obj, "foo.bar.baz", 'val');
set(obj, "['foo']['bar']['baz']", 'val');
obj = {}
set(obj, "foo.bar.baz", 'val');
obj = { foo: 1 }
set(obj, "foo.bar.baz", 'val');
obj = { foo: 1 }
set(obj, "foo[0].baz", 'val');
set({}, "foo.bar.baz", 'val', { force: false });
set({ foo: 'str' }, 'foo.bar', 'val', { overwritePrimitives: false })
DEL
Deletes value a keypath in obj. Similar to delete obj.key
.
const del = require('keypather/del');
const obj = {
foo: {
bar: {
baz: 'val'
}
}
};
del(obj, "foo['bar'].baz");
del(obj, "foo.bar.baz");
del(obj, "['foo']['bar']['baz']");
del(obj, "one.two.three", 'val', { force: false });
IMMUTABLE SET
Sets a value in obj at keypath. If force=true, set will create objects at non-existant keys in the
keypath. If the non-existant key is a number, its value will be initialized as an array.
- @param {any} obj - context to read keypath from
- @param {string} keypath - bracket and/or dot notation keypath string to read from obj
- @returns {any} value - value to set at keypath
- @param {?object} opts - optional, defaults to { force: true, overwritePrimitives: true, warn: true }
- opts.force - whether non-existant keys in keypath should be created, defaults to true.
-
if false, `immutable-set` will error when reading a key on a non-existant keypath.
- opts.overwritePrimitives - whether primitive keys (booleans, strings, numbers) should be overwritten.
-
setting a key on a primitive will convert it to an object or array (if key is string or number).
-
if false, `immutable-set` will log a warning when setting keys on primitives.
- opts.silent - specifies whether warning logs should be enabled, defaults to false.
- opts.shallowClone - provide custom shallowClone, defaults to shallow-clone
- @returns {any} returns same obj if unmodified, otherwise modified clone of obj
const set = require('keypather/immutable-set');
let obj = {
foo: {
bar: {
baz: 'val'
}
}
};
let out
out = set(obj, "foo['bar'].baz", 'val');
out = set(obj, "foo.bar.baz", 'val2');
out = set(obj, "['foo']['bar']['baz']", 'val3');
obj = { foo: 1 }
out = set(obj, "foo.bar.baz", 'val');
obj = { foo: 1 }
out = set(obj, "foo[0].baz", 'val');
obj = {}
set(obj, "foo.bar.baz", 'val', { force: false });
obj = { foo: 'str' }
out = set(obj, 'foo.bar', 'val', { overwritePrimitives: false })
IMMUTABLE DEL
Deletes value a keypath in obj. Similar to delete obj.key
.
const del = require('keypather/immutable-del');
const obj = {
foo: {
bar: {
baz: 'val'
}
}
};
let out
out = del(obj, "foo['bar'].baz");
out = del(obj, "foo.bar.baz");
out = del(obj, "['foo']['bar']['baz']");
del(obj, "one.two.three", 'val', { force: false });
IN
Returns true if keypath is "in" the obj at the keypath. Similar to "in" operator.
- @param {any} obj - context to read keypath in
- @param {string} keypath - bracket and/or dot notation keypath string to read from obj
- @param {?object} opts - optional, defaults to { force: true }
- opts.force - force specifies whether non-existant keypaths should be ignored, defaults to true
- @returns {boolean} true if the keypath is "in" the obj, else false
const keypathIn = require('keypather/in');
const obj = {
foo: {
bar: {
baz: 'val'
__proto__: {
qux: 'val'
}
}
}
};
keypathIn(obj, "foo.bar.baz");
keypathIn(obj, "foo.bar.qux");
keypathIn(obj, "foo.bar.bing");
keypathIn(obj, "foo['bar'].baz");
keypathIn(obj, "one.two.three");
keypathIn(obj, "one.two.three", { force: false });
keypathIn(obj, "foo.two.three", { force: false });
HAS
Returns true if the obj has the keypath. Similar to obj.hasOwnProperty
.
- @param {any} obj - context to read keypath in
- @param {string} keypath - bracket and/or dot notation keypath string to read from obj
- @param {?object} opts - optional, defaults to { force: true }
- opts.force - force specifies whether non-existant keypaths should be ignored, defaults to true
- @returns {boolean} true if the keypath is "in" the obj, else false
const hasKeypath = require('keypather/has');
const obj = {
foo: {
bar: {
baz: 'val'
__proto__: {
qux: 'val'
}
}
}
};
hasKeypath(obj, "foo.bar.baz");
hasKeypath(obj, "foo.bar.qux");
hasKeypath(obj, "['foo']['bar']['baz']");
hasKeypath(obj, "one.two.three");
hasKeypath(obj, "one.two.three", { force: false });
hasKeypath(obj, "foo.two.three", { force: false });
FLATTEN
Flatten an object or array into a keypath object
- @param {any} obj - object or array to flatten
const flatten = require('keypather/flatten');
flatten({
foo: {
qux: 'hello'
},
bar: [
1,
{
yolo: [1]
}
]
});
flatten({
foo: {
qux: 'hello'
}
}, '_');
EXPAND
Expand a flattened object back into an object or array
- @param {any} obj - flattened object or array to be expanded
const expand = require('keypather/expand');
expand({
'foo.qux': 'hello',
'bar[0]': 1,
'bar[1].yolo[0]': 1
});
expand({
'[0]': 1,
'[1].yolo[0]': 1
});
expand({
'foo_qux': 'hello'
}, '_');
Changelog
Changelog history
License
MIT