Skip to content
Myr

Add async random, generate, and format

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 71d61c6 · Browse files · 10 files+279 added−3 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+279 added−3 removed

  • tests:+130 added−0 removed
  • other:+149 added−3 removed

10 files+279 added−3 removed

added:async/format.js+56 added
added:async/generate.js+26 added
modified:async/index.js+1 added
added:async/random.browser.js+5 added
added:async/random.js+27 added
added:test/async-format.test.js+15 added
added:test/async-generate.test.js+42 added
added:test/async-random.test.js+73 added
modified:package.json+10 added-3 removed
10 unmodified lines
11 "author": "Andrey Sitnik <andrey@sitnik.ru>",12 "license": "MIT",13 "repository": "ai/nanoid",14 "browser": {15 "./random.js": "./random.browser.js",15 "./index.js": "./index.browser.js",16 "./random.js": "./random.browser.js",17 "./async/index.js": "./async/index.browser.js"17 "./async/index.js": "./async/index.browser.js",18 "./async/random.js": "./async/random.browser.js"19 },20 "devDependencies": {21 "benchmark": "^2.1.4",22 "chalk": "^2.4.1",
50 unmodified lines
73 },74 {75 "path": "async/index.js",76 "limit": "155 B"77 },78 {79 "path": "async/generate.js",80 "limit": "216 B"81 }82 ],83 "eslintConfig": {84 "extends": "eslint-config-logux/browser",
77 unmodified lines
162 "Tsvettsikh",163 "Belym",164 "Oct",165 "Nim",161 "rqrqrqrq"166 "rqrqrqrq",167 "formatAsync",168 "generateAsync"169 ]170 }171}
modified:README.md+24 added
174 unmodified lines
175176Alphabet must contain 256 symbols or less.177Otherwise, the generator will not be secure.178179Asynchronous API is also available:180181```js182const generate = require('nanoid/async/generate')183async function createUser () {184 user.id = await generate('1234567890abcdef', 10)185}186```187188[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/189[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary190191
26 unmodified lines
218const url = require('nanoid/url')219format(random, url, 10) //=> "93ce_Ltuub"220```221222Asynchronous API is also available:223224```js225const format = require('nanoid/async/format')226const url = require('nanoid/url')227228function random (size) {229 return new Promise(…)230}231232async function createUser () {233 user.id = await format(random, url, 10)234}235```236237238## Tools239240* [ID size calculator] to choice smaller ID size depends on your case.241* [`nanoid-dictionary`] with popular alphabets to use with `nanoid/generate`.
29 unmodified lines