diff --git a/src/object.js b/src/object.js index 3d83ac2..2be3e64 100644 --- a/src/object.js +++ b/src/object.js @@ -1,19 +1,19 @@ if (typeof FEATURE_NO_ES2015 === 'undefined') { -(function() { - let needsFix = false; - - //ES5 did not accept primitives, but ES6 does - try { - let s = Object.keys('a'); - needsFix = (s.length !== 1 || s[0] !== '0'); - } catch(e) { - needsFix = true; - } + (function() { + let needsFix = false; + + //ES5 did not accept primitives, but ES6 does + try { + let s = Object.keys('a'); + needsFix = (s.length !== 1 || s[0] !== '0'); + } catch(e) { + needsFix = true; + } - if (needsFix) { - Object.keys = (function() { - var hasOwnProperty = Object.prototype.hasOwnProperty, + if (needsFix) { + Object.keys = (function() { + var hasOwnProperty = Object.prototype.hasOwnProperty, hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), dontEnums = [ 'toString', @@ -26,54 +26,54 @@ if (typeof FEATURE_NO_ES2015 === 'undefined') { ], dontEnumsLength = dontEnums.length; - return function(obj) { - if (obj === undefined || obj === null){ - throw TypeError(`Cannot convert undefined or null to object`); - } + return function(obj) { + if (obj === undefined || obj === null) { + throw TypeError(`Cannot convert undefined or null to object`); + } - obj = Object(obj); + obj = Object(obj); - var result = [], prop, i; + var result = [], prop, i; - for (prop in obj) { - if (hasOwnProperty.call(obj, prop)) { - result.push(prop); + for (prop in obj) { + if (hasOwnProperty.call(obj, prop)) { + result.push(prop); + } } - } - if (hasDontEnumBug) { - for (i = 0; i < dontEnumsLength; i++) { - if (hasOwnProperty.call(obj, dontEnums[i])) { - result.push(dontEnums[i]); + if (hasDontEnumBug) { + for (i = 0; i < dontEnumsLength; i++) { + if (hasOwnProperty.call(obj, dontEnums[i])) { + result.push(dontEnums[i]); + } } } - } - return result; - }; - }()); - } -}()); + return result; + }; + }()); + } + }()); -(function (O) { - if ('assign' in O) { - return; - } + (function (O) { + if ('assign' in O) { + return; + } - O.defineProperty(O, 'assign', { + O.defineProperty(O, 'assign', { configurable: true, writable: true, value: (function() { var gOPS = O.getOwnPropertySymbols, - // shortcut without explicitly passing through prototype - pIE = O.propertyIsEnumerable, - filterOS = gOPS ? - function (self) { - return gOPS(self).filter(pIE, self); - } : function () { - // just empty Array won't do much within a .concat(...) - return Array.prototype; - }; + // shortcut without explicitly passing through prototype + pIE = O.propertyIsEnumerable, + filterOS = gOPS ? + function (self) { + return gOPS(self).filter(pIE, self); + } : function () { + // just empty Array won't do much within a .concat(...) + return Array.prototype; + }; return function assign(where) { // Object.create(null) and null objects in general @@ -104,23 +104,37 @@ if (typeof FEATURE_NO_ES2015 === 'undefined') { }; }()) }); -}(Object)); - -/** - * Object.is() polyfill - * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is - */ -if (!Object.is) { - Object.is = function(x, y) { - // SameValue algorithm - if (x === y) { // Steps 1-5, 7-10 - // Steps 6.b-6.e: +0 != -0 - return x !== 0 || 1 / x === 1 / y; - } else { - // Step 6.a: NaN == NaN - return x !== x && y !== y; - } - }; -} + }(Object)); + + /** + * Object.is() polyfill + * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is + */ + if (!Object.is) { + Object.is = function(x, y) { + // SameValue algorithm + if (x === y) { // Steps 1-5, 7-10 + // Steps 6.b-6.e: +0 != -0 + return x !== 0 || 1 / x === 1 / y; + } else { + // Step 6.a: NaN == NaN + return x !== x && y !== y; + } + }; + } } // endif FEATURE_NO_ES2015 + +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Polyfill +if (!Object.entries) { + Object.entries = function(obj) { + var ownProps = Object.keys(obj), + i = ownProps.length, + resArray = new Array(i); // preallocate the Array + while (i--) { + resArray[i] = [ownProps[i], obj[ownProps[i]]]; + } + + return resArray; + } +}