nanoid 1.3.2 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,22 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 2.0.1
5
+ * Reduce npm package size.
6
+ * Mark package as not having side effects (by @xiaody).
7
+
8
+ ## 2.0
9
+ * Use `-` instead of `~` in default alphabet to by file name safe.
10
+ * Add `nanoid/non-secure/generate`.
11
+
12
+ ## 1.3.4
13
+ * Reduce `non-secure` size.
14
+ * Add `async` callback type check.
15
+
16
+ ## 1.3.3
17
+ * Fix `nanoid/async` performance regression.
18
+ * Fix old Node.js `not seeded` issue in synchronous version too.
19
+
4
20
  ## 1.3.2
5
21
  * Fix random generator `not seeded` issue of old Node.js.
6
22
 
package/README.md CHANGED
@@ -5,17 +5,17 @@
5
5
 
6
6
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
7
7
 
8
- * **Small.** 142 bytes (minified and gzipped). No dependencies.
8
+ * **Small.** 141 bytes (minified and gzipped). No dependencies.
9
9
  It uses [Size Limit] to control size.
10
10
  * **Safe.** It uses cryptographically strong random APIs
11
11
  and tests distribution of symbols.
12
12
  * **Fast.** It’s 16% faster than UUID.
13
- * **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_~`).
13
+ * **Compact.** It uses a larger alphabet than UUID (`A-Za-z0-9_-`).
14
14
  So ID size was reduced from 36 to 21 symbols.
15
15
 
16
16
  ```js
17
17
  var nanoid = require('nanoid')
18
- model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B~myT"
18
+ model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
19
19
  ```
20
20
 
21
21
  The generator supports Node.js, React Native, and [all browsers].
@@ -72,27 +72,27 @@ There are two main differences between Nano ID and UUID v4:
72
72
  1. Nano ID uses a bigger alphabet, so a similar number of random bits
73
73
  are packed in just 21 symbols instead of 36.
74
74
  2. Nano ID code is 3 times less than `uuid/v4` package:
75
- 142 bytes instead of 435.
75
+ 141 bytes instead of 435.
76
76
 
77
77
 
78
78
  ## Benchmark
79
79
 
80
80
  ```rust
81
81
  $ ./test/benchmark
82
- nanoid 413,579 ops/sec
83
- nanoid/generate 401,349 ops/sec
84
- uid.sync 354,882 ops/sec
85
- uuid/v4 353,836 ops/sec
86
- shortid 39,152 ops/sec
82
+ nanoid 337,879 ops/sec
83
+ nanoid/generate 335,929 ops/sec
84
+ uid.sync 275,315 ops/sec
85
+ uuid/v4 273,602 ops/sec
86
+ shortid 32,798 ops/sec
87
87
 
88
88
  Async:
89
- nanoid/async 85,168 ops/sec
90
- nanoid/async/generate 81,037 ops/sec
91
- uid 78,426 ops/sec
89
+ nanoid/async 73,782 ops/sec
90
+ nanoid/async/generate 80,775 ops/sec
91
+ uid 67,419 ops/sec
92
92
 
93
93
  Non-secure:
94
- nanoid/non-secure 2,718,186 ops/sec
95
- rndm 2,544,612 ops/sec
94
+ nanoid/non-secure 2,745,340 ops/sec
95
+ rndm 2,611,595 ops/sec
96
96
  ```
97
97
 
98
98
 
@@ -100,12 +100,12 @@ rndm 2,544,612 ops/sec
100
100
 
101
101
  ### Normal
102
102
 
103
- The main module uses URL-friendly symbols (`A-Za-z0-9_~`) and returns an ID
103
+ The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID
104
104
  with 21 characters (to have a collision probability similar to UUID v4).
105
105
 
106
106
  ```js
107
107
  const nanoid = require('nanoid')
108
- model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"
108
+ model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
109
109
  ```
110
110
 
111
111
  Symbols `-,.()` are not encoded in the URL. If used at the end of a link
@@ -115,7 +115,7 @@ If you want to reduce ID length (and increase collisions probability),
115
115
  you can pass the length as an argument.
116
116
 
117
117
  ```js
118
- nanoid(10) //=> "IRFa~VaY2b"
118
+ nanoid(10) //=> "IRFa-VaY2b"
119
119
  ```
120
120
 
121
121
  Don’t forget to check the safety of your ID length
@@ -123,6 +123,29 @@ in our [ID collision probability] calculator.
123
123
 
124
124
  [ID collision probability]: https://zelark.github.io/nano-id-cc/
125
125
 
126
+ ### React
127
+ **Do not** use a nanoid for `key` prop. In React `key` should be consistence
128
+ between renders. This is bad code:
129
+
130
+ ```jsx
131
+ <Item key={nanoid()} /> /* DON’T DO IT */
132
+ ```
133
+
134
+ This is good code. Note, that we added `"input"` string in front of `id`,
135
+ because Nano ID could be started from number. HTML ID can’t be started
136
+ from the number.
137
+
138
+ ```jsx
139
+ id = 'input' + nanoid()
140
+
141
+ render () {
142
+ return <>
143
+ <label htmlFor={this.id}>Label text</label>
144
+ <input id={this.id} type="text"/>
145
+ </>;
146
+ }
147
+ }
148
+ ```
126
149
 
127
150
  ### React Native
128
151
 
@@ -154,7 +177,7 @@ you can use non‑secure ID generator.
154
177
 
155
178
  ```js
