nanoid 1.3.1 → 2.0.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/CHANGELOG.md CHANGED
@@ -1,6 +1,21 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 2.0
5
+ * Use `-` instead of `~` in default alphabet to by file name safe.
6
+ * Add `nanoid/non-secure/generate`.
7
+
8
+ ## 1.3.4
9
+ * Reduce `non-secure` size.
10
+ * Add `async` callback type check.
11
+
12
+ ## 1.3.3
13
+ * Fix `nanoid/async` performance regression.
14
+ * Fix old Node.js `not seeded` issue in synchronous version too.
15
+
16
+ ## 1.3.2
17
+ * Fix random generator `not seeded` issue of old Node.js.
18
+
4
19
  ## 1.3.1
5
20
  * Reduce library size.
6
21
 
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,7 +72,7 @@ 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
@@ -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
@@ -154,7 +154,7 @@ you can use non‑secure ID generator.
154
154
 
155
155
  ```js
156
156
  const nanoid = require('nanoid/non-secure')
157
- model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"
157
+ model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
158
158
  ```
159
159
 
160
160
 
@@ -195,7 +195,7 @@ You can find popular alphabets in [`nanoid-dictionary`].
195
195
  Alphabet must contain 256 symbols or less.
196
196
  Otherwise, the generator will not be secure.
197
197
 
198
- Asynchronous API is also available:
198
+ Asynchronous and non-secure API is also available:
199
199
 
200
200
  ```js
201
201
  const generate = require('nanoid/async/generate')
@@ -204,6 +204,12 @@ async function createUser () {
204
204
  }
205
205
  ```
206
206
 
207
+ ```js
208
+ const generate = require('nanoid/non-secure/generate')
209
+
210
+ user.id = generate('1234567890abcdef', 10)
211
+ ```
212
+
207
213
  [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/
208
214
  [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
209
215
 
@@ -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
@@ -31,12 +31,12 @@ if (crypto.randomFill) {
31
31
  * @name async
32
32
  * @function
33
33
  */
34
- module.exports = function (size, callback) {
34
+ module.exports = function (size, callback, attempt) {
35
35
  size = size || 21
36
36
 
37
37
  if (!callback) {
38
38
  if (typeof Promise !== 'function') {
39
- throw new Error('Function callback is required')
39
+ throw new TypeError('Function callback is required')
40
40
  }
41
41
  return new Promise(function (resolve, reject) {
42
42
  module.exports(size, function (error, id) {
@@ -47,18 +47,25 @@ module.exports = function (size, callback) {
47
47
  }
48
48
  })
49
49
  })
50
+ } else if (typeof callback !== 'function') {
51
+ throw new TypeError('Callback is not a function')
50
52
  }
51
53
 
52
- random(size, function (error, bytes) {
53
- if (error) {
54
- return callback(error)
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)
55
69
  }
56
-
57
- var id = ''
58
- while (0 < size--) {
59
- id += url[bytes[size] & 63]
60
- }
61
-
62
- callback(null, id)
63
70
  })
64
71
  }
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,17 +13,31 @@ 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
- module.exports = function (size) {
21
+ module.exports = function (size, attempt) {
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
37
  var id = ''
24
- var bytes = random(size)
25
38
  while (0 < size--) {
26
39
  id += url[bytes[size] & 63]
27
40
  }
41
+
28
42
  return id
29
43
  }
@@ -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.1",
4
- "description": "A tiny (143 bytes), secure URL-friendly unique string ID generator",
3
+ "version": "2.0.0",
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,10 @@
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
+ "husky": {
21
+ "hooks": {
22
+ "pre-commit": "lint-staged"
23
+ }
19
24
  }
20
25
  }
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'