Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Square bracket syntax support #29

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# `dlv(obj, keypath)` [![NPM](https://img.shields.io/npm/v/dlv.svg)](https://npmjs.com/package/dlv) [![Build](https://travis-ci.org/developit/dlv.svg?branch=master)](https://travis-ci.org/developit/dlv)

> Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist or the value is undefined
> Safely get a path-specified value within a nested object, with ability to return a default if the full key path does not exist or the value is undefined


### Why?

Smallest possible implementation: only **130 bytes.**
Smallest possible implementation: only **160 bytes.**

You could write this yourself, but then you'd have to write [tests].

Expand All @@ -29,7 +29,11 @@ let obj = {
b: {
c: 1,
d: undefined,
e: null
e: null,
x: [
{ y: 8 },
{ z: 9 }
]
}
}
};
Expand All @@ -42,11 +46,15 @@ delve(obj, ['a', 'b', 'c']) === 1;

delve(obj, 'a.b') === obj.a.b;

//or access nested objects within an array
delve(obj, 'a.b.x[1].z') === 9;

//returns undefined if the full key path does not exist and no default is specified
delve(obj, 'a.b.f') === undefined;

//optional third parameter for default if the full key in path is missing
delve(obj, 'a.b.f', 'foo') === 'foo';
delve(obj, 'a.b.x[2].f', 'foo') === 'foo';

//or if the key exists but the value is undefined
delve(obj, 'a.b.d', 'foo') === 'foo';
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function dlv(obj, key, def, p) {
p = 0;
key = key.split ? key.split('.') : key;
key = key.split ? key.replace(/\[("|')?([^\[\]]+)\1\]/g, '.$2').split('.') : key;
while (obj && p<key.length) obj = obj[key[p++]];
return (obj===undefined || p<key.length) ? def : obj;
}
15 changes: 13 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ var obj = {
four: 4
}
}
}
},
foo: {
"some property": 8
},
arr: [4, 3, [9, { bar: false, deep: [false, true] }, 7]]
};

// assert equality of a given path, as dot notation and array.
Expand All @@ -27,7 +31,7 @@ function check(path, value, def) {
console.log(' ✓ delve(obj, "'+path+'"'+ (def ? ', "'+def+'"' : '') + ')');

if (path) {
var arr = path.split('.');
var arr = path.replace(/\[("|')?([^\[\]]+)\1\]/g, '.$2').split(".");
assert.strictEqual(delve(obj, arr, def), value);
console.log(' ✓ delve(obj, ' + JSON.stringify(arr) + (def ? ', "'+def+'"' : '') + ')');
console.log(' ✓ delve(obj, '+JSON.stringify(arr)+')');
Expand All @@ -48,6 +52,13 @@ check('n', obj.n);
check('n.badkey', undefined);
check('f', false);
check('f.badkey', undefined);
check('foo.some property', obj.foo["some property"]);
check('foo["some property"]', obj.foo["some property"]);
check('foo[\'some property\']', obj.foo["some property"]);
check('foo[some property]', obj.foo["some property"]);
check('arr[1]', obj.arr[1]);
check('arr[2][1].bar', obj.arr[2][1].bar);
check('arr[2][1].deep[1]', obj.arr[2][1].deep[1]);

//test defaults
console.log("\n> With Defaults");
Expand Down