nanoid 2.1.10 → 2.1.11

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,9 @@
1
1
  # Change Log
2
2
  This project adheres to [Semantic Versioning](http://semver.org/).
3
3
 
4
+ ## 2.1.11
5
+ * Reduce size (by Anton Evzhakov).
6
+
4
7
  ## 2.1.10
5
8
  * Reduce size by 10% (by Anton Khlynovskiy).
6
9
 
package/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
7
7
 
8
- * **Small.** 127 bytes (minified and gzipped). No dependencies.
8
+ * **Small.** 119 bytes (minified and gzipped). No dependencies.
9
9
  [Size Limit] controls the size.
10
10
  * **Safe.** It uses cryptographically strong random APIs.
11
11
  Can be used in clusters.
@@ -63,7 +63,7 @@ There are three main differences between Nano ID and UUID v4:
63
63
  1. Nano ID uses a bigger alphabet, so a similar number of random bits
64
64
  are packed in just 21 symbols instead of 36.
65
65
  2. Nano ID code is 4 times less than `uuid/v4` package:
66
- 127 bytes instead of 435.
66
+ 119 bytes instead of 435.
67
67
  3. Because of memory allocation tricks, Nano ID is 16% faster than UUID.
68
68
 
69
69
 
@@ -4,39 +4,34 @@
4
4
  var crypto = self.crypto || self.msCrypto
5
5
 
6
6
  // This alphabet uses a-z A-Z 0-9 _- symbols.
7
- // Despite the fact the source code is quite long, its entropy
8
- // is low and there are lots of duplicates - just what compressors
9
- // like GZIP and Brotli likes the best.
10
- var i
11
- var url = '_-' + String.fromCharCode(
12
- // ASCII codes for 0...9
13
- i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
14
- i += 1, i += 1,
15
-
16
- // ASCII codes for A...Z
17
- i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
18
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
19
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
20
- i += 1, i += 1,
21
-
22
- // ASCII codes for a...z
23
- i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
24
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
25
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
26
- i += 1, i += 1
27
- )
7
+ // Symbols are generated for smaller size.
8
+ // -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
9
+ var url = '-_'
10
+ // Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
11
+ var i = 36
12
+ while (i--) {
13
+ // 36 is radix. Number.prototype.toString(36) returns number
14
+ // in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
15
+ url += i.toString(36)
16
+ }
17
+ // Loop from 36 to 10 (from Z to A in Base36).
18
+ i = 36
19
+ while (i-- - 10) {
20
+ url += i.toString(36).toUpperCase()
21
+ }
28
22
 
29
23
  module.exports = function (size) {
30
- size = size || 21
31
24
  var id = ''
32
- var bytes = crypto.getRandomValues(new Uint8Array(size))
25
+ var bytes = crypto.getRandomValues(new Uint8Array(size || 21))
26
+ i = size || 21
27
+
33
28
  // Compact alternative for `for (var i = 0; i < size; i++)`
34
- while (size--) {
29
+ while (i--) {
35
30
  // We can’t use bytes bigger than the alphabet. 63 is 00111111 bitmask.
36
31
  // This mask reduces random byte 0-255 to 0-63 values.
37
32
  // There is no need in `|| ''` and `* 1.6` hacks in here,
38
33
  // because bitmask trim bytes exact to alphabet size.
39
- id += url[bytes[size] & 63]
34
+ id += url[bytes[i] & 63]
40
35
  }
41
36
  return Promise.resolve(id)
42
37
  }
package/index.browser.js CHANGED
@@ -21,39 +21,34 @@ if (process.env.NODE_ENV !== 'production') {
21
21
  var crypto = self.crypto || self.msCrypto
22
22
 
23
23
  // This alphabet uses a-z A-Z 0-9 _- symbols.
24
- // Despite the fact the source code is quite long, its entropy
25
- // is low and there are lots of duplicates - just what compressors
26
- // like GZIP and Brotli likes the best.
27
- var i
28
- var url = '_-' + String.fromCharCode(
29
- // ASCII codes for 0...9
30
- i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
31
- i += 1, i += 1,
32
-
33
- // ASCII codes for A...Z
34
- i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
35
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
36
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
37
- i += 1, i += 1,
38
-
39
- // ASCII codes for a...z
40
- i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
41
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
42
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
43
- i += 1, i += 1
44
- )
24
+ // Symbols are generated for smaller size.
25
+ // -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
26
+ var url = '-_'
27
+ // Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
28
+ var i = 36
29
+ while (i--) {
30
+ // 36 is radix. Number.prototype.toString(36) returns number
31
+ // in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
32
+ url += i.toString(36)
33
+ }
34
+ // Loop from 36 to 10 (from Z to A in Base36).
35
+ i = 36
36
+ while (i-- - 10) {
37
+ url += i.toString(36).toUpperCase()
38
+ }
45
39
 
46
40
  module.exports = function (size) {
47
- size = size || 21
48
41
  var id = ''
49
- var bytes = crypto.getRandomValues(new Uint8Array(size))
42
+ var bytes = crypto.getRandomValues(new Uint8Array(size || 21))
43
+ i = size || 21
44
+
50
45
  // Compact alternative for `for (var i = 0; i < size; i++)`
51
- while (size--) {
46
+ while (i--) {
52
47
  // We can’t use bytes bigger than the alphabet. 63 is 00111111 bitmask.
53
48
  // This mask reduces random byte 0-255 to 0-63 values.
54
49
  // There is no need in `|| ''` and `* 1.6` hacks in here,
55
50
  // because bitmask trim bytes exact to alphabet size.
56
- id += url[bytes[size] & 63]
51
+ id += url[bytes[i] & 63]
57
52
  }
58
53
  return id
59
54
  }
@@ -1,25 +1,19 @@
1
1
  // This alphabet uses a-z A-Z 0-9 _- symbols.
2
- // Despite the fact the source code is quite long, its entropy
3
- // is low and there are lots of duplicates - just what compressors
4
- // like GZIP and Brotli likes the best.
5
- var i
6
- var url = '_-' + String.fromCharCode(
7
- // ASCII codes for 0...9
8
- i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
9
- i += 1, i += 1,
10
-
11
- // ASCII codes for A...Z
12
- i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
13
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
14
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
15
- i += 1, i += 1,
16
-
17
- // ASCII codes for a...z
18
- i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
19
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
20
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
21
- i += 1, i += 1
22
- )
2
+ // Symbols are generated for smaller size.
3
+ // -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
4
+ var url = '-_'
5
+ // Loop from 36 to 0 (from z to a and 9 to 0 in Base36).
6
+ var i = 36
7
+ while (i--) {
8
+ // 36 is radix. Number.prototype.toString(36) returns number
9
+ // in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
10
+ url += i.toString(36)
11
+ }
12
+ // Loop from 36 to 10 (from Z to A in Base36).
13
+ i = 36
14
+ while (i-- - 10) {
15
+ url += i.toString(36).toUpperCase()
16
+ }
23
17
 
24
18
  /**
25
19
  * Generate URL-friendly unique ID. This method use non-secure predictable
@@ -37,10 +31,10 @@ var url = '_-' + String.fromCharCode(
37
31
  * @function
38
32
  */
39
33
  module.exports = function (size) {
40
- size = size || 21
41
34
  var id = ''
35
+ i = size || 21
42
36
  // Compact alternative for `for (var i = 0; i < size; i++)`
43
- while (size--) {
37
+ while (i--) {
44
38
  // `| 0` is compact and faster alternative for `Math.floor()`
45
39
  id += url[Math.random() * 64 | 0]
46
40
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nanoid",
3
- "version": "2.1.10",
4
- "description": "A tiny (127 bytes), secure URL-friendly unique string ID generator",
3
+ "version": "2.1.11",
4
+ "description": "A tiny (119 bytes), secure URL-friendly unique string ID generator",
5
5
  "keywords": [
6
6
  "uuid",
7
7
  "random",
package/url.js CHANGED
@@ -3,8 +3,6 @@
3
3
  // is low and there are lots of duplicates - just what compressors
4
4
  // like GZIP and Brotli likes the best.
5
5
 
6
- var i
7
-
8
6
  /**
9
7
  * URL safe symbols.
10
8
  *
@@ -15,20 +13,15 @@ var i
15
13
  * const url = require('nanoid/url')
16
14
  * generate(url, 10) //=> "Uakgb_J5m9"
17
15
  */
18
- module.exports = '_-' + String.fromCharCode(
19
- // ASCII codes for 0...9
20
- i = 48, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
21
- i += 1, i += 1,
22
-
23
- // ASCII codes for A...Z
24
- i += 8, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
25
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
26
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
27
- i += 1, i += 1,
28
16
 
29
- // ASCII codes for a...z
30
- i += 7, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
31
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
32
- i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1, i += 1,
33
- i += 1, i += 1
34
- )
17
+ // This alphabet uses a-z A-Z 0-9 _- symbols.
18
+ // Symbols are generated for smaller size.
19
+ // -_zyxwvutsrqponmlkjihgfedcba9876543210ZYXWVUTSRQPONMLKJIHGFEDCBA
20
+ module.exports = '-_'
21
+ var i = 36
22
+ while (i--) {
23
+ // 36 is radix. Number.prototype.toString(36) returns number
24
+ // in Base36 representation. Base36 is like hex, but it uses 0–9 and a-z.
25
+ module.exports += i.toString(36)
26
+ i > 9 && (module.exports += i.toString(36).toUpperCase())
27
+ }