Skip to content

Commit

Permalink
feat: support string properties
Browse files Browse the repository at this point in the history
Such as `undefsafe(v, 'a["b.c"].d.e')`
  • Loading branch information
remy committed Feb 8, 2017
1 parent 4096e74 commit f6a7369
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/undefsafe.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
'use strict';

function undefsafe(obj, path, value) {
var parts = path.split('.');

// I'm not super keen on this private function, but it's because
// it'll also be use in the browser and I wont *one* function exposed
function split(path) {
var res = [];
var level = 0;
var key = '';

for (var i = 0; i < path.length; i++) {
var c = path.substr(i, 1);

if (level === 0 && (c === '.' || c === '[')) {
if (c === '[') {
level++;
i++;
c = path.substr(i, 1);
}
res.push(key);
key = '';
continue;
}

if (c === ']') {
level--;
key = key.slice(0, -1);
continue;
}

key += c;
}

res.push(key);

return res;
}

var parts = split(path);
var key = null;
var type = typeof obj;
var root = obj;
Expand Down
29 changes: 29 additions & 0 deletions test/undefsafe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ test('should handle null properties', function (t) {
t.end();
});

test('should find properties with periods in them', function (t) {
var value = {
a: { 'one.two': true }
};

var r = undefsafe(value, 'a["one.two"]');
t.equal(r, true, 'a["one.two"]: ' + r);

value = {
a: { 'one.two.and\three': true }
};

r = undefsafe(value, `a['one.two.and\three']`);
t.equal(r, true, 'weird: ' + r);

value = {
a: { 'one.two.and\three': [
false,
true,
] }
};

r = undefsafe(value, `a['one.two.and\three'].1`);
t.equal(r, true, 'combo: ' + r);

t.end();
});


test('should find deep object properties', function (t) {
var value = {
a: {
Expand Down

0 comments on commit f6a7369

Please sign in to comment.