Skip to content
Myr

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

10 files+35 added−31 removed

modified:async/index.browser.js+2 added-2 removed
modified:test/alphabet+1 added-1 removed
18 unmodified lines
19spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])20var js = fs.readFileSync(output).toString()21fs.unlinkSync(output)2223var match = js.match(/[A-Za-z0-9~_]{30,}/)23var match = js.match(/[A-Za-z0-9-_]{30,}/)24if (!match) {25 process.stderr.write(chalk.red('Alphabet was not found\n'))26 process.exit(1)27}
56 unmodified lines
modified:test/alphabet-worker.js+1 added-1 removed
modified:index.browser.js+2 added-2 removed
modified:index.js+1 added-1 removed
modified:non-secure.js+2 added-2 removed
modified: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 lines
59 },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 lines
modified:README-cn.md+8 added-8 removed
3 unmodified lines
4 alt="Nano ID logo by Anton Lovchikov" width="180" height="94">56一个用于 JavaScript 的唯一字符串 ID 生成器,它迷你、安全,并且对 URL 友好。78* **体积小:** 压缩并打包后仅占 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。1213```js14var nanoid = require('nanoid')15model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B~myT"15model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"16```1718这个生成器支持 Node.js, React Native, 以及 [所有浏览器]。19
38 unmodified lines
5859Nano ID 和 UUID v4 有两个主要的区别:60611. Nano ID 使用了一个更大的字符表,因此一个类似长度的随机位数可以被封装到 21 个字符里,而不是 36 个。622. Nano ID 的代码体积大小比 `uuid/v4` 小三倍: 143 字节,而不是 435 字节。622. Nano ID 的代码体积大小比 `uuid/v4` 小三倍: 141 字节,而不是 435 字节。636465## 性能基准(Benchmark)66
19 unmodified lines
86## 使用方法8788### 常规8990主模块使用 URL 友好型的字符 (`A-Za-z0-9_~`) 并返回长度为 21 的 ID (为了达到与 UUID v4 类似的碰撞概率)90主模块使用 URL 友好型的字符 (`A-Za-z0-9_-`) 并返回长度为 21 的 ID (为了达到与 UUID v4 类似的碰撞概率)9192```js93const nanoid = require('nanoid')94model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"94model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"95```9697字符 `-,.()` 不会在 URL 里转码,因为如果在链接末尾使用,他们会被识别成标点符号。9899如果你想要降低 ID 长度 (并会提高碰撞概率),你可以把长度作为参数传递进去。100101```js102nanoid(10) //=> "IRFa~VaY2b"102nanoid(10) //=> "IRFa-VaY2b"103```104105不要忘记在我们的 [ID 碰撞概率计算器] 检查你的 ID 长度的安全性。106
9 unmodified lines
116如果你不需要不可预测的 ID,但你需要支持 React Native 或 Web Workers,你可以使用不安全的 ID 生成器。117118```js119const nanoid = require('nanoid/non-secure')120model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"120model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"121```122123124## 异步
92 unmodified lines
modified:README.md+8 added-8 removed
3 unmodified lines
4 alt="Nano ID logo by Anton Lovchikov" width="180" height="94">56A tiny, secure, URL-friendly, unique string ID generator for JavaScript.78* **Small.** 142 bytes (minified and gzipped). No dependencies.8* **Small.** 141 bytes (minified and gzipped). No dependencies.9It uses [Size Limit] to control size.10* **Safe.** It uses cryptographically strong random APIs11and 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_-`).14So ID size was reduced from 36 to 21 symbols.1516```js17var nanoid = require('nanoid')18model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B~myT"18model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"19```2021The generator supports Node.js, React Native, and [all browsers].22
48 unmodified lines
71721. Nano ID uses a bigger alphabet, so a similar number of random bits73 are packed in just 21 symbols instead of 36.742. Nano ID code is 3 times less than `uuid/v4` package:75 142 bytes instead of 435.75 141 bytes instead of 435.767778## Benchmark79
19 unmodified lines
99## Usage100101### Normal102103The main module uses URL-friendly symbols (`A-Za-z0-9_~`) and returns an ID103The main module uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID104with 21 characters (to have a collision probability similar to UUID v4).105106```js107const nanoid = require('nanoid')108model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"108model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"109```110111Symbols `-,.()` are not encoded in the URL. If used at the end of a link112they could be identified as a punctuation symbol.
1 unmodified line
114If you want to reduce ID length (and increase collisions probability),115you can pass the length as an argument.116117```js118nanoid(10) //=> "IRFa~VaY2b"118nanoid(10) //=> "IRFa-VaY2b"119```120121Don’t forget to check the safety of your ID length122in our [ID collision probability] calculator.
30 unmodified lines
153you can use non‑secure ID generator.154155```js156const nanoid = require('nanoid/non-secure')157model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"157model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"158```159160161## Async
128 unmodified lines
modified: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 lines
11 * const url = require('nanoid/url')12 * generate(url, 10) //=> "Uakgb_J5m9"13 */14module.exports =15 'SymboljecthasOwnPr0123456789ABCDEFGHIJKLMNQRTUVWXYZ_dfgikpquvxz~'15 'ModuleSymbhasOwnPr-0123456789ABCDEFGHIJKLNQRTUVWXYZ_cfgijkpqtvxz'