Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional fixes and tests for #508 #509

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export let customRandom = (alphabet, defaultSize, getRandom) => {
while (j--) {
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
id += alphabet[bytes[j] & mask] || ''
if (id.length === size) return id
if (id.length >= size) return id
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function customRandom(alphabet, defaultSize, getRandom) {
while (i--) {
// Adding `|| ''` refuses a random byte that exceeds the alphabet size.
id += alphabet[bytes[i] & mask] || ''
if (id.length === size) return id
if (id.length >= size) return id
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ for (let type of ['node', 'browser']) {
equal(nanoidA(10), 'aaaaaaaaaa')
})

test(`${type} / customAlphabet / avoids pool pollution, infinite loop`, () => {
let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
let nanoid2 = customAlphabet(ALPHABET)
nanoid2(2.1)
let second = nanoid2()
let third = nanoid2()
notEqual(second, third)
})

test(`${type} / customRandom / supports generator`, () => {
let sequence = [2, 255, 3, 7, 7, 7, 7, 7, 0, 1]
function fakeRandom(size) {
Expand Down
18 changes: 17 additions & 1 deletion test/non-secure.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { equal, match, ok } from 'node:assert'
import { equal, match, notEqual, ok } from 'node:assert'
import { describe, test } from 'node:test'

import { urlAlphabet } from '../index.js'
Expand Down Expand Up @@ -57,6 +57,13 @@ describe('non secure', () => {
ok(max - min <= 0.05)
})

test('nanoid / avoids pool pollution, infinite loop', () => {
nanoid(2.1)
let second = nanoid()
let third = nanoid()
notEqual(second, third)
})

test('customAlphabet / has options', () => {
let nanoidA = customAlphabet('a', 5)
equal(nanoidA(), 'aaaaa')
Expand Down Expand Up @@ -88,4 +95,13 @@ describe('non secure', () => {
}
ok(max - min <= 0.05)
})

test('customAlphabet / avoids pool pollution, infinite loop', () => {
let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
let nanoid2 = customAlphabet(ALPHABET)
nanoid2(2.1)
let second = nanoid2()
let third = nanoid2()
notEqual(second, third)
})
})