This commit is contained in:
Carlos Valente
2023-02-20 18:28:54 +01:00
committed by GitHub
parent 63d763cbaf
commit d8a84ac88d
18 changed files with 79 additions and 20 deletions
@@ -0,0 +1,28 @@
import { generateId } from './generateId.js';
test('generate a valid 5 digit id', () => {
const id = generateId();
expect(id.length).toBe(5);
});
test('generate 100 with less than 110 attempts', () => {
const ids = new Set<string>();
let attempts = 1;
while (ids.size < 100) {
ids.add(generateId());
attempts++;
}
expect(attempts).toBeLessThan(105);
});
test('generate 1000 with less than 1020 attempts', () => {
const ids = new Set<string>();
let attempts = 1;
while (ids.size < 1000) {
ids.add(generateId());
attempts++;
}
expect(attempts).toBeLessThan(1020);
});
@@ -0,0 +1,8 @@
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('1234567890abcdef', 5);
/**
* Generates a random id from the defined alphabet
*/
export const generateId = (): string => nanoid();