Munchable's server generates its own primary keys for a couple of tables, and the schema deliberately has no database default for them. The ids are ULIDs: 26 characters, sortable by creation time, URL safe. The usual move is npm install ulid. We wrote the function instead, and it is 22 lines.

This is the whole file:

const ENC = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; export function ulid(now: number = Date.now()): string { let ts = ''; let t = now; for (let i = 0; i < 10; i++) { ts = ENC[t % 32] + ts; t = Math.floor(t / 32); } const bytes = crypto.getRandomValues(new Uint8Array(16)); let rand = ''; for (let i = 0; i < 16; i++) rand += ENC[bytes[i] % 32]; return ts + rand; } 

The open source notices for what the site does ship are listed at munchable.app/licenses. This function is not on that list, because there is nothing to list.

The timestamp half

The first ten characters are the current time in milliseconds, written in base 32. Ten characters at five bits each is 50 bits, which comfortably holds the 48 bit millisecond count the format calls for.

The loop uses t % 32 and Math.floor(t / 32) rather than t & 31 and t >>> 5. That is the detail most hand rolled versions get wrong. JavaScript bitwise operators convert their operands to 32 bit integers first, and a current millisecond timestamp is around 1.8 trillion, which is far past 2^32. t & 31 on a number that large silently works on the wrong low bits. Plain arithmetic on a double is exact up to 2^53, so division and modulo are safe where the shift operators are not.

Prepending each digit (ts = ENC[...] + ts) produces the most significant digit first. That ordering is what makes the result sort correctly as a string: later times compare greater, character by character.

The random half

Sixteen random bytes become sixteen characters, one byte each, using bytes[i] % 32.

Taking a random value modulo something is normally a bias bug. If you reduce a number from 0 to 255 modulo 10, the values 0 to 5 appear 26 times and the values 6 to 9 appear 25 times, so some digits are slightly more likely than others. The bias disappears only when the range divides evenly.

Here it does. 256 is exactly 8 times 32, so every one of the 32 symbols is hit by exactly 8 byte values. The distribution is uniform, with no rejection loop and no extra randomness. The alphabet size is the thing carrying the correctness, and it is the sort of fact that is easy to break later by "just adding a character". We wrote the same argument down for our licence key generator, which is also 32 symbols for the same reason.

The alphabet itself is Crockford base 32: digits plus letters with I, L, O and U left out, so a key read aloud or copied from a screenshot cannot be confused with a one or a zero.

crypto.getRandomValues is the global Web Crypto call, available in Node and in the edge runtime, so the same file works wherever the route happens to run. Sixteen characters at five bits is 80 bits of randomness per millisecond, which is far more than two inserts in the same millisecond will ever collide on.

What it does not promise

The format has an optional feature that this one skips on purpose: monotonic ordering within a single millisecond. Two ids generated in the same millisecond share a timestamp and then sort by their random halves, so they are unordered relative to each other. For a primary key that is fine, because nothing here depends on a total order inside one millisecond. A queue that did would need the monotonic variant, and would be a reason to take the package.

It also accepts now as an argument, defaulting to Date.now(). That is not for callers. It is so a test can ask for the id of a fixed instant and assert that two instants sort in the right order.

Why not the package

A dependency is a small bet that the maintainers and the supply chain stay trustworthy for as long as you use it. For a function this size the bet is not worth taking. The header comment in the file puts it as a lean mandate: no package for about 20 lines.

That is not a rule against dependencies. It is a rule that the decision gets made once, in writing, at the top of the file. The next person reads the header, sees the reasoning, and either agrees or replaces it with a better argument.