Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isValid to validate timestamp #29

Open
loretoparisi opened this issue Aug 24, 2023 · 0 comments
Open

isValid to validate timestamp #29

loretoparisi opened this issue Aug 24, 2023 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@loretoparisi
Copy link

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.

@perry-mitchell perry-mitchell added enhancement New feature or request good first issue Good for newcomers labels Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants