-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(object): isInteger and ensureInteger
- Loading branch information
Showing
5 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}; |