object.assign
Advanced tools
Comparing version 0.2.1 to 0.3.1
15
index.js
@@ -5,7 +5,16 @@ "use strict"; | ||
var keys = require('object-keys'); | ||
var isObject = function (obj) { | ||
return obj && typeof obj === 'object'; | ||
}; | ||
var assignShim = function assign(target, source) { | ||
var props = keys(source); | ||
for (var i = 0; i < props.length; ++i) { | ||
target[props[i]] = source[props[i]]; | ||
var s, i, props; | ||
if (!isObject(target)) { throw new TypeError('target must be an object'); } | ||
for (s = 1; s < arguments.length; ++s) { | ||
source = arguments[s]; | ||
if (!isObject(source)) { throw new TypeError('source ' + s + ' must be an object'); } | ||
props = keys(Object(source)); | ||
for (i = 0; i < props.length; ++i) { | ||
target[props[i]] = source[props[i]]; | ||
} | ||
} | ||
@@ -12,0 +21,0 @@ return target; |
{ | ||
"name": "object.assign", | ||
"version": "0.2.1", | ||
"version": "0.3.1", | ||
"author": "Jordan Harband", | ||
@@ -32,3 +32,3 @@ "description": "ES6 spec-compliant Object.assign shim. From https://github.com/es-shims/es6-shim", | ||
"is": "~0.3.0", | ||
"tape": "~2.10.2", | ||
"tape": "~2.12.3", | ||
"covert": "~0.3.1" | ||
@@ -35,0 +35,0 @@ }, |
@@ -11,2 +11,6 @@ #object.assign <sup>[![Version Badge][2]][1]</sup> | ||
Takes a minimum of 2 object arguments: `target` and `source`. | ||
Takes a variable sized list of source arguments - at least 1, as many as you want. | ||
Throws a TypeError if any arguments are not objects. | ||
Most common usage: | ||
@@ -36,3 +40,3 @@ ```js | ||
[source1, source2, sourceN].reduce(Object.assign, target); | ||
assign(target, source1, source2, sourceN); | ||
assert.deepEqual(target, expected); // AWESOME! | ||
@@ -49,9 +53,11 @@ ``` | ||
}; | ||
var source = { | ||
var source1 = { | ||
c: false, | ||
d: false, | ||
d: false | ||
}; | ||
var sourceN = { | ||
e: false | ||
}; | ||
var assigned = assign(target, source); | ||
var assigned = assign(target, source1, sourceN); | ||
assert.equal(target, assigned); // returns the target object | ||
@@ -111,5 +117,2 @@ assert.deepEqual(assigned, { | ||
## Source | ||
Implementation taken directly from [es6-shim]([11]) and modified locally. | ||
## Tests | ||
@@ -116,0 +119,0 @@ Simply clone the repo, `npm install`, and run `npm test` |
var test = require('tape'); | ||
var assign = require('../index.js'); | ||
var keys = require('object-keys'); | ||
test('error cases', function (t) { | ||
var target = {}; | ||
t.throws(function () { assign(null); }, TypeError, 'target must be an object'); | ||
t.throws(function () { assign(target, null); }, TypeError, 'source 1 must be an object'); | ||
t.throws(function () { assign(target, { a: 1 }, undefined, { b: 2 }); }, TypeError, 'source 2 must be an object'); | ||
t.deepEqual(target, { a: 1 }, 'target is partially modified when errors are thrown'); | ||
t.end(); | ||
}); | ||
test('returns the modified target object', function (t) { | ||
@@ -11,2 +21,7 @@ var target = {}; | ||
test('has the right length', function (t) { | ||
t.equal(assign.length, 2, 'length is 2 => 2 required arguments'); | ||
t.end(); | ||
}); | ||
test('merge two objects', function (t) { | ||
@@ -19,2 +34,11 @@ var target = { a: 1 }; | ||
test('merge N objects', function (t) { | ||
var target = { a: 1 }; | ||
var source1 = { b: 2 }; | ||
var source2 = { c: 3 }; | ||
var returned = assign(target, source1, source2); | ||
t.deepEqual(returned, { a: 1, b: 2, c: 3 }, 'returned object has properties from all sources'); | ||
t.end(); | ||
}); | ||
test('only iterates over own keys', function (t) { | ||
@@ -21,0 +45,0 @@ var Foo = function () {}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
9973
129
130