Skip to content
Myr

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

1 file+167 added−149 removed

modified:README.md+167 added-149 removed
37 unmodified lines
38## Table of Contents3940* [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)616263## Comparison with UUID64
38 unmodified lines
103104Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.9.105106107## Tools108109* [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.113114[`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-good118119107## Security108109*See a good article about random generators theory:110[Secure random values (in Node.js)]*
20 unmodified lines
131[better algorithm]: https://github.com/ai/nanoid/blob/main/index.js132[the source]: https://github.com/ai/nanoid/blob/main/index.js133134148## Usage135## Install136150### JS137```bash138npm install --save nanoid139```140152The main function uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID153with 21 characters (to have a collision probability similar to UUID v4).141For quick hacks, you can load Nano ID from CDN. Though, it is not recommended142to be used in production because of the lower loading performance.143144```js145import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'146```147148Nano ID provides ES modules. You do not need to do anything to use Nano ID149as ESM in webpack, Rollup, Parcel, or Node.js.150151```js152import { nanoid } from 'nanoid'157model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"153```154155In Node.js you can use CommonJS import:156157```js158const { nanoid } = require('nanoid')159```160161162## API163164Nano ID has 3 APIs: normal (blocking), asynchronous, and non-secure.165166By default, Nano ID uses URL-friendly symbols (`A-Za-z0-9_-`) and returns an ID167with 21 characters (to have a collision probability similar to UUID v4).168169170### Blocking171172The safe and easiest way to use Nano ID.173174In rare cases could block CPU from other work while noise collection175for hardware random generator.176177```js178import { nanoid } from 'nanoid'179model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"180```181182If you want to reduce the ID size (and increase collisions probability),183you can pass the size as an argument.184185```js
8 unmodified lines
194195[ID collision probability]: https://zelark.github.io/nano-id-cc/196197198### Async199200To generate hardware random bytes, CPU collects electromagnetic noise.201For most cases, entropy will be already collected.202203In the synchronous API during the noise collection, the CPU is busy and204cannot do anything useful (for instance, process another HTTP request).205206Using the asynchronous API of Nano ID, another code can run during207the entropy collection.208209```js210import { nanoid } from 'nanoid/async'211212async function createUser () {213 user.id = await nanoid()214}215```216217Read more about entropy collection in [`crypto.randomBytes`] docs.218219Unfortunately, you will lose Web Crypto API advantages in a browser220if you use the asynchronous API. So, currently, in the browser, you are limited221with either security or asynchronous behavior.222223[`crypto.randomBytes`]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback224225226### Non-Secure227228By default, Nano ID uses hardware random bytes generation for security229and low collision probability. If you are not so concerned with security230and more concerned with performance, you can use the faster non-secure generator.231232```js233import { nanoid } from 'nanoid/non-secure'234const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"235```236237238### Custom Alphabet or Size239240`customAlphabet` allows you to create `nanoid` with your own alphabet241and ID size.242243```js244import { customAlphabet } from 'nanoid'245const nanoid = customAlphabet('1234567890abcdef', 10)246model.id = nanoid() //=> "4f90d13a42"247```248249Check the safety of your custom alphabet and ID size in our250[ID collision probability] calculator. For more alphabets, check out the options251in [`nanoid-dictionary`].252253Alphabet must contain 256 symbols or less.254Otherwise, the security of the internal generator algorithm is not guaranteed.255256Customizable asynchronous and non-secure APIs are also available:257258```js259import { customAlphabet } from 'nanoid/async'260const nanoid = customAlphabet('1234567890abcdef', 10)261async function createUser () {262 user.id = await nanoid()263}264```265266```js267import { customAlphabet } from 'nanoid/non-secure'268const nanoid = customAlphabet('1234567890abcdef', 10)269user.id = nanoid()270```271272[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/273[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary274275276### Custom Random Bytes Generator277278`customRandom` allows you to create a `nanoid` and replace alphabet279and the default random bytes generator.280281In this example, a seed-based generator is used:282283```js284import { customRandom } from 'nanoid'285286const rng = seedrandom(seed)287const nanoid = customRandom('abcdef', 10, size => {288 return (new Uint8Array(size)).map(() => 256 * rng())289})290291nanoid() //=> "fbaefaadeb"292```293294`random` callback must accept the array size and return an array295with random numbers.296297If you want to use the same URL-friendly symbols with `customRandom`,298you can get the default alphabet using the `urlAlphabet`.299300```js301const { customRandom, urlAlphabet } = require('nanoid')302const nanoid = customRandom(urlAlphabet, 10, random)303```304305Asynchronous and non-secure APIs are not available for `customRandom`.306307308## Usage309310### IE311312If you support IE, you need to [transpile `node_modules`] by Babel313and add `crypto` alias:
41 unmodified lines
355 </li>356)357```358231In case you don’t have stable ids you'd rather use index as `key`359In case you don’t have stable IDs you'd rather use index as `key`360instead of `nanoid()`:361362```jsx363const todoItems = todos.map((text, index) =>
3 unmodified lines
367 </li>368)369```370371372### HTML ID373374If 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).376377```jsx
65 unmodified lines
443})444```445446316### ES Modules317318Nano ID provides ES modules. You do not need to do anything to use Nano ID319as ESM in webpack, Rollup, Parcel, or Node.js.320321```js322import { nanoid } from 'nanoid'323```324325For quick hacks, you can load Nano ID from CDN. Special minified326`nanoid.js` module is available on jsDelivr.327328Though, it is not recommended to be used in production329because of the lower loading performance.330331```js332import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js'333```334335447### Web Workers448449Web Workers do not have access to a secure random generator.450
56 unmodified lines
507508[CLI]: #cli509510400## API511## Tools512402### 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.517404To generate hardware random bytes, CPU collects electromagnetic noise.405In the synchronous API during the noise collection, the CPU is busy and406cannot do anything useful in parallel.407408Using the asynchronous API of Nano ID, another code can run during409the entropy collection.410411```js412import { nanoid } from 'nanoid/async'413414async function createUser () {415 user.id = await nanoid()416}417```418419Unfortunately, you will lose Web Crypto API advantages in a browser420if you use the asynchronous API. So, currently, in the browser, you are limited421with either security or asynchronous behavior.422423424### Non-Secure425426By default, Nano ID uses hardware random bytes generation for security427and low collision probability. If you are not so concerned with security428and more concerned with performance, you can use the faster non-secure generator.429430```js431import { nanoid } from 'nanoid/non-secure'432const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"433```434435436### Custom Alphabet or Size437438`customAlphabet` allows you to create `nanoid` with your own alphabet439and ID size.440441```js442import { customAlphabet } from 'nanoid'443const nanoid = customAlphabet('1234567890abcdef', 10)444model.id = nanoid() //=> "4f90d13a42"445```446447Check the safety of your custom alphabet and ID size in our448[ID collision probability] calculator. For more alphabets, check out the options449in [`nanoid-dictionary`].450451Alphabet must contain 256 symbols or less.452Otherwise, the security of the internal generator algorithm is not guaranteed.453454Customizable asynchronous and non-secure APIs are also available:455456```js457import { customAlphabet } from 'nanoid/async'458const nanoid = customAlphabet('1234567890abcdef', 10)459async function createUser () {460 user.id = await nanoid()461}462```463464```js465import { customAlphabet } from 'nanoid/non-secure'466const nanoid = customAlphabet('1234567890abcdef', 10)467user.id = nanoid()468```469470[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/471[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary472473474### Custom Random Bytes Generator475476`customRandom` allows you to create a `nanoid` and replace alphabet477and the default random bytes generator.478479In this example, a seed-based generator is used:480481```js482import { customRandom } from 'nanoid'483484const rng = seedrandom(seed)485const nanoid = customRandom('abcdef', 10, size => {486 return (new Uint8Array(size)).map(() => 256 * rng())487})488489nanoid() //=> "fbaefaadeb"490```491492`random` callback must accept the array size and return an array493with random numbers.494495If you want to use the same URL-friendly symbols with `customRandom`,496you can get the default alphabet using the `urlAlphabet`.497498```js499const { customRandom, urlAlphabet } = require('nanoid')500const nanoid = customRandom(urlAlphabet, 10, random)501```502503Asynchronous 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