nanoid 2.0.0 → 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,10 @@
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
+
4
8
  ## 2.0
5
9
  * Use `-` instead of `~` in default alphabet to by file name safe.
6
10
  * Add `nanoid/non-secure/generate`.
package/README.md CHANGED
@@ -79,20 +79,20 @@ There are two main differences between Nano ID and UUID v4:
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
 
@@ -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
 
@@ -285,7 +308,7 @@ async function createUser () {
285
308
  * [Java](https://github.com/aventrix/jnanoid)
286
309
  * [Nim](https://github.com/icyphox/nanoid.nim)
287
310
  * [PHP](https://github.com/hidehalo/nanoid-php)
288
- * [Python](https://github.com/puyuan/py-nanoid)
311
+ * [Python](https://github.com/puyuan/py-nanoid) with [dictionaries](https://github.com/aidarkhanov/py-nanoid-dictionary)
289
312
  * [Ruby](https://github.com/radeno/nanoid.rb)
290
313
  * [Rust](https://github.com/nikolay-govorov/nanoid)
291
314
  * [Swift](https://github.com/antiflasher/NanoID)
package/async/index.js CHANGED
@@ -1,16 +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
4
  /**
15
5
  * Generate secure URL-friendly unique ID. Non-blocking version.
16
6
  *
@@ -18,7 +8,6 @@ if (crypto.randomFill) {
18
8
  * to UUID v4.
19
9
  *
20
10
  * @param {number} [size=21] The number of symbols in ID.
21
- * @param {function} [callback] for environments without `Promise`.
22
11
  *
23
12
  * @return {Promise} Promise with random string.
24
13
  *
@@ -31,41 +20,13 @@ if (crypto.randomFill) {
31
20
  * @name async
32
21
  * @function
33
22
  */
34
- module.exports = function (size, callback, attempt) {
23
+ module.exports = function (size) {
35
24
  size = size || 21
36
-
37
- if (!callback) {
38
- if (typeof Promise !== 'function') {
39
- throw new TypeError('Function callback is required')
40
- }
41
- return new Promise(function (resolve, reject) {
42
- module.exports(size, function (error, id) {
43
- if (error) {
44
- reject(error)
45
- } else {
46
- resolve(id)
47
- }
48
- })
49
- })
50
- } else if (typeof callback !== 'function') {
51
- throw new TypeError('Callback is not a function')
52
- }
53
-
54
- random(size, function (err, bytes) {
55
- if (err) {
56
- if (typeof attempt === 'undefined') attempt = 3
57
- attempt -= 1
58
- if (attempt === 0) {
59
- callback(err)
60
- } else {
61
- setTimeout(module.exports.bind(null, size, callback, attempt), 10)
62
- }
63
- } else {
64
- var id = ''
65
- while (0 < size--) {
66
- id += url[bytes[size] & 63]
67
- }
68
- callback(null, id)
25
+ return random(size).then(function (bytes) {
26
+ var id = ''
27
+ while (0 < size--) {
28
+ id += url[bytes[size] & 63]
69
29
  }
30
+ return id
70
31
  })
71
32
  }
package/index.js CHANGED
@@ -18,26 +18,12 @@ var url = require('./url')
18
18
  * @name nanoid
19
19
  * @function
20
20
  */
21
- module.exports = function (size, attempt) {
21
+ module.exports = function (size) {
22
22
  size = size || 21
23
-
24
- var bytes
25
- try {
26
- bytes = random(size)
27
- } catch (e) {
28
- if (typeof attempt === 'undefined') attempt = 3
29
- attempt -= 1
30
- if (attempt === 0) {
31
- throw e
32
- } else {
33
- return module.exports(size, attempt)
34
- }
35
- }
36
-
23
+ var bytes = random(size)
37
24
  var id = ''
38
25
  while (0 < size--) {
39
26
  id += url[bytes[size] & 63]
40
27
  }
41
-
42
28
  return id
43
29
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "A tiny (141 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
@@ -17,6 +17,7 @@
17
17
  "./async/index.js": "./async/index.browser.js",
18
18
  "./async/random.js": "./async/random.browser.js"
19
19
  },
20
+ "sideEffects": false,
20
21
  "husky": {
21
22
  "hooks": {
22
23
  "pre-commit": "lint-staged"