object.assign
Advanced tools
Comparing version 4.0.2 to 4.0.3
@@ -0,1 +1,11 @@ | ||
4.0.3 / 2015-10-21 | ||
================== | ||
* [Fix] Support core-js's Symbol sham (#17) | ||
* [Fix] Ensure that properties removed or made non-enumerable during enumeration are not assigned (#16) | ||
* [Fix] Avoid looking up keys and values more than once | ||
* [Tests] Avoid using `reduce` so `npm run test:shams:corejs` passes in `node` `v0.8` ([core-js#122](https://github.com/zloirock/core-js/issues/122)) | ||
* [Tests] Refactor to use my conventional structure that separates shimmed, implementation, and common tests | ||
* [Tests] Create `npm run test:shams` and better organize tests for symbol shams | ||
* [Tests] Remove `nsp` in favor of `requiresafe` | ||
4.0.2 / 2015-10-20 | ||
@@ -2,0 +12,0 @@ ================== |
@@ -25,4 +25,10 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
if (typeof sym === 'string') { return false; } | ||
if (sym instanceof Symbol) { return false; } | ||
obj[sym] = 42; | ||
// temp disabled per https://github.com/ljharb/object.assign/issues/17 | ||
// if (sym instanceof Symbol) { return false; } | ||
// temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4 | ||
// if (!(Object(sym) instanceof Symbol)) { return false; } | ||
var symVal = 42; | ||
obj[sym] = symVal; | ||
for (sym in obj) { return false; } | ||
@@ -41,3 +47,3 @@ if (keys(obj).length !== 0) { return false; } | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, sym); | ||
if (descriptor.value !== 42 || descriptor.enumerable !== true) { return false; } | ||
if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; } | ||
} | ||
@@ -65,3 +71,3 @@ | ||
var objTarget = toObject(target); | ||
var s, source, i, props, syms; | ||
var s, source, i, props, syms, value, key; | ||
for (s = 1; s < arguments.length; ++s) { | ||
@@ -73,4 +79,5 @@ source = toObject(arguments[s]); | ||
for (i = 0; i < syms.length; ++i) { | ||
if (propIsEnumerable(source, syms[i])) { | ||
push(props, syms[i]); | ||
key = syms[i]; | ||
if (propIsEnumerable(source, key)) { | ||
push(props, key); | ||
} | ||
@@ -80,3 +87,7 @@ } | ||
for (i = 0; i < props.length; ++i) { | ||
objTarget[props[i]] = source[props[i]]; | ||
key = props[i]; | ||
value = source[key]; | ||
if (propIsEnumerable(source, key)) { | ||
objTarget[key] = value; | ||
} | ||
} | ||
@@ -261,2 +272,3 @@ } | ||
$console: true, | ||
$frame: true, | ||
$frameElement: true, | ||
@@ -263,0 +275,0 @@ $frames: true, |
@@ -12,4 +12,10 @@ 'use strict'; | ||
if (typeof sym === 'string') { return false; } | ||
if (sym instanceof Symbol) { return false; } | ||
obj[sym] = 42; | ||
// temp disabled per https://github.com/ljharb/object.assign/issues/17 | ||
// if (sym instanceof Symbol) { return false; } | ||
// temp disabled per https://github.com/WebReflection/get-own-property-symbols/issues/4 | ||
// if (!(Object(sym) instanceof Symbol)) { return false; } | ||
var symVal = 42; | ||
obj[sym] = symVal; | ||
for (sym in obj) { return false; } | ||
@@ -28,3 +34,3 @@ if (keys(obj).length !== 0) { return false; } | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, sym); | ||
if (descriptor.value !== 42 || descriptor.enumerable !== true) { return false; } | ||
if (descriptor.value !== symVal || descriptor.enumerable !== true) { return false; } | ||
} | ||
@@ -31,0 +37,0 @@ |
@@ -17,3 +17,3 @@ 'use strict'; | ||
var objTarget = toObject(target); | ||
var s, source, i, props, syms; | ||
var s, source, i, props, syms, value, key; | ||
for (s = 1; s < arguments.length; ++s) { | ||
@@ -25,4 +25,5 @@ source = toObject(arguments[s]); | ||
for (i = 0; i < syms.length; ++i) { | ||
if (propIsEnumerable(source, syms[i])) { | ||
push(props, syms[i]); | ||
key = syms[i]; | ||
if (propIsEnumerable(source, key)) { | ||
push(props, key); | ||
} | ||
@@ -32,3 +33,7 @@ } | ||
for (i = 0; i < props.length; ++i) { | ||
objTarget[props[i]] = source[props[i]]; | ||
key = props[i]; | ||
value = source[key]; | ||
if (propIsEnumerable(source, key)) { | ||
objTarget[key] = value; | ||
} | ||
} | ||
@@ -35,0 +40,0 @@ } |
{ | ||
"name": "object.assign", | ||
"version": "4.0.2", | ||
"version": "4.0.3", | ||
"author": "Jordan Harband", | ||
@@ -10,4 +10,8 @@ "description": "ES6 spec-compliant Object.assign shim. From https://github.com/es-shims/es6-shim", | ||
"test": "npm run lint && es-shim-api && npm run tests-only && npm run security", | ||
"tests-only": "node test/index.js && npm run test:symbol-shim", | ||
"test:symbol-shim": "node test/symbolShim.js", | ||
"tests-only": "npm run test:implementation && npm run test:shim && npm run test:shams", | ||
"test:shim": "node test/shimmed.js", | ||
"test:implementation": "node test/index.js", | ||
"test:shams": "npm run test:shams:getownpropertysymbols && npm run test:shams:corejs", | ||
"test:shams:corejs": "node test/shams/core-js.js", | ||
"test:shams:getownpropertysymbols": "node test/shams/get-own-property-symbols.js", | ||
"coverage": "covert test/*.js", | ||
@@ -20,3 +24,3 @@ "coverage:quiet": "covert test/*.js --quiet", | ||
"prepublish": "npm run build", | ||
"security": "nsp package" | ||
"security": "requiresafe check" | ||
}, | ||
@@ -42,3 +46,3 @@ "repository": { | ||
"function-bind": "^1.0.2", | ||
"object-keys": "^1.0.8", | ||
"object-keys": "^1.0.9", | ||
"define-properties": "^1.1.2" | ||
@@ -49,9 +53,10 @@ }, | ||
"is": "^3.1.0", | ||
"tape": "^4.2.1", | ||
"tape": "^4.2.2", | ||
"covert": "^1.1.0", | ||
"jscs": "^2.3.4", | ||
"nsp": "^1.1.0", | ||
"eslint": "^1.7.1", | ||
"jscs": "^2.3.5", | ||
"requiresafe": "^2.3.0", | ||
"eslint": "^1.7.2", | ||
"@ljharb/eslint-config": "^1.4.1", | ||
"get-own-property-symbols": "^0.5.1", | ||
"core-js": "^1.2.2", | ||
"@es-shims/api": "^1.0.0", | ||
@@ -58,0 +63,0 @@ "for-each": "^0.3.2" |
'use strict'; | ||
var assign = require('../'); | ||
var test = require('tape'); | ||
var assign = require('../'); | ||
var hasSymbols = require('../hasSymbols')(); | ||
var keys = require('object-keys'); | ||
var forEach = require('for-each'); | ||
var runTests = require('./tests'); | ||
test('error cases', function (t) { | ||
t.throws(function () { assign(null); }, TypeError, 'target must be an object'); | ||
t.throws(function () { assign(undefined); }, TypeError, 'target must be an object'); | ||
t.throws(function () { assign(null, {}); }, TypeError, 'target must be an object'); | ||
t.throws(function () { assign(undefined, {}); }, TypeError, 'target must be an object'); | ||
t.end(); | ||
}); | ||
test('non-object target, no sources', function (t) { | ||
var bool = assign(true); | ||
t.equal(typeof bool, 'object', 'bool is object'); | ||
t.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`'); | ||
var number = assign(1); | ||
t.equal(typeof number, 'object', 'number is object'); | ||
t.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`'); | ||
var string = assign('1'); | ||
t.equal(typeof string, 'object', 'number is object'); | ||
t.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`'); | ||
t.end(); | ||
}); | ||
test('non-object target, with sources', function (t) { | ||
var signal = {}; | ||
t.test('boolean', function (st) { | ||
var bool = assign(true, { a: signal }); | ||
st.equal(typeof bool, 'object', 'bool is object'); | ||
st.equal(Boolean.prototype.valueOf.call(bool), true, 'bool coerces to `true`'); | ||
st.equal(bool.a, signal, 'source properties copied'); | ||
test('as a function', function (t) { | ||
t.test('bad array/this value', function (st) { | ||
st.throws(function () { assign(undefined); }, TypeError, 'undefined is not an object'); | ||
st.throws(function () { assign(null); }, TypeError, 'null is not an object'); | ||
st.end(); | ||
}); | ||
t.test('number', function (st) { | ||
var number = assign(1, { a: signal }); | ||
st.equal(typeof number, 'object', 'number is object'); | ||
st.equal(Number.prototype.valueOf.call(number), 1, 'number coerces to `1`'); | ||
st.equal(number.a, signal, 'source properties copied'); | ||
st.end(); | ||
}); | ||
runTests(assign, t); | ||
t.test('string', function (st) { | ||
var string = assign('1', { a: signal }); | ||
st.equal(typeof string, 'object', 'number is object'); | ||
st.equal(String.prototype.valueOf.call(string), '1', 'number coerces to `"1"`'); | ||
st.equal(string.a, signal, 'source properties copied'); | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); | ||
test('non-object sources', function (t) { | ||
t.deepEqual(assign({ a: 1 }, null, { b: 2 }), { a: 1, b: 2 }, 'ignores null source'); | ||
t.deepEqual(assign({ a: 1 }, { b: 2 }, undefined), { a: 1, b: 2 }, 'ignores undefined source'); | ||
t.end(); | ||
}); | ||
test('returns the modified target object', function (t) { | ||
var target = {}; | ||
var returned = assign(target, { a: 1 }); | ||
t.equal(returned, target, 'returned object is the same reference as the target object'); | ||
t.end(); | ||
}); | ||
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) { | ||
var target = { a: 1 }; | ||
var returned = assign(target, { b: 2 }); | ||
t.deepEqual(returned, { a: 1, b: 2 }, 'returned object has properties from both'); | ||
t.end(); | ||
}); | ||
test('works with functions', function (t) { | ||
var target = function () {}; | ||
target.a = 1; | ||
var returned = assign(target, { b: 2 }); | ||
t.equal(target, returned, 'returned object is target'); | ||
t.equal(returned.a, 1); | ||
t.equal(returned.b, 2); | ||
t.end(); | ||
}); | ||
test('works with primitives', function (t) { | ||
var target = 2; | ||
var source = { b: 42 }; | ||
var returned = assign(target, source); | ||
t.equal(Object.prototype.toString.call(returned), '[object Number]', 'returned is object form of number primitive'); | ||
t.equal(Number(returned), target, 'returned and target have same valueOf'); | ||
t.equal(returned.b, source.b); | ||
t.end(); | ||
}); | ||
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) { | ||
var Foo = function () {}; | ||
Foo.prototype.bar = true; | ||
var foo = new Foo(); | ||
foo.baz = true; | ||
var target = { a: 1 }; | ||
var returned = assign(target, foo); | ||
t.equal(returned, target, 'returned object is the same reference as the target object'); | ||
t.deepEqual(target, { baz: true, a: 1 }, 'returned object has only own properties from both'); | ||
t.end(); | ||
}); | ||
test('includes enumerable symbols, after keys', { skip: !hasSymbols }, function (t) { | ||
var visited = []; | ||
var obj = {}; | ||
Object.defineProperty(obj, 'a', { get: function () { visited.push('a'); return 42; }, enumerable: true }); | ||
var symbol = Symbol('enumerable'); | ||
Object.defineProperty(obj, symbol, { get: function () { visited.push(symbol); return Infinity; }, enumerable: true }); | ||
var nonEnumSymbol = Symbol('non-enumerable'); | ||
Object.defineProperty(obj, nonEnumSymbol, { get: function () { visited.push(nonEnumSymbol); return -Infinity; }, enumerable: false }); | ||
var target = assign({}, obj); | ||
t.deepEqual(visited, ['a', symbol], 'key is visited first, then symbol'); | ||
t.equal(target.a, 42, 'target.a is 42'); | ||
t.equal(target[symbol], Infinity, 'target[symbol] is Infinity'); | ||
t.notEqual(target[nonEnumSymbol], -Infinity, 'target[nonEnumSymbol] is not -Infinity'); | ||
t.end(); | ||
}); | ||
test('does not fail when symbols are not present', function (t) { | ||
var getSyms; | ||
if (hasSymbols) { | ||
getSyms = Object.getOwnPropertySymbols; | ||
delete Object.getOwnPropertySymbols; | ||
} | ||
var visited = []; | ||
var obj = {}; | ||
Object.defineProperty(obj, 'a', { get: function () { visited.push('a'); return 42; }, enumerable: true }); | ||
if (hasSymbols) { | ||
var symbol = Symbol(); | ||
Object.defineProperty(obj, symbol, { get: function () { visited.push(symbol); return Infinity; }, enumerable: true }); | ||
} | ||
var target = assign({}, obj); | ||
t.equal(target.a, 42, 'target.a is 42'); | ||
t.deepEqual(visited, ['a'], 'only key is visited'); | ||
if (hasSymbols) { | ||
// sanity check for "visited" array | ||
t.equal(obj[symbol], Infinity); | ||
t.deepEqual(visited, ['a', symbol], 'symbol is visited manually'); | ||
Object.getOwnPropertySymbols = getSyms; | ||
} | ||
t.end(); | ||
}); | ||
test('exports a "shim" function', function (t) { | ||
t.equal(typeof assign.shim, 'function', 'assign.shim is a function'); | ||
t.test('when Object.assign is not present', function (st) { | ||
var originalObjectAssign = Object.assign; | ||
delete Object.assign; | ||
var shimmedAssign = assign.shim(); | ||
st.equal(Object.assign, assign, 'Object.assign is overridden'); | ||
st.equal(shimmedAssign, assign, 'shim is returned'); | ||
if (Object.getOwnPropertyDescriptor) { | ||
st.equal(Object.getOwnPropertyDescriptor(Object, 'assign').enumerable, false, 'is not enumerable'); | ||
} | ||
Object.assign = originalObjectAssign; | ||
st.end(); | ||
}); | ||
t.test('when Object.assign is present and has pending exceptions', { skip: !Object.assign || !Object.preventExtensions }, function (st) { | ||
var originalObjectAssign = Object.assign; | ||
delete Object.assign; | ||
assign.shim(); | ||
// Firefox 37 still has "pending exception" logic in its Object.assign implementation, | ||
// which is 72% slower than our shim, and Firefox 40's native implementation. | ||
var thrower = Object.preventExtensions({ 1: 2 }); | ||
var error; | ||
try { Object.assign(thrower, 'xy'); } catch (e) { error = e; } | ||
st.equal(error instanceof TypeError, true, 'error is TypeError'); | ||
st.equal(thrower[1], 2, 'thrower[1] === 2'); | ||
Object.assign = originalObjectAssign; | ||
st.end(); | ||
}); | ||
var testEnumerationOrder = function (testObject, objectAssign) { | ||
var str = 'abcdefghijklmnopqrst'; | ||
var letters = str.split('').reduce(function (map, letter) { | ||
map[letter] = letter; | ||
return map; | ||
}, {}); | ||
var n = 5; | ||
testObject.comment('run the next test ' + n + ' times'); | ||
var object = objectAssign({}, letters); | ||
var actual = ''; | ||
for (var k in object) { | ||
actual += k; | ||
} | ||
for (var i = 0; i < n; ++i) { | ||
testObject.equal(actual, str, 'property enumeration order should be followed'); | ||
} | ||
}; | ||
t.test('preserves correct property enumeration order', function (st) { | ||
st.comment('implementation:'); | ||
testEnumerationOrder(st, assign); | ||
var original = Object.assign; | ||
assign.shim(); | ||
st.comment('shimmed method:'); | ||
testEnumerationOrder(st, Object.assign); | ||
Object.assign = original; | ||
st.end(); | ||
}); | ||
t.end(); | ||
}); | ||
test('working with actual shim', function (t) { | ||
t.notEqual(Object.assign, assign, 'assign shim is not native Object.assign'); | ||
t.end(); | ||
}); | ||
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
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
41844
16
806
12
Updatedobject-keys@^1.0.9