From 02bc0720db81ca4c864331c8647675defbfbbc5d Mon Sep 17 00:00:00 2001 From: jusx Date: Sun, 3 Mar 2019 00:14:12 -0800 Subject: [PATCH] feat(set): Support for arguments as parameters instead of object. --- __tests__/object-dot.js | 6 ++++++ lib/object-dot.js | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/__tests__/object-dot.js b/__tests__/object-dot.js index 928d597..64c32a7 100644 --- a/__tests__/object-dot.js +++ b/__tests__/object-dot.js @@ -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', () => { diff --git a/lib/object-dot.js b/lib/object-dot.js index b04e738..752bd4c 100644 --- a/lib/object-dot.js +++ b/lib/object-dot.js @@ -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 @@ -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 }