Skip to content

Commit

Permalink
update test & document & code base
Browse files Browse the repository at this point in the history
  • Loading branch information
0xvoidmain committed Dec 2, 2016
1 parent 2ea67c5 commit ea6102a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ console.log(get(testObject, '["test-abc"].a.b[0][2]'), 5);
set(testObject, 'a.b.c[0].ae', 10);
console.log(get(testObject, 'a.b.c[0].ae'), 10);

set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);
console.log(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);

```

## API
Expand All @@ -67,7 +70,7 @@ The function will return an VALUE or undefined

Where `path` is a string like `foo.bar` or `foo.bar[0][1]` or `[0][1].foo['bar']`.

If you want non-existent paths to be initialize
The function return TRUE or FALSE. Return TRUE if it can set value with the path

### DeepObject.parse(path)
Where `path` is a string like `foo.bar` or `foo.bar[0][1]` or `[0][1].foo['bar']`.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ function set(obj, path, value) {
for (var i = 0; i < keys.length - 1; i++) {
var key = keys[i];
if (!hasOwnProp.call(obj, key)) {
obj[key] = {}
return false;
};
obj = obj[key];
}
obj[keys[i]] = value;
return true;
} catch (ex) {

return false;
}
}

Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ set(testObject, 'a.b.c[0].ae', 10);
assert.equal(get(testObject, 'a.b.c[0].ae'), 10);
console.log('>> Ok\n');

console.log('Test:', "set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);\nassert.equal(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);");
set(testObject, 'a.b.c[0].ae.d.e.f.g.h', 10);
assert.equal(get(testObject, 'a.b.c[0].ae.d.e.f.g.h'), undefined);
console.log('>> Ok\n');

console.log('Good job');

0 comments on commit ea6102a

Please sign in to comment.