nanoid 3.1.29 → 3.2.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
@@ -3,21 +3,21 @@
3
3
  <img src="https://ai.github.io/nanoid/logo.svg" align="right"
4
4
  alt="Nano ID logo by Anton Lovchikov" width="180" height="94">
5
5
 
6
- **English** | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md)
6
+ **English** | [Русский](./README.ru.md) | [简体中文](./README.zh-CN.md) | [Bahasa Indonesia](./README.id-ID.md)
7
7
 
8
8
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
9
9
 
10
10
  > “An amazing level of senseless perfectionism,
11
11
  > which is simply impossible not to respect.”
12
12
 
13
- * **Small.** 108 bytes (minified and gzipped). No dependencies.
13
+ * **Small.** 130 bytes (minified and gzipped). No dependencies.
14
14
  [Size Limit] controls the size.
15
15
  * **Fast.** It is 2 times faster than UUID.
16
16
  * **Safe.** It uses hardware random generator. Can be used in clusters.
17
17
  * **Short IDs.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
18
18
  So ID size was reduced from 36 to 21 symbols.
19
19
  * **Portable.** Nano ID was ported
20
- to [19 programming languages](#other-programming-languages).
20
+ to [20 programming languages](#other-programming-languages).
21
21
 
22
22
  ```js
23
23
  import { nanoid } from 'nanoid'
@@ -1,21 +1,20 @@
1
- let random = bytes =>
2
- Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
1
+ let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
3
2
  let customAlphabet = (alphabet, size) => {
4
3
  let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
5
4
  let step = -~((1.6 * mask * size) / alphabet.length)
6
- return () => {
5
+ return async () => {
7
6
  let id = ''
8
7
  while (true) {
9
8
  let bytes = crypto.getRandomValues(new Uint8Array(step))
10
9
  let i = step
11
10
  while (i--) {
12
11
  id += alphabet[bytes[i] & mask] || ''
13
- if (id.length === size) return Promise.resolve(id)
12
+ if (id.length === size) return id
14
13
  }
15
14
  }
16
15
  }
17
16
  }
18
- let nanoid = (size = 21) => {
17
+ let nanoid = async (size = 21) => {
19
18
  let id = ''
20
19
  let bytes = crypto.getRandomValues(new Uint8Array(size))
21
20
  while (size--) {
@@ -30,6 +29,6 @@ let nanoid = (size = 21) => {
30
29
  id += '-'
31
30
  }
32
31
  }
33
- return Promise.resolve(id)
32
+ return id
34
33
  }
35
34
  module.exports = { nanoid, customAlphabet, random }
@@ -1,21 +1,20 @@
1
- let random = bytes =>
2
- Promise.resolve(crypto.getRandomValues(new Uint8Array(bytes)))
1
+ let random = async bytes => crypto.getRandomValues(new Uint8Array(bytes))
3
2
  let customAlphabet = (alphabet, size) => {
4
3
  let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
5
4
  let step = -~((1.6 * mask * size) / alphabet.length)
6
- return () => {
5
+ return async () => {
7
6
  let id = ''
8
7
  while (true) {
9
8
  let bytes = crypto.getRandomValues(new Uint8Array(step))
10
9
  let i = step
11
10
  while (i--) {
12
11
  id += alphabet[bytes[i] & mask] || ''
13
- if (id.length === size) return Promise.resolve(id)
12
+ if (id.length === size) return id
14
13
  }
15
14
  }
16
15
  }
17
16
  }
18
- let nanoid = (size = 21) => {
17
+ let nanoid = async (size = 21) => {
19
18
  let id = ''
20
19
  let bytes = crypto.getRandomValues(new Uint8Array(size))
21
20
  while (size--) {
@@ -30,6 +29,6 @@ let nanoid = (size = 21) => {
30
29
  id += '-'
31
30
  }
32
31
  }
33
- return Promise.resolve(id)
32
+ return id
34
33
  }
35
34
  export { nanoid, customAlphabet, random }
package/bin/nanoid.cjs CHANGED
@@ -1,5 +1,58 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- let { nanoid } = require('..')
3
+ let { nanoid, customAlphabet } = require('..')
4
4
 
5
- process.stdout.write(nanoid() + '\n')
5
+ function print(msg) {
6
+ process.stdout.write(msg + '\n')
7
+ }
8
+
9
+ function error(msg) {
10
+ process.stderr.write(msg + '\n')
11
+ process.exit(1)
12
+ }
13
+
14
+ if (process.argv.includes('--help') || process.argv.includes('-h')) {
15
+ print(`
16
+ Usage
17
+ $ nanoid [options]
18
+
19
+ Options
20
+ -s, --size Generated ID size
21
+ -a, --alphabet Alphabet to use
22
+ -h, --help Show this help
23
+
24
+ Examples
25
+ $ nano --s 15
26
+ S9sBF77U6sDB8Yg
27
+
28
+ $ nano --size 10 --alphabet abc
29
+ bcabababca`)
30
+ process.exit()
31
+ }
32
+
33
+ let alphabet, size
34
+ for (let i = 2; i < process.argv.length; i++) {
35
+ let arg = process.argv[i]
36
+ if (arg === '--size' || arg === '-s') {
37
+ size = Number(process.argv[i + 1])
38
+ i += 1
39
+ if (Number.isNaN(size) || size <= 0) {
40
+ error('Size must be positive integer')
41
+ }
42
+ } else if (arg === '--alphabet' || arg === '-a') {
43
+ alphabet = process.argv[i + 1]
44
+ i += 1
45
+ } else {
46
+ error('Unknown argument ' + arg)
47
+ }
48
+ }
49
+
50
+ if (alphabet) {
51
+ if (typeof size === 'undefined') {
52
+ error('You must also specify size option, when using custom alphabet')
53
+ }
54
+ let customNanoid = customAlphabet(alphabet, size)
55
+ print(customNanoid())
56
+ } else {
57
+ print(nanoid(size))
58
+ }
package/index.cjs CHANGED
@@ -14,7 +14,7 @@ let fillPool = bytes => {
14
14
  poolOffset += bytes
15
15
  }
16
16
  let random = bytes => {
17
- fillPool(bytes)
17
+ fillPool((bytes -= 0))
18
18
  return pool.subarray(poolOffset - bytes, poolOffset)
19
19
  }
20
20
  let customRandom = (alphabet, size, getRandom) => {
@@ -34,7 +34,7 @@ let customRandom = (alphabet, size, getRandom) => {
34
34
  }
35
35
  let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
36
36
  let nanoid = (size = 21) => {
37
- fillPool(size)
37
+ fillPool((size -= 0))
38
38
  let id = ''
39
39
  for (let i = poolOffset - size; i < poolOffset; i++) {
40
40
  id += urlAlphabet[pool[i] & 63]
package/index.js CHANGED
@@ -14,7 +14,7 @@ let fillPool = bytes => {
14
14
  poolOffset += bytes
15
15
  }
16
16
  let random = bytes => {
17
- fillPool(bytes)
17
+ fillPool((bytes -= 0))
18
18
  return pool.subarray(poolOffset - bytes, poolOffset)
19
19
  }
20
20
  let customRandom = (alphabet, size, getRandom) => {
@@ -34,7 +34,7 @@ let customRandom = (alphabet, size, getRandom) => {
34
34
  }
35
35
  let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
36
36
  let nanoid = (size = 21) => {
37
- fillPool(size)
37
+ fillPool((size -= 0))
38
38
  let id = ''
39
39
  for (let i = poolOffset - size; i < poolOffset; i++) {
40
40
  id += urlAlphabet[pool[i] & 63]
@@ -1,5 +1,5 @@
1
1
  let urlAlphabet =
2
- 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
3
  let customAlphabet = (alphabet, size) => {
4
4
  return () => {
5
5
  let id = ''
@@ -1,5 +1,5 @@
1
1
  let urlAlphabet =
2
- 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
3
  let customAlphabet = (alphabet, size) => {
4
4
  return () => {
5
5
  let id = ''
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "3.1.29",
4
- "description": "A tiny (108 bytes), secure URL-friendly unique string ID generator",
3
+ "version": "3.2.0",
4
+ "description": "A tiny (130 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "random",
@@ -1,3 +1,3 @@
1
1
  let urlAlphabet =
2
- 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
3
  module.exports = { urlAlphabet }
@@ -1,3 +1,3 @@
1
1
  let urlAlphabet =
2
- 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
2
+ 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
3
3
  export { urlAlphabet }