Skip to content

Commit

Permalink
feature(nessy) add ability to use custom divider
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Jul 14, 2017
1 parent b2fb4bc commit 6fd70c2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@ Set value in nested object.
```js
const nessy = require('nessy');

nessy('hello.world', 'why not?', {
nessy('hello.world', 'why not?', '.', {
hello: {
world: {
'could be used in browser as well'
}
}
});

// returns
{
hello: {
world: {
'why not?'
}
}
}

nessy('hello*world', 'why not?', '*', {
hello: {
world: {
'can be used any divider'
}
}
});

// returns
{
hello: {
Expand Down
9 changes: 6 additions & 3 deletions lib/nessy.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict';

module.exports = (selector, value, obj) => {
obj = obj || {};
module.exports = (selector, value, divider, obj) => {
if (!obj) {
obj = divider || {};
divider = '.';
}

const result = obj;

check(selector);

selector.split('.')
selector.split(divider)
.forEach((name, i, arr) => {
if (i === arr.length - 1)
obj[name] = value;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@
"tape": "^4.2.0"
},
"dependencies": {}
}
}
23 changes: 21 additions & 2 deletions test/nessy.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,33 @@ test('result: should set object\'s property', (t) => {
});

test('result: should set object\'s property, when there is parent node', (t) => {
const actual = nessy('hello.world', 'anyone', {
const actual = nessy('hello.world', 'anyone', {
hello: {
world: 'someone',
cloud: true
}
});

const expected = {
const expected = {
hello: {
world: 'anyone',
cloud: true
}
};

t.deepEqual(actual, expected, 'should not clear parent node');
t.end();
});

test('nessy: custom divider', (t) => {
const actual = nessy('hello*world', 'anyone', '*', {
hello: {
world: 'someone',
cloud: true
}
});

const expected = {
hello: {
world: 'anyone',
cloud: true
Expand Down

0 comments on commit 6fd70c2

Please sign in to comment.