Normalize behaviour
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 191414f · Browse files · 3 files·+46 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+46 added−51 removed
- tests:+45 added−47 removed
- other:+1 added−4 removed
Provenance
- Commit
464d36b- Tree
289c129- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
191414f
3 files changed+46 added−51 removed
modified:test/benchmark.js+39 added-39 removed
20 unmodified lines212223242526272829303132333435363738392526272829303132333435363738394041424344454647484950515253545556575859606162636465664344454647484950515253545556575859606162636465666768697016 unmodified lines20 unmodified lines
let nanoid2 = customAlphabet('1234567890abcdef-', 10)
bench .add('crypto.randomUUID', () => { crypto.randomUUID() }) .add('uuid v4', () => { uuid4() }) .add('@napi-rs/uuid', () => { napiV4() }) .add('uid/secure', () => { uidSecure(32) }) .add('@lukeed/uuid', () => { lukeed4() }) // .add('crypto.randomUUID', () => { // crypto.randomUUID() // }) // .add('uuid v4', () => { // uuid4() // }) // .add('@napi-rs/uuid', () => { // napiV4() // }) // .add('uid/secure', () => { // uidSecure(32) // }) // .add('@lukeed/uuid', () => { // lukeed4() // }) .add('nanoid', () => { nanoid() }) .add('customAlphabet', () => { nanoid2() }) .add('nanoid for browser', () => { browser() }) .add('secure-random-string', () => { srs() }) .add('uid-safe.sync', () => { uidSafe.sync(14) }) .add('shortid', () => { shortid() }) .add('uid', () => { uid(32) }) .add('nanoid/non-secure', () => { nonSecure() }) .add('rndm', () => { rndm(21) })// .add('customAlphabet', () => {// nanoid2()// })// .add('nanoid for browser', () => {// browser()// })// .add('secure-random-string', () => {// srs()// })// .add('uid-safe.sync', () => {// uidSafe.sync(14)// })// .add('shortid', () => {// shortid()// })// .add('uid', () => {// uid(32)// })// .add('nanoid/non-secure', () => {// nonSecure()// })// .add('rndm', () => {// rndm(21)// })
let longestTask = bench.tasks.reduce((maxLength, task) => { return Math.max(maxLength, task.name.length)}, 0)16 unmodified linesmodified:test/index.test.js+6 added-8 removed
1-import { equal, match, notEqual, ok, throws } from 'node:assert'1+import { equal, match, notEqual, ok } from 'node:assert'2 import { after, before, describe, test } from 'node:test'3 4 import * as browser from '../index.browser.js'5 import * as node from '../index.js'172 unmodified lines178 ok(byte >= 0)179 }180 })181 182+ test(`${type} / customAlphabet / does not fall in infinite loop`, () => {183+ equal(nanoid(0), '')184+ equal(customAlphabet('abc', 0)(0), '')185+ })186+187 if (type === 'node') {188 test(`${type} / proxy number / prevent collision`, () => {189 let makeProxyNumberToReproducePreviousID = () => {190 let step = 023 unmodified lines214 let id1 = nanoid()215 let id2 = nanoid(makeProxyNumberToReproducePreviousID())216 notEqual(id1, id2)217 })213-214- test(`${type} / customAlphabet / does not fall in infinite loop`, () => {215- let generateId = customAlphabet('abc', 0)216- throws(() => {217- generateId()218- }, /ID size is 0/)219- })218 }219 })220 }modified:index.js+1 added-4 removed
49 unmodified lines5051525354555657545556575829 unmodified lines49 unmodified lines // according to benchmarks). let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
return (size = defaultSize) => { if (size === 0) { throw new Error('ID size is 0') }
if (!size) return '' let id = '' while (true) { let bytes = getRandom(step) // A compact alternative for `for (let i = 0; i < step; i++)`.29 unmodified lines