Skip to content

Commit

Permalink
feat(set): Support for arguments as parameters instead of object.
Browse files Browse the repository at this point in the history
  • Loading branch information
jusx committed Mar 3, 2019
1 parent a456614 commit 02bc072
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions __tests__/object-dot.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('set', () => {
odjectd.set({ object, path: 'a.b', value: 'foo' })
expect(object).toEqual({ a: { exist: true, b: 'foo' } })
})

test('with arguments instead of destructing object', () => {
let object = { a: { exist: true } }
odjectd.set(object, 'a.b', 'foo')
expect(object.a.b).toBe('foo')
})
})

describe('get', () => {
Expand Down
5 changes: 3 additions & 2 deletions lib/object-dot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

function set ({ object, path, value, separator = '.' }) {
// support arguments as parameter instead of object.
if (arguments.length > 1) [ object, path, value, separator = '.' ] = arguments

let properties = (Array.isArray(path)) ? path : path.split(separator)
if (properties.length === 1) {
object[path] = value
Expand All @@ -13,11 +16,9 @@ function set ({ object, path, value, separator = '.' }) {

function get ({ object, path, value }) {
let properties = path.split('.')

while (properties.length && object) {
object = object[properties.shift()]
}

return (value === undefined) ? object : value
}

Expand Down

0 comments on commit 02bc072

Please sign in to comment.