Replace ~ in alphabet to -
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Andrey Sitnik · committed by Andrey Sitnik · parent 2ecb208 · Browse files · 10 files·+35 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+35 added−31 removed
- tests:+2 added−2 removed
- other:+33 added−29 removed
Provenance
- Commit
22bc8ff- Tree
dd11afa- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
2ecb208
10 files changed+35 added−31 removed
modified:async/index.browser.js+2 added-2 removed
1234456778910116 unmodified linesvar crypto = self.crypto || self.msCrypto
/* * This alphabet uses a-z A-Z 0-9 _~ symbols. * This alphabet uses a-z A-Z 0-9 _- symbols. * Symbols order was changed for better gzip compression. */var url = 'ModuleSymbhasOwnPr0123456789ABCDEFGHIJKLNQRTUVWXYZ_cfgijkpqtvxz~'var url = 'Uint8ArModuleSymbhasOw-012345679BCDEFGHIJKLNPQRTVWXYZ_cfgjkpqvxz'
module.exports = function (size) { size = size || 21 var id = ''6 unmodified linesmodified:test/alphabet+1 added-1 removed
18 unmodified lines19 spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])20 var js = fs.readFileSync(output).toString()21 fs.unlinkSync(output)22 23-var match = js.match(/[A-Za-z0-9~_]{30,}/)23+var match = js.match(/[A-Za-z0-9-_]{30,}/)24 if (!match) {25 process.stderr.write(chalk.red('Alphabet was not found\n'))26 process.exit(1)27 }56 unmodified linesmodified:test/alphabet-worker.js+1 added-1 removed
58 unmodified lines5960616263636465666712 unmodified lines58 unmodified lines } else { steps += 1 var alphabet = getAlphabet(positions) if (alphabet) { var file = workerData.js.replace(/[A-Za-z0-9~_]{30,}/, alphabet) var file = workerData.js.replace(/[A-Za-z0-9-_]{30,}/, alphabet) gzipSize(file).then(function (size) { parentPort.postMessage({ alphabet: alphabet, steps: steps,12 unmodified linesmodified:index.browser.js+2 added-2 removed
8 unmodified lines9101112131314151616171819206 unmodified lines8 unmodified lines
var crypto = self.crypto || self.msCrypto
/* * This alphabet uses a-z A-Z 0-9 _~ symbols. * This alphabet uses a-z A-Z 0-9 _- symbols. * Symbols order was changed for better gzip compression. */var url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz~'var url = 'Uint8ArdomValuesObj012345679BCDEFGHIJKLMNPQRSTWXYZ_cfghkpqvwxyz-'
module.exports = function (size) { size = size || 21 var id = ''6 unmodified linesmodified:index.js+1 added-1 removed
11 unmodified lines1213141516161718192023 unmodified lines11 unmodified lines * @return {string} Random string. * * @example * const nanoid = require('nanoid') * model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqL" * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL" * * @name nanoid * @function */23 unmodified linesmodified:non-secure.js+2 added-2 removed
1123456 unmodified lines121314151616171819208 unmodified linesvar url = 'bjectxporhasOwnP0123456789ABCDEFGHIJKLMNQRSTUVWXYZ_dfgiklmquvyz~'var url = 'bjectSymhasOwnProp-0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgiklquvxz'
/** * Generate URL-friendly unique ID. This method use non-secure predictable * random generator.6 unmodified lines * @return {string} Random string. * * @example * const nanoid = require('nanoid/non-secure') * model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqL" * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL" * * @name nonSecure * @function */8 unmodified linesmodified:package.json+8 added-4 removed
1 {2 "name": "nanoid",3 "version": "1.3.4",4- "description": "A tiny (143 bytes), secure URL-friendly unique string ID generator",4+ "description": "A tiny (141 bytes), secure URL-friendly unique string ID generator",5 "keywords": [6 "uuid",7 "random",8 "id",50 unmodified lines59 },60 "size-limit": [61 {62 "path": "index.js",63- "limit": "142 B"63+ "limit": "141 B"64 },65 {66 "path": "generate.js",67 "limit": "177 B"68 },69 {70+ "path": "url.js",71+ "limit": "64 B"72+ },73+ {74 "path": "non-secure.js",71- "limit": "107 B"75+ "limit": "105 B"76 },77 {78 "path": "async/index.js",75- "limit": "154 B"79+ "limit": "153 B"80 },81 {82 "path": "async/generate.js",83 "limit": "198 B"99 unmodified linesmodified:README-cn.md+8 added-8 removed
3 unmodified lines4 alt="Nano ID logo by Anton Lovchikov" width="180" height="94">5 6 一个用于 JavaScript 的唯一字符串 ID 生成器,它迷你、安全,并且对 URL 友好。7 8-* **体积小:** 压缩并打包后仅占 143 字节,无依赖,它使用 [Size Limit] 来控制体积。8+* **体积小:** 压缩并打包后仅占 141 字节,无依赖,它使用 [Size Limit] 来控制体积。9 * **安全:** 它使用了一系列密码学上的非常健壮的 API,并且测试了各种符号的分布。10 * **速度快:** 比 UUID 快16%。11-* **紧凑的:** 它使用了一个比 UUID 更大的字符表 (`A-Za-z0-9_~`)。因此,他可以将 ID 长度从 36 缩减至 21。11+* **紧凑的:** 它使用了一个比 UUID 更大的字符表 (`A-Za-z0-9_-`)。因此,他可以将 ID 长度从 36 缩减至 21。12 13 ```js14 var nanoid = require('nanoid')15-model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B~myT"15+model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"16 ```17 18 这个生成器支持 Node.js, React Native, 以及 [所有浏览器]。19 38 unmodified lines58 59 Nano ID 和 UUID v4 有两个主要的区别:60 61 1. Nano ID 使用了一个更大的字符表,因此一个类似长度的随机位数可以被封装到 21 个字符里,而不是 36 个。62-2. Nano ID 的代码体积大小比 `uuid/v4` 小三倍: 143 字节,而不是 435 字节。62+2. Nano ID 的代码体积大小比 `uuid/v4` 小三倍: 141 字节,而不是 435 字节。63 64 65 ## 性能基准(Benchmark)66 19 unmodified lines86 ## 使用方法87 88 ### 常规89 90-主模块使用 URL 友好型的字符 (`A-Za-z0-9_~`) 并返回长度为 21 的 ID (为了达到与 UUID v4 类似的碰撞概率)90+主模块使用 URL 友好型的字符 (`A-Za-z0-9_-`) 并返回长度为 21 的 ID (为了达到与 UUID v4 类似的碰撞概率)91 92 ```js93 const nanoid = require('nanoid')94-model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"94+model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"95 ```96 97 字符 `-,.()` 不会在 URL 里转码,因为如果在链接末尾使用,他们会被识别成标点符号。98 99 如果你想要降低 ID 长度 (并会提高碰撞概率),你可以把长度作为参数传递进去。100 101 ```js102-nanoid(10) //=> "IRFa~VaY2b"102+nanoid(10) //=> "IRFa-VaY2b"103 ```104 105 不要忘记在我们的 [ID 碰撞概率计算器] 检查你的 ID 长度的安全性。106 9 unmodified lines116 如果你不需要不可预测的 ID,但你需要支持 React Native 或 Web Workers,你可以使用不安全的 ID 生成器。117 118 ```js119 const nanoid = require('nanoid/non-secure')120-model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"120+model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"121 ```122 123 124 ## 异步92 unmodified linesmodified:README.md+8 added-8 removed
3 unmodified lines4 alt="Nano ID logo by Anton Lovchikov" width="180" height="94">5 6 A tiny, secure, URL-friendly, unique string ID generator for JavaScript.7 8-* **Small.** 142 bytes (minified and gzipped). No dependencies.8+* **Small.** 141 bytes (minified and gzipped). No dependencies.9 It uses [Size Limit] to control size.10 * **Safe.** It uses cryptographically strong random APIs11 and tests distribution of symbols.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 So ID size was reduced from 36 to 21 symbols.15 16 ```js17 var nanoid = require('nanoid')18-model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B~myT"18+model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"19 ```20 21 The generator supports Node.js, React Native, and [all browsers].22 48 unmodified lines71 72 1. Nano ID uses a bigger alphabet, so a similar number of random bits73 are packed in just 21 symbols instead of 36.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 77 78 ## Benchmark79 19 unmodified lines99 ## Usage100 101 ### Normal102 103-The main module uses URL-friendly symbols (`A-Za-z0-9_~`) and returns an ID103+The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID104 with 21 characters (to have a collision probability similar to UUID v4).105 106 ```js107 const nanoid = require('nanoid')108-model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"108+model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"109 ```110 111 Symbols `-,.()` are not encoded in the URL. If used at the end of a link112 they could be identified as a punctuation symbol.1 unmodified line114 If you want to reduce ID length (and increase collisions probability),115 you can pass the length as an argument.116 117 ```js118-nanoid(10) //=> "IRFa~VaY2b"118+nanoid(10) //=> "IRFa-VaY2b"119 ```120 121 Don’t forget to check the safety of your ID length122 in our [ID collision probability] calculator.30 unmodified lines153 you can use non‑secure ID generator.154 155 ```js156 const nanoid = require('nanoid/non-secure')157-model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"157+model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"158 ```159 160 161 ## Async128 unmodified linesmodified:url.js+2 added-2 removed
1 /**2 * URL safe symbols.3 *4- * This alphabet uses a-z A-Z 0-9 _~ symbols.4+ * This alphabet uses a-z A-Z 0-9 _- symbols.5 * Symbols order was changed for better gzip compression.6 *7 * @name url8 * @type {string}2 unmodified lines11 * const url = require('nanoid/url')12 * generate(url, 10) //=> "Uakgb_J5m9"13 */14 module.exports =15- 'SymboljecthasOwnPr0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgikpquvxz~'15+ 'ModuleSymbhasOwnPr-0123456789ABCDEFGHIJKLNQRTUVWXYZ_cfgijkpqtvxz'