Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(object): object.entries polyfill #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 79 additions & 65 deletions src/object.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}