Back-port Russian docs fixes to English
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 289b5b2 · Browse files · 1 file·+167 added−149 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+167 added−149 removed
- other:+167 added−149 removed
Provenance
- Commit
292fe82- Tree
1eb5f8a- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
289b5b2
1 file changed+167 added−149 removed
modified:README.md+167 added-149 removed
37 unmodified lines38 ## Table of Contents39 40 * [Comparison with UUID](#comparison-with-uuid)41 * [Benchmark](#benchmark)42-* [Tools](#tools)42 * [Security](#security)43+* [API](#api)44+ * [Blocking](#blocking)45+ * [Async](#async)46+ * [Non-Secure](#non-secure)47+ * [Custom Alphabet or Size](#custom-alphabet-or-size)48+ * [Custom Random Bytes Generator](#custom-random-bytes-generator)49 * [Usage](#usage)45- * [JS](#js)50 * [IE](#ie)51 * [React](#react)52+ * [HTML ID](#html-id)53 * [React Native](#react-native)54 * [Rollup](#rollup)55 * [PouchDB and CouchDB](#pouchdb-and-couchdb)56 * [Mongoose](#mongoose)52- * [ES Modules](#es-modules)57 * [Web Workers](#web-workers)58 * [CLI](#cli)59 * [Other Programming Languages](#other-programming-languages)56-* [API](#api)57- * [Async](#async)58- * [Non-Secure](#non-secure)59- * [Custom Alphabet or Size](#custom-alphabet-or-size)60- * [Custom Random Bytes Generator](#custom-random-bytes-generator)60+* [Tools](#tools)61 62 63 ## Comparison with UUID64 38 unmodified lines103 104 Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.9.105 106 107-## Tools108-109-* [ID size calculator] shows collision probability when adjusting110- the ID alphabet or size.111-* [`nanoid-dictionary`] with popular alphabets to use with [`customAlphabet`].112-* [`nanoid-good`] to be sure that your ID doesn’t contain any obscene words.113-114-[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary115-[ID size calculator]: https://zelark.github.io/nano-id-cc/116-[`customAlphabet`]: #custom-alphabet-or-size117-[`nanoid-good`]: https://github.com/y-gagar1n/nanoid-good118-119-107 ## Security108 109 *See a good article about random generators theory:110 [Secure random values (in Node.js)]*20 unmodified lines131 [better algorithm]: https://github.com/ai/nanoid/blob/main/index.js132 [the source]: https://github.com/ai/nanoid/blob/main/index.js133 134 148-## Usage135+## Install136 150-### JS137+```bash138+npm install --save nanoid139+```140 152-The main function uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID153-with 21 characters (to have a collision probability similar to UUID v4).141+For quick hacks, you can load Nano ID from CDN. Though, it is not recommended142+to be used in production because of the lower loading performance.143 144 ```js145+import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'146+```147+148+Nano ID provides ES modules. You do not need to do anything to use Nano ID149+as ESM in webpack, Rollup, Parcel, or Node.js.150+151+```js152 import { nanoid } from 'nanoid'157-model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"153 ```154 155 In Node.js you can use CommonJS import:156 157 ```js158 const { nanoid } = require('nanoid')159 ```160 161+162+## API163+164+Nano ID has 3 APIs: normal (blocking), asynchronous, and non-secure.165+166+By default, Nano ID uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID167+with 21 characters (to have a collision probability similar to UUID v4).168+169+170+### Blocking171+172+The safe and easiest way to use Nano ID.173+174+In rare cases could block CPU from other work while noise collection175+for hardware random generator.176+177+```js178+import { nanoid } from 'nanoid'179+model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"180+```181+182 If you want to reduce the ID size (and increase collisions probability),183 you can pass the size as an argument.184 185 ```js8 unmodified lines194 195 [ID collision probability]: https://zelark.github.io/nano-id-cc/196 197 198+### Async199+200+To generate hardware random bytes, CPU collects electromagnetic noise.201+For most cases, entropy will be already collected.202+203+In the synchronous API during the noise collection, the CPU is busy and204+cannot do anything useful (for instance, process another HTTP request).205+206+Using the asynchronous API of Nano ID, another code can run during207+the entropy collection.208+209+```js210+import { nanoid } from 'nanoid/async'211+212+async function createUser () {213+ user.id = await nanoid()214+}215+```216+217+Read more about entropy collection in [`crypto.randomBytes`] docs.218+219+Unfortunately, you will lose Web Crypto API advantages in a browser220+if you use the asynchronous API. So, currently, in the browser, you are limited221+with either security or asynchronous behavior.222+223+[`crypto.randomBytes`]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback224+225+226+### Non-Secure227+228+By default, Nano ID uses hardware random bytes generation for security229+and low collision probability. If you are not so concerned with security230+and more concerned with performance, you can use the faster non-secure generator.231+232+```js233+import { nanoid } from 'nanoid/non-secure'234+const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"235+```236+237+238+### Custom Alphabet or Size239+240+`customAlphabet` allows you to create `nanoid` with your own alphabet241+and ID size.242+243+```js244+import { customAlphabet } from 'nanoid'245+const nanoid = customAlphabet('1234567890abcdef', 10)246+model.id = nanoid() //=> "4f90d13a42"247+```248+249+Check the safety of your custom alphabet and ID size in our250+[ID collision probability] calculator. For more alphabets, check out the options251+in [`nanoid-dictionary`].252+253+Alphabet must contain 256 symbols or less.254+Otherwise, the security of the internal generator algorithm is not guaranteed.255+256+Customizable asynchronous and non-secure APIs are also available:257+258+```js259+import { customAlphabet } from 'nanoid/async'260+const nanoid = customAlphabet('1234567890abcdef', 10)261+async function createUser () {262+ user.id = await nanoid()263+}264+```265+266+```js267+import { customAlphabet } from 'nanoid/non-secure'268+const nanoid = customAlphabet('1234567890abcdef', 10)269+user.id = nanoid()270+```271+272+[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/273+[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary274+275+276+### Custom Random Bytes Generator277+278+`customRandom` allows you to create a `nanoid` and replace alphabet279+and the default random bytes generator.280+281+In this example, a seed-based generator is used:282+283+```js284+import { customRandom } from 'nanoid'285+286+const rng = seedrandom(seed)287+const nanoid = customRandom('abcdef', 10, size => {288+ return (new Uint8Array(size)).map(() => 256 * rng())289+})290+291+nanoid() //=> "fbaefaadeb"292+```293+294+`random` callback must accept the array size and return an array295+with random numbers.296+297+If you want to use the same URL-friendly symbols with `customRandom`,298+you can get the default alphabet using the `urlAlphabet`.299+300+```js301+const { customRandom, urlAlphabet } = require('nanoid')302+const nanoid = customRandom(urlAlphabet, 10, random)303+```304+305+Asynchronous and non-secure APIs are not available for `customRandom`.306+307+308+## Usage309+310 ### IE311 312 If you support IE, you need to [transpile `node_modules`] by Babel313 and add `crypto` alias:41 unmodified lines355 </li>356 )357 ```358 231-In case you don’t have stable ids you'd rather use index as `key`359+In case you don’t have stable IDs you'd rather use index as `key`360 instead of `nanoid()`:361 362 ```jsx363 const todoItems = todos.map((text, index) =>3 unmodified lines367 </li>368 )369 ```370 371+372+### HTML ID373+374 If you want to use Nano ID in the `id` prop, you must set some string prefix375 (it is invalid for the HTML ID to start with a number).376 377 ```jsx65 unmodified lines443 })444 ```445 446 316-### ES Modules317-318-Nano ID provides ES modules. You do not need to do anything to use Nano ID319-as ESM in webpack, Rollup, Parcel, or Node.js.320-321-```js322-import { nanoid } from 'nanoid'323-```324-325-For quick hacks, you can load Nano ID from CDN. Special minified326-`nanoid.js` module is available on jsDelivr.327-328-Though, it is not recommended to be used in production329-because of the lower loading performance.330-331-```js332-import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'333-```334-335-447 ### Web Workers448 449 Web Workers do not have access to a secure random generator.450 56 unmodified lines507 508 [CLI]: #cli509 510 400-## API511+## Tools512 402-### Async513+* [ID size calculator] shows collision probability when adjusting514+ the ID alphabet or size.515+* [`nanoid-dictionary`] with popular alphabets to use with [`customAlphabet`].516+* [`nanoid-good`] to be sure that your ID doesn’t contain any obscene words.517 404-To generate hardware random bytes, CPU collects electromagnetic noise.405-In the synchronous API during the noise collection, the CPU is busy and406-cannot do anything useful in parallel.407-408-Using the asynchronous API of Nano ID, another code can run during409-the entropy collection.410-411-```js412-import { nanoid } from 'nanoid/async'413-414-async function createUser () {415- user.id = await nanoid()416-}417-```418-419-Unfortunately, you will lose Web Crypto API advantages in a browser420-if you use the asynchronous API. So, currently, in the browser, you are limited421-with either security or asynchronous behavior.422-423-424-### Non-Secure425-426-By default, Nano ID uses hardware random bytes generation for security427-and low collision probability. If you are not so concerned with security428-and more concerned with performance, you can use the faster non-secure generator.429-430-```js431-import { nanoid } from 'nanoid/non-secure'432-const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"433-```434-435-436-### Custom Alphabet or Size437-438-`customAlphabet` allows you to create `nanoid` with your own alphabet439-and ID size.440-441-```js442-import { customAlphabet } from 'nanoid'443-const nanoid = customAlphabet('1234567890abcdef', 10)444-model.id = nanoid() //=> "4f90d13a42"445-```446-447-Check the safety of your custom alphabet and ID size in our448-[ID collision probability] calculator. For more alphabets, check out the options449-in [`nanoid-dictionary`].450-451-Alphabet must contain 256 symbols or less.452-Otherwise, the security of the internal generator algorithm is not guaranteed.453-454-Customizable asynchronous and non-secure APIs are also available:455-456-```js457-import { customAlphabet } from 'nanoid/async'458-const nanoid = customAlphabet('1234567890abcdef', 10)459-async function createUser () {460- user.id = await nanoid()461-}462-```463-464-```js465-import { customAlphabet } from 'nanoid/non-secure'466-const nanoid = customAlphabet('1234567890abcdef', 10)467-user.id = nanoid()468-```469-470-[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/471-[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary472-473-474-### Custom Random Bytes Generator475-476-`customRandom` allows you to create a `nanoid` and replace alphabet477-and the default random bytes generator.478-479-In this example, a seed-based generator is used:480-481-```js482-import { customRandom } from 'nanoid'483-484-const rng = seedrandom(seed)485-const nanoid = customRandom('abcdef', 10, size => {486- return (new Uint8Array(size)).map(() => 256 * rng())487-})488-489-nanoid() //=> "fbaefaadeb"490-```491-492-`random` callback must accept the array size and return an array493-with random numbers.494-495-If you want to use the same URL-friendly symbols with `customRandom`,496-you can get the default alphabet using the `urlAlphabet`.497-498-```js499-const { customRandom, urlAlphabet } = require('nanoid')500-const nanoid = customRandom(urlAlphabet, 10, random)501-```502-503-Asynchronous and non-secure APIs are not available for `customRandom`.518+[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary519+[ID size calculator]: https://zelark.github.io/nano-id-cc/520+[`customAlphabet`]: #custom-alphabet-or-size521+[`nanoid-good`]: https://github.com/y-gagar1n/nanoid-good