Skip to content

Commit

Permalink
feat: Object.ensureArray util
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Aug 18, 2017
1 parent 860fe8b commit 595c341
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions object/ensure-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var toShortString = require("../to-short-string-representation")
, isArray = require("./is-array-like");

module.exports = function (value) {
if (isArray(value)) return value;
throw new TypeError(toShortString(value) + " is not a array");
};
1 change: 1 addition & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
copyDeep: require("./copy-deep"),
count: require("./count"),
create: require("./create"),
ensureArray: require("./ensure-array"),
ensureFiniteNumber: require("./ensure-finite-number"),
ensureNaturalNumber: require("./ensure-natural-number"),
ensureNaturalNumberValue: require("./ensure-natural-number-value"),
Expand Down
55 changes: 55 additions & 0 deletions test/object/ensure-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

module.exports = function (t, a) {
var arr = [];
a(t(arr), arr, "Array");
a(t(""), "", "String");
var args = (function () {
return arguments;
}());
a(t(args), args, "Arguments");
var arrayLike = { length: 0 };
a(t(arrayLike), arrayLike, "Array like");
a.throws(
function () {
t(function () {});
},
TypeError,
"Function"
);
a.throws(
function () {
t({});
},
TypeError,
"Plain object"
);
a.throws(
function () {
t(/raz/);
},
TypeError,
"Regexp"
);
a.throws(
function () {
t();
},
TypeError,
"No argument"
);
a.throws(
function () {
t(null);
},
TypeError,
"Null"
);
a.throws(
function () {
t(undefined);
},
TypeError,
"Undefined"
);
};

0 comments on commit 595c341

Please sign in to comment.