Skip to content

Commit

Permalink
feat: add Object.entries
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed May 29, 2018
1 parent 0ca1873 commit 51d2f43
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions object/entries/implement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

if (!require("./is-implemented")()) {
Object.defineProperty(Object, "entries", {
value: require("./shim"),
configurable: true,
enumerable: false,
writable: true
});
}
3 changes: 3 additions & 0 deletions object/entries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

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

module.exports = function () {
try {
return Object.entries({ foo: 12 })[0][0] === "foo";
} catch (e) {
return false;
}
};
14 changes: 14 additions & 0 deletions object/entries/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

var ensureValue = require("../valid-value");

module.exports = function (object) {
ensureValue(object);
var result = [];
object = Object(object);
for (var key in object) {
if (!propertyIsEnumerable.call(object, key)) continue;
result.push([key, object[key]]);
}
return result;
};
1 change: 1 addition & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
ensurePlainFunction: require("./ensure-plain-function"),
ensurePromise: require("./ensure-promise"),
ensureThenable: require("./ensure-thenable"),
entries: require("./entries"),
eq: require("./eq"),
every: require("./every"),
filter: require("./filter"),
Expand Down
5 changes: 5 additions & 0 deletions test/object/entries/implement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

var isImplemented = require("../../../object/entries/is-implemented");

module.exports = function (a) { a(isImplemented(), true); };
3 changes: 3 additions & 0 deletions test/object/entries/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./shim");
5 changes: 5 additions & 0 deletions test/object/entries/is-implemented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

module.exports = function (t, a) {
a(typeof t(), "boolean");
};
8 changes: 8 additions & 0 deletions test/object/entries/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";

module.exports = function (t, a) {
a.deep(t({ foo: "bar" }), [["foo", "bar"]], "Object");
a.deep(t("raz"), [["0", "r"], ["1", "a"], ["2", "z"]], "Primitive");
a.throws(function () { t(); }, TypeError, "Undefined");
a.throws(function () { t(null); }, TypeError, "Undefined");
};

0 comments on commit 51d2f43

Please sign in to comment.