diff options
Diffstat (limited to 'date-fns/src/_lib/assign')
-rw-r--r-- | date-fns/src/_lib/assign/index.js | 17 | ||||
-rw-r--r-- | date-fns/src/_lib/assign/test.js | 30 |
2 files changed, 47 insertions, 0 deletions
diff --git a/date-fns/src/_lib/assign/index.js b/date-fns/src/_lib/assign/index.js new file mode 100644 index 0000000..6fd8782 --- /dev/null +++ b/date-fns/src/_lib/assign/index.js @@ -0,0 +1,17 @@ +export default function assign(target, dirtyObject) { + if (target == null) { + throw new TypeError( + 'assign requires that input parameter not be null or undefined' + ) + } + + dirtyObject = dirtyObject || {} + + for (var property in dirtyObject) { + if (dirtyObject.hasOwnProperty(property)) { + target[property] = dirtyObject[property] + } + } + + return target +} diff --git a/date-fns/src/_lib/assign/test.js b/date-fns/src/_lib/assign/test.js new file mode 100644 index 0000000..629358f --- /dev/null +++ b/date-fns/src/_lib/assign/test.js @@ -0,0 +1,30 @@ +// @flow +/* eslint-env mocha */ + +import assert from 'power-assert' +import assign from '.' + +describe('assign', function() { + it('assigns properties of the second argument to the first argument', function() { + var object = {} + assign(object, { a: 1, b: 2, c: 3 }) + assert.deepEqual(object, { a: 1, b: 2, c: 3 }) + }) + + it('the object passed as 2nd argument remains unchanged when the result is mutated', function() { + var object = { a: 1, b: 2, c: 3 } + var result = assign({}, object) + result.c = 4 + assert.deepEqual(object, { a: 1, b: 2, c: 3 }) + }) + + it('returns the first argument when the second argument is `undefined`', function() { + var original = { a: 1, b: 2, c: 3 } + var result = assign(original, undefined) + assert(original === result) + }) + + it('throws TypeError exception if the first argument is `undefined', function() { + assert.throws(assign.bind(null, undefined, { a: 1, b: 2, c: 3 }), TypeError) + }) +}) |