ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
https://zelark.github.io/nano-id-cc/
MIT License
24.22k stars 790 forks source link

Maybe throw an error if letters is empty? #428

Closed bendytree closed 1 year ago

bendytree commented 1 year ago

My code had the following bug which hung the server:

import { customAlphabet } from 'nanoid';
const alphabet = 'aeiou';
const nanoid = customAlphabet('', 10);
nanoid(); // hangs forever

Took a bit to find the issue. If letters is null or empty, might be a good spot to throw an error.

ai commented 1 year ago

Sorry, these extra checks don’t fit in an idea of nano-library.

Maybe we can do it by types?

bendytree commented 1 year ago

Doesn't really make me any difference if the check is added; I just thought it'd be helpful for others. Fwiw - I opened the C# and Swift versions to see what they do. C# throws if letters is empty. Swift won't hang because it doesn't use a while(true) loop.

ai commented 1 year ago

If you know how to add it by types, please send PR.

Unfortunately, JS code for check we can’t add because it will bloat the JS footprint of this nano library (and will help for a very small part of users)