-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(date): isTimeValue and ensureTimeValue utils
- Loading branch information
Showing
5 changed files
with
53 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,10 @@ | ||
"use strict"; | ||
|
||
var safeToString = require("../safe-to-string") | ||
, toInteger = require("../number/to-integer") | ||
, isTimeValue = require("./is-time-value"); | ||
|
||
module.exports = function (value) { | ||
if (isTimeValue(value)) return toInteger(value); | ||
throw new TypeError(safeToString(value) + " is not a valid time value"); | ||
}; |
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,12 @@ | ||
"use strict"; | ||
|
||
module.exports = function (value) { | ||
try { | ||
value = Number(value); | ||
} catch (e) { | ||
return false; | ||
} | ||
if (isNaN(value)) return false; | ||
if (Math.abs(value) > 8.64e16) return false; | ||
return true; | ||
}; |
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,14 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
a(t(12), 12, "Number in range"); | ||
a(t(12.23), 12, "Rounds number in range"); | ||
a(t(-12.63), -12, "Rounds negative number in range"); | ||
a.throws( | ||
function () { | ||
t(NaN); | ||
}, | ||
TypeError, | ||
"Throws on invalid" | ||
); | ||
}; |
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,15 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
a(t("arar"), false, "String"); | ||
a(t(12), true, "Number in range"); | ||
a(t(true), true, "Boolean"); | ||
a(t(new Date()), true, "Date"); | ||
a(t({}), false, "Plain object"); | ||
a(t(NaN), false, "NaN"); | ||
a(t(Infinity), false, "Infinity"); | ||
a(t(8.64e17), false, "Beyond range"); | ||
a(t(8.64e15), true, "Below range"); | ||
a(t(-8.64e17), false, "Negative beyond range"); | ||
a(t(-8.64e15), true, "Negative below range"); | ||
}; |