diff --git a/lib/utils.js b/lib/utils.js index 89199d2e..75ca6f8b 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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, ' ')); diff --git a/test/utils.js b/test/utils.js index 0721dd8e..eff4011a 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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(); +});