5 ms·
I like to also remove 0 and 1 — because the human entering the code probably doesn't know that 0, o and O will be considered equivalent — and A and E — because
by gmac 4y ago
I like to also remove 0 and 1 — because the human entering the code probably doesn't know that 0, o and O will be considered equivalent — and A and E — because accidental profanity isn't actually prevented by excluding only U and I.
This gives base 28, of course, which is admittedly slightly less neat, but still perfectly usable. For example, in TypeScript I use this:
const targetChars = '23456789bcdfghjkmnpqrstvwxyz'; // must be <=36 chars
const sourceChars = '0123456789abcdefghijklmnopqr'; // what we get from toString(radix)
const radix = targetChars.length;
export function tokenFromId(id: number) {
return [...id.toString(radix)]
.map(origChar => targetChars.charAt(sourceChars.indexOf(origChar)))
.join('');
}
export function idFromToken(token: string) {
return parseInt([...token.toLowerCase()]
.map(pathChar => sourceChars.charAt(targetChars.indexOf(pathChar)))
.join('')
, radix);
}
- junon 4y agoTo each their own, but the point is that the human doesn't need to know that 0, o and O are considered the same.