Skip to content

Commit

Permalink
feat: Object.ensurePlainObject util
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Feb 22, 2019
1 parent ffb9c33 commit f48fbcf
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions object/ensure-plain-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var safeToString = require("../safe-to-string")
, isPlainObject = require("./is-plain-object");

module.exports = function (value) {
if (!isPlainObject(value)) throw new TypeError(safeToString(value) + " is not a plain object");
return value;
};
1 change: 1 addition & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
ensureNaturalNumber: require("./ensure-natural-number"),
ensureNaturalNumberValue: require("./ensure-natural-number-value"),
ensurePlainFunction: require("./ensure-plain-function"),
ensurePlainObject: require("./ensure-plain-object"),
ensurePromise: require("./ensure-promise"),
ensureThenable: require("./ensure-thenable"),
entries: require("./entries"),
Expand Down
16 changes: 16 additions & 0 deletions test/object/ensure-plain-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use strict";

module.exports = function (t, a) {
// Just sanity checks, as logic is tested at isPlainFunction
var obj = {};
a(t(obj), obj, "Reguar object instance");
obj = Object.create(null);
a(t(obj), obj, "Null prototype");
a.throws(
function () {
t(function () {});
},
TypeError,
"Error"
);
};

0 comments on commit f48fbcf

Please sign in to comment.