Skip to content

Commit f48fbcf

Browse files
committed
feat: Object.ensurePlainObject util
1 parent ffb9c33 commit f48fbcf

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

object/ensure-plain-object.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
3+
var safeToString = require("../safe-to-string")
4+
, isPlainObject = require("./is-plain-object");
5+
6+
module.exports = function (value) {
7+
if (!isPlainObject(value)) throw new TypeError(safeToString(value) + " is not a plain object");
8+
return value;
9+
};

object/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
ensureNaturalNumber: require("./ensure-natural-number"),
1717
ensureNaturalNumberValue: require("./ensure-natural-number-value"),
1818
ensurePlainFunction: require("./ensure-plain-function"),
19+
ensurePlainObject: require("./ensure-plain-object"),
1920
ensurePromise: require("./ensure-promise"),
2021
ensureThenable: require("./ensure-thenable"),
2122
entries: require("./entries"),

test/object/ensure-plain-object.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
3+
module.exports = function (t, a) {
4+
// Just sanity checks, as logic is tested at isPlainFunction
5+
var obj = {};
6+
a(t(obj), obj, "Reguar object instance");
7+
obj = Object.create(null);
8+
a(t(obj), obj, "Null prototype");
9+
a.throws(
10+
function () {
11+
t(function () {});
12+
},
13+
TypeError,
14+
"Error"
15+
);
16+
};

0 commit comments

Comments
 (0)