Light wrapper around assorted ID generation and validation functions. Currently:
asin(input: string)
returns the raw input if it's a plausibly-formatted Amazon product ID, andundefined
if it isn't.asin.isValid()
andasin.isIsbn(input: string)
functions are useful for explicit validationasin.asUrl(input: string)
returns a full Amazon product URL for the ID if it's valid.
isbn(input: string)
parses and returns detailed information about the internal structure of any valid ISBN; if valid, theisbn10
andisbn13
properties can be used to retrieve both permutations if available.isbn.asIsbn13(input: string)
is a useful shortcut, returningundefined
if the ISBN is invalid, and expanding ISBN10s to the full 13 digits, in one step.
hash(input: NotUndefined)
just wraps the object-hash library for convenience.md5()
,sha1()
,sha256()
,sha256()
are convenience wrappers forobject-hash
's algorithm-specific hashing options.- See
nanohash
, below, for an alternative that's not quite as secure but nice and short.
nanoid(size?: number, alphabet?: string)
wraps the nanoid library, which generates arbitrarily-sized, url-safe, collision-resistant IDs. Shorter than UUIDs by default, with options to control the dictionary of characters used and the final length of the ID.nanohash(input: any, size?: number, alphabet?: string)
uses a simpler hashing algorithm than the UUID and hash helpers, but leverages the bufferbase library to convert them into the same dictionary-based format as nanoid. By default hashes are 7 characters long, but that will shift around if longer/shorter alphabets are passed in.alphabets
is a useful list of potential character sets that can be used with nanoid and nanohash. URL Safe strings are the default for both, but options like 'Uppercase' and 'NoLookalikes' can be handy as well.
ssn.isValid(input: string)
validates US Social Security Numbers, which would be terrible to use in a random personal web project but serve as a nice test case.ssn.format(input: string)
pretty-prints SSNs with dashes in the proper locations.
uuid()
generates a random UUID4.uuid(input: any)
useshash.sha1
from the hashing helper functions, but formats the resulting value as a UUID5. If an explicit null value is given, the nil UUID is returned.uuid.isValid(input: string)
can be used to check an existing string.uuid.random()
can be used to unambiguously generate a random UUID4.uuid.setNamespace()
can be used to set a custom namespace for UUID5 generation; it must be a valid UUID.uuid.namespaces
provides convenience consts for the official URL and DNS namespaces;uuid.namespaces.fyi
is the one I use by default.
ulid()
generates a unique, creation-time-sortable identifier that's URL-safe and a touch shorter than a UUID. Uses the ulid reference library.