Skip to content

Commit

Permalink
feat: array/ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 3, 2019
1 parent 9672839 commit 9db1515
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ isArray({}); // false
isArray("foo"); // false
```

##### `array/ensure`

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

```javascript
const ensureArray = require("type/array/ensure");

ensureArray(["foo"]); // ["foo"]
ensureArray("foo"); // Thrown TypeError: null is not a regular expression object
```

#### RegExp

The JavaScript _RegExp_ instance
Expand Down
9 changes: 9 additions & 0 deletions array/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 array object", arguments[1]);
};
20 changes: 20 additions & 0 deletions test/array/ensure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

var assert = require("chai").assert
, ensureArray = require("../../array/ensure");

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

0 comments on commit 9db1515

Please sign in to comment.