156
179
  const nanoid = require('nanoid/non-secure')
157
- model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"
180
+ model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
158
181
  ```
159
182
 
160
183
 
@@ -195,7 +218,7 @@ You can find popular alphabets in [`nanoid-dictionary`].
195
218
  Alphabet must contain 256 symbols or less.
196
219
  Otherwise, the generator will not be secure.
197
220
 
198
- Asynchronous API is also available:
221
+ Asynchronous and non-secure API is also available:
199
222
 
200
223
  ```js
201
224
  const generate = require('nanoid/async/generate')
@@ -204,6 +227,12 @@ async function createUser () {
204
227
  }
205
228
  ```
206
229
 
230
+ ```js
231
+ const generate = require('nanoid/non-secure/generate')
232
+
233
+ user.id = generate('1234567890abcdef', 10)
234
+ ```
235
+
207
236
  [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/
208
237
  [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
209
238
 
@@ -279,7 +308,7 @@ async function createUser () {
279
308
  * [Java](https://github.com/aventrix/jnanoid)
280
309
  * [Nim](https://github.com/icyphox/nanoid.nim)
281
310
  * [PHP](https://github.com/hidehalo/nanoid-php)
282
- * [Python](https://github.com/puyuan/py-nanoid)
311
+ * [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://github.com/aidarkhanov/py-nanoid-dictionary)
283
312
  * [Ruby](https://github.com/radeno/nanoid.rb)
284
313
  * [Rust](https://github.com/nikolay-govorov/nanoid)
285
314
  * [Swift](https://github.com/antiflasher/NanoID)
@@ -1,10 +1,10 @@
1
1
  var crypto = self.crypto || self.msCrypto
2
2
 
3
3
  /*
4
- * This alphabet uses a-z A-Z 0-9 _~ symbols.
4
+ * This alphabet uses a-z A-Z 0-9 _- symbols.
5
5
  * Symbols order was changed for better gzip compression.
6
6
  */
7
- var url = 'ModuleSymbhasOwnPr0123456789ABCDEFGHIJKLNQRTUVWXYZ_cfgijkpqtvxz~'
7
+ var url = 'Uint8ArModuleSymbhasOw-012345679BCDEFGHIJKLNPQRTVWXYZ_cfgjkpqvxz'
8
8
 
9
9
  module.exports = function (size) {
10
10
  size = size || 21
package/async/index.js CHANGED
@@ -1,34 +1,6 @@
1
- var crypto = require('crypto')
2
-
1
+ var random = require('./random')
3
2
  var url = require('../url')
4
3
 
5
- var random
6
- if (crypto.randomFill) {
7
- random = function (bytes, callback) {
8
- return crypto.randomFill(Buffer.allocUnsafe(bytes), callback)
9
- }
10
- } else {
11
- random = crypto.randomBytes
12
- }
13
-
14
- /* eslint-disable-next-line */
15
- var ATTEMPTS = crypto.randomBytes === crypto.pseudoRandomBytes ? 1 : 3
16
-
17
- function safeRandom (size, attempt, callback) {
18
- random(size, function (err, bytes) {
19
- if (err) {
20
- attempt -= 1
21
- if (attempt === 0) {
22
- callback(err)
23
- } else {
24
- setTimeout(safeRandom.bind(null, size, attempt, callback), 10)
25
- }
26
- } else {
27
- callback(null, bytes)
28
- }
29
- })
30
- }
31
-
32
4
  /**
33
5
  * Generate secure URL-friendly unique ID. Non-blocking version.
34
6
  *
@@ -36,7 +8,6 @@ function safeRandom (size, attempt, callback) {
36
8
  * to UUID v4.
37
9
  *
38
10
  * @param {number} [size=21] The number of symbols in ID.
39
- * @param {function} [callback] for environments without `Promise`.
40
11
  *
41
12
  * @return {Promise} Promise with random string.
42
13
  *
@@ -49,34 +20,13 @@ function safeRandom (size, attempt, callback) {
49
20
  * @name async
50
21
  * @function
51
22
  */
52
- module.exports = function (size, callback) {
23
+ module.exports = function (size) {
53
24
  size = size || 21
54
-
55
- if (!callback) {
56
- if (typeof Promise !== 'function') {
57
- throw new Error('Function callback is required')
58
- }
59
- return new Promise(function (resolve, reject) {
60
- module.exports(size, function (error, id) {
61
- if (error) {
62
- reject(error)
63
- } else {
64
- resolve(id)
65
- }
66
- })
67
- })
68
- }
69
-
70
- safeRandom(size, ATTEMPTS, function (error, bytes) {
71
- if (error) {
72
- return callback(error)
73
- }
74
-
25
+ return random(size).then(function (bytes) {
75
26
  var id = ''
76
27
  while (0 < size--) {
77
28
  id += url[bytes[size] & 63]
78
29
  }
79
-
80
- callback(null, id)
30
+ return id
81
31
  })
82
32
  }
package/index.browser.js CHANGED
@@ -10,10 +10,10 @@ if (process.env.NODE_ENV !== 'production') {
10
10
  var crypto = self.crypto || self.msCrypto
11
11
 
12
12
  /*
13
- * This alphabet uses a-z A-Z 0-9 _~ symbols.
13
+ * This alphabet uses a-z A-Z 0-9 _- symbols.
14
14
  * Symbols order was changed for better gzip compression.
15
15
  */
16
- var url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz~'
16
+ var url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz-'
17
17
 
18
18
  module.exports = function (size) {
19
19
  size = size || 21
package/index.js CHANGED
@@ -13,15 +13,15 @@ var url = require('./url')
13
13
  *
14
14
  * @example
15
15
  * const nanoid = require('nanoid')
16
- * model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqL"
16
+ * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
17
17
  *
18
18
  * @name nanoid
19
19
  * @function
20
20
  */
21
21
  module.exports = function (size) {
22
22
  size = size || 21
23
- var id = ''
24
23
  var bytes = random(size)
24
+ var id = ''
25
25
  while (0 < size--) {
26
26
  id += url[bytes[size] & 63]
27
27
  }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Generate URL-friendly unique ID. This method use non-secure predictable
3
+ * random generator.
4
+ *
5
+ * By default, ID will have 21 symbols to have a collision probability similar
6
+ * to UUID v4.
7
+ *
8
+ * @param {string} alphabet Symbols to be used in ID.
9
+ * @param {number} [size=21] The number of symbols in ID.
10
+ *
11
+ * @return {string} Random string.
12
+ *
13
+ * @example
14
+ * const nanoid = require('nanoid/non-secure')
15
+ * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
16
+ *
17
+ * @name nonSecure
18
+ * @function
19
+ */
20
+ module.exports = function (alphabet, size) {
21
+ size = size || 21
22
+ var id = ''
23
+ while (0 < size--) {
24
+ id += alphabet[Math.random() * alphabet.length | 0]
25
+ }
26
+ return id
27
+ }
@@ -1,4 +1,4 @@
1
- var url = '_~varfunctio0125634789bdegjhklmpqswxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
1
+ var url = 'bjectSymhasOwnProp-0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgiklquvxz'
2
2
 
3
3
  /**
4
4
  * Generate URL-friendly unique ID. This method use non-secure predictable
@@ -13,7 +13,7 @@ var url = '_~varfunctio0125634789bdegjhklmpqswxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
13
13
  *
14
14
  * @example
15
15
  * const nanoid = require('nanoid/non-secure')
16
- * model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqL"
16
+ * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
17
17
  *
18
18
  * @name nonSecure
19
19
  * @function
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "1.3.2",
4
- "description": "A tiny (143 bytes), secure URL-friendly unique string ID generator",
3
+ "version": "2.0.1",
4
+ "description": "A tiny (141 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "random",
@@ -16,5 +16,11 @@
16
16
  "./random.js": "./random.browser.js",
17
17
  "./async/index.js": "./async/index.browser.js",
18
18
  "./async/random.js": "./async/random.browser.js"
19
+ },
20
+ "sideEffects": false,
21
+ "husky": {
22
+ "hooks": {
23
+ "pre-commit": "lint-staged"
24
+ }
19
25
  }
20
26
  }
package/url.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * URL safe symbols.
3
3
  *
4
- * This alphabet uses a-z A-Z 0-9 _~ symbols.
4
+ * This alphabet uses a-z A-Z 0-9 _- symbols.
5
5
  * Symbols order was changed for better gzip compression.
6
6
  *
7
7
  * @name url
@@ -12,4 +12,4 @@
12
12
  * generate(url, 10) //=> "Uakgb_J5m9"
13
13
  */
14
14
  module.exports =
15
- 'SymboljecthasOwnPr0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgikpquvxz~'
15
+ 'ModuleSymbhasOwnPr-0123456789ABCDEFGHIJKLNQRTUVWXYZ_cfgijkpqtvxz'