Skip to content

Commit

Permalink
[New] add utils.assign
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 11, 2017
1 parent 64d620d commit 5ebf8b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/utils.js
Expand Up @@ -77,6 +77,13 @@ exports.merge = function (target, source, options) {
}, mergeTarget);
};

exports.assign = function assignSingleSource(target, source) {
return Object.keys(source).reduce(function (acc, key) {
acc[key] = source[key];
return acc;
}, target);
};

exports.decode = function (str) {
try {
return decodeURIComponent(str.replace(/\+/g, ' '));
Expand Down
12 changes: 12 additions & 0 deletions test/utils.js
Expand Up @@ -20,3 +20,15 @@ test('merge()', function (t) {

t.end();
});

test('assign()', function (t) {
var target = { a: 1, b: 2 };
var source = { b: 3, c: 4 };
var result = utils.assign(target, source);

t.equal(result, target, 'returns the target');
t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');

t.end();
});

0 comments on commit 5ebf8b4

Please sign in to comment.