Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
43 changes: 43 additions & 0 deletions object/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Workaround for http://code.google.com/p/v8/issues/detail?id=2804

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To maintain changes history, ideally, if this is moved module from _es5-ext/object/create.js


"use strict";

var create = Object.create, shim;

if (!require("./set-prototype-of/is-implemented")()) {
shim = require("./set-prototype-of/implementation");
}

module.exports = (function () {
var nullObject, polyProps, desc;
if (!shim) return create;
if (shim.level !== 1) return create;

nullObject = {};
polyProps = {};
desc = { configurable: false, enumerable: false, writable: true, value: undefined };
Object.getOwnPropertyNames(Object.prototype).forEach(function (name) {
if (name === "__proto__") {
polyProps[name] = {
configurable: true,
enumerable: false,
writable: true,
value: undefined
};
return;
}
polyProps[name] = desc;
});
Object.defineProperties(nullObject, polyProps);

Object.defineProperty(shim, "nullPolyfill", {
configurable: false,
enumerable: false,
writable: false,
value: nullObject
});

return function (prototype, props) {
return create(prototype === null ? nullObject : prototype, props);
};
})();
7 changes: 7 additions & 0 deletions object/is-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use strict";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be introduced (we should rely on type/object/is


var isValue = require("./is-value");

var map = { function: true, object: true };

module.exports = function (value) { return (isValue(value) && map[typeof value]) || false; };
10 changes: 10 additions & 0 deletions object/set-prototype-of/implement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those modules should be moved from _es5-ext/object/set-prototype-of


if (!require("./is-implemented")()) {
Object.defineProperty(Object, "setPrototypeOf", {
value: require("./implementation"),
configurable: true,
enumerable: false,
writable: true
});
}
81 changes: 81 additions & 0 deletions object/set-prototype-of/implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint no-proto: "off" */

// Big thanks to @WebReflection for sorting this out
// https://gist.github.com/WebReflection/5593554

"use strict";

var isObject = require("../is-object")
, value = require("../valid-value")
, objIsPrototypeOf = Object.prototype.isPrototypeOf
, defineProperty = Object.defineProperty
, nullDesc = { configurable: true, enumerable: false, writable: true, value: undefined }
, validate;

validate = function (obj, prototype) {
value(obj);
if (prototype === null || isObject(prototype)) return obj;
throw new TypeError("Prototype must be null or an object");
};

module.exports = (function (status) {
var fn, set;
if (!status) return null;
if (status.level === 2) {
if (status.set) {
set = status.set;
fn = function (obj, prototype) {
set.call(validate(obj, prototype), prototype);
return obj;
};
} else {
fn = function (obj, prototype) {
validate(obj, prototype).__proto__ = prototype;
return obj;
};
}
} else {
fn = function self(obj, prototype) {
var isNullBase;
validate(obj, prototype);
isNullBase = objIsPrototypeOf.call(self.nullPolyfill, obj);
if (isNullBase) delete self.nullPolyfill.__proto__;
if (prototype === null) prototype = self.nullPolyfill;
obj.__proto__ = prototype;
if (isNullBase) defineProperty(self.nullPolyfill, "__proto__", nullDesc);
return obj;
};
}
return Object.defineProperty(fn, "level", {
configurable: false,
enumerable: false,
writable: false,
value: status.level
});
})(
(function () {
var tmpObj1 = Object.create(null)
, tmpObj2 = {}
, set
, desc = Object.getOwnPropertyDescriptor(Object.prototype, "__proto__");

if (desc) {
try {
set = desc.set; // Opera crashes at this point
set.call(tmpObj1, tmpObj2);
} catch (ignore) {}
if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { set: set, level: 2 };
}

tmpObj1.__proto__ = tmpObj2;
if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { level: 2 };

tmpObj1 = {};
tmpObj1.__proto__ = tmpObj2;
if (Object.getPrototypeOf(tmpObj1) === tmpObj2) return { level: 1 };

return false;
})()
);

require("../create");
3 changes: 3 additions & 0 deletions object/set-prototype-of/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./is-implemented")() ? Object.setPrototypeOf : require("./implementation");
9 changes: 9 additions & 0 deletions object/set-prototype-of/is-implemented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var create = Object.create, getPrototypeOf = Object.getPrototypeOf, plainObject = {};

module.exports = function (/* CustomCreate*/) {
var setPrototypeOf = Object.setPrototypeOf, customCreate = arguments[0] || create;
if (typeof setPrototypeOf !== "function") return false;
return getPrototypeOf(setPrototypeOf(customCreate(null), plainObject)) === plainObject;
};
8 changes: 8 additions & 0 deletions object/valid-value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be introduced (we should rely on type/value/ensure)


var isValue = require("./is-value");

module.exports = function (value) {
if (!isValue(value)) throw new TypeError("Cannot use null or undefined");
return value;
};