Skip to content

Commit

Permalink
feat: object/ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 25, 2019
1 parent b62577d commit a9e8eed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ isObject(true); // false
isObject(null); // false
```

##### `object/ensure`

If given argument is an object, it is returned back. Otherwise `TypeError` is thrown.

```javascript
const ensureObject = require("type/object/ensure");

const obj = {};

ensureObject(obj); // obj
ensureString(null); // Thrown TypeError: null is not an object
```

#### String

_string_ primitives
Expand Down
9 changes: 9 additions & 0 deletions object/ensure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var resolveException = require("../lib/resolve-exception")
, is = require("./is");

module.exports = function (value/*, options*/) {
if (is(value)) return value;
return resolveException(value, "%v is not an object", arguments[1]);
};
20 changes: 20 additions & 0 deletions test/object/ensure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var assert = require("chai").assert
, ensureObject = require("../../object/ensure");

describe("string/ensure", function () {
it("Should return input value", function () {
var value = {};
assert.equal(ensureObject(value), value);
});
it("Should crash on no value", function () {
try {
ensureObject(null);
throw new Error("Unexpected");
} catch (error) {
assert.equal(error.name, "TypeError");
assert.equal(error.message, "null is not an object");
}
});
});

0 comments on commit a9e8eed

Please sign in to comment.