-
-
Notifications
You must be signed in to change notification settings - Fork 21
isValid to validate timestamp #29
Copy link
Copy link
Open
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers
Description
The isValid works ok, but does not take in account the validation of timestamp and randomness separately, hence I suggest this enhancement to validate the timestamp part:
function isValid(id) {
var valid = (
typeof id === "string" &&
id.length === TIME_LEN + RANDOM_LEN &&
id
.toUpperCase()
.split("")
.every(char => ENCODING.indexOf(char) !== -1)
);
try { valid &= isTimestamp(decodeTime(id)); } catch(error) { valid = false }
return valid;
}where decodeTime is ulid.decodeTime and utils are
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function isTimestamp(_timestamp) {
const newTimestamp = new Date(_timestamp).getTime();
return isNumeric(newTimestamp);
}This should be enough to guaratee that both ulid and monotonic are valid ulid with timestamp and randomness information.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers