nanoid 5.0.8 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,7 +10,7 @@ A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
10
10
  > “An amazing level of senseless perfectionism,
11
11
  > which is simply impossible not to respect.”
12
12
 
13
- * **Small.** 116 bytes (minified and brotlied). No dependencies.
13
+ * **Small.** 118 bytes (minified and brotlied). No dependencies.
14
14
  [Size Limit] controls the size.
15
15
  * **Safe.** It uses hardware random generator. Can be used in clusters.
16
16
  * **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
package/index.browser.js CHANGED
@@ -8,19 +8,19 @@ export let customRandom = (alphabet, defaultSize, getRandom) => {
8
8
  let id = ''
9
9
  while (true) {
10
10
  let bytes = getRandom(step)
11
- let j = step
11
+ let j = step | 0
12
12
  while (j--) {
13
13
  id += alphabet[bytes[j] & mask] || ''
14
- if (id.length === size) return id
14
+ if (id.length >= size) return id
15
15
  }
16
16
  }
17
17
  }
18
18
  }
19
19
  export let customAlphabet = (alphabet, size = 21) =>
20
- customRandom(alphabet, size, random)
20
+ customRandom(alphabet, size | 0, random)
21
21
  export let nanoid = (size = 21) => {
22
22
  let id = ''
23
- let bytes = crypto.getRandomValues(new Uint8Array(size))
23
+ let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
24
24
  while (size--) {
25
25
  id += scopedUrlAlphabet[bytes[size] & 63]
26
26
  }
package/index.d.ts CHANGED
@@ -10,9 +10,10 @@
10
10
  * ```
11
11
  *
12
12
  * @param size Size of the ID. The default size is 21.
13
+ * @typeparam Type The type of the generated ID.
13
14
  * @returns A random string.
14
15
  */
15
- export function nanoid(size?: number): string
16
+ export function nanoid<Type extends string>(size?: number): Type
16
17
 
17
18
  /**
18
19
  * Generate secure unique ID with custom alphabet.
@@ -22,6 +23,7 @@ export function nanoid(size?: number): string
22
23
  *
23
24
  * @param alphabet Alphabet used to generate the ID.
24
25
  * @param defaultSize Size of the ID. The default size is 21.
26
+ * @typeparam Type The type of the generated ID.
25
27
  * @returns A random string generator.
26
28
  *
27
29
  * ```js
@@ -30,10 +32,10 @@ export function nanoid(size?: number): string
30
32
  * nanoid() //=> "8ё56а"
31
33
  * ```
32
34
  */
33
- export function customAlphabet(
35
+ export function customAlphabet<Type extends string>(
34
36
  alphabet: string,
35
37
  defaultSize?: number
36
- ): (size?: number) => string
38
+ ): (size?: number) => Type
37
39
 
38
40
  /**
39
41
  * Generate unique ID with custom random generator and alphabet.
@@ -58,13 +60,14 @@ export function customAlphabet(
58
60
  * @param alphabet Alphabet used to generate a random string.
59
61
  * @param size Size of the random string.
60
62
  * @param random A random bytes generator.
63
+ * @typeparam Type The type of the generated ID.
61
64
  * @returns A random string generator.
62
65
  */
63
- export function customRandom(
66
+ export function customRandom<Type extends string>(
64
67
  alphabet: string,
65
68
  size: number,
66
69
  random: (bytes: number) => Uint8Array
67
- ): () => string
70
+ ): () => Type
68
71
 
69
72
  /**
70
73
  * URL safe symbols.
package/index.js CHANGED
@@ -15,7 +15,7 @@ function fillPool(bytes) {
15
15
  poolOffset += bytes
16
16
  }
17
17
  export function random(bytes) {
18
- fillPool((bytes -= 0))
18
+ fillPool((bytes |= 0))
19
19
  return pool.subarray(poolOffset - bytes, poolOffset)
20
20
  }
21
21
  export function customRandom(alphabet, defaultSize, getRandom) {
@@ -28,7 +28,7 @@ export function customRandom(alphabet, defaultSize, getRandom) {
28
28
  let i = step
29
29
  while (i--) {
30
30
  id += alphabet[bytes[i] & mask] || ''
31
- if (id.length === size) return id
31
+ if (id.length >= size) return id
32
32
  }
33
33
  }
34
34
  }
@@ -37,7 +37,7 @@ export function customAlphabet(alphabet, size = 21) {
37
37
  return customRandom(alphabet, size, random)
38
38
  }
39
39
  export function nanoid(size = 21) {
40
- fillPool((size -= 0))
40
+ fillPool((size |= 0))
41
41
  let id = ''
42
42
  for (let i = poolOffset - size; i < poolOffset; i++) {
43
43
  id += scopedUrlAlphabet[pool[i] & 63]
@@ -3,7 +3,7 @@ let urlAlphabet =
3
3
  export let customAlphabet = (alphabet, defaultSize = 21) => {
4
4
  return (size = defaultSize) => {
5
5
  let id = ''
6
- let i = size
6
+ let i = size | 0
7
7
  while (i--) {
8
8
  id += alphabet[(Math.random() * alphabet.length) | 0]
9
9
  }
@@ -12,7 +12,7 @@ export let customAlphabet = (alphabet, defaultSize = 21) => {
12
12
  }
13
13
  export let nanoid = (size = 21) => {
14
14
  let id = ''
15
- let i = size
15
+ let i = size | 0
16
16
  while (i--) {
17
17
  id += urlAlphabet[(Math.random() * 64) | 0]
18
18
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "5.0.8",
4
- "description": "A tiny (116 bytes), secure URL-friendly unique string ID generator",
3
+ "version": "5.1.0",
4
+ "description": "A tiny (118 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "random",