Skip to content

Commit

Permalink
feat(object): isInteger and ensureInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 9, 2018
1 parent 6b67deb commit a5f7d04
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
9 changes: 9 additions & 0 deletions object/ensure-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

var toShortString = require("../to-short-string-representation")
, isInteger = require("./is-integer");

module.exports = function (num) {
if (!isInteger(num)) throw new TypeError(toShortString(num) + " is not a integer");
return Number(num);
};
2 changes: 2 additions & 0 deletions object/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
create: require("./create"),
ensureArray: require("./ensure-array"),
ensureFiniteNumber: require("./ensure-finite-number"),
ensureInteger: require("./ensure-integer"),
ensureNaturalNumber: require("./ensure-natural-number"),
ensureNaturalNumberValue: require("./ensure-natural-number-value"),
ensurePromise: require("./ensure-promise"),
Expand All @@ -31,6 +32,7 @@ module.exports = {
isCopyDeep: require("./is-copy-deep"),
isEmpty: require("./is-empty"),
isFiniteNumber: require("./is-finite-number"),
isInteger: require("./is-integer"),
isNaturalNumber: require("./is-natural-number"),
isNaturalNumberValue: require("./is-natural-number-value"),
isNumberValue: require("./is-number-value"),
Expand Down
10 changes: 10 additions & 0 deletions object/is-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"use strict";

var isInteger = require("../number/is-integer")
, isValue = require("./is-value");

module.exports = function (arg) {
if (!isValue(arg)) return false;
arg = Number(arg);
return isInteger(arg);
};
42 changes: 42 additions & 0 deletions test/object/ensure-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

module.exports = function (t, a) {
a.throws(
function () {
t(undefined);
},
TypeError,
"Undefined"
);
a.throws(
function () {
t(null);
},
TypeError,
"Null"
);
a(t(2), 2, "Number");
a(t(-2), -2, "Negative");
a.throws(
function () {
t(2.34);
},
TypeError,
"Float"
);
a(t("23"), 23, "Numeric string");
a.throws(
function () {
t(NaN);
},
TypeError,
"NaN"
);
a.throws(
function () {
t(Infinity);
},
TypeError,
"Infinity"
);
};
12 changes: 12 additions & 0 deletions test/object/is-integer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

module.exports = function (t, a) {
a(t(undefined), false, "Undefined");
a(t(null), false, "Null");
a(t(2), true, "Number");
a(t(-2), true, "Negative");
a(t(2.34), false, "Float");
a(t("23"), true, "Numeric string");
a(t(NaN), false, "NaN");
a(t(Infinity), false, "Infinity");
};

0 comments on commit a5f7d04

Please sign in to comment.