Skip to content
Myr

Use ES6 in examples

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 2d39104 · Browse files · 6 files+21 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+21 added−17 removed

  • other:+21 added−17 removed

6 files+21 added−17 removed

modified:async.js+2 added-2 removed
modified:format.js+5 added-3 removed
modified:generate.js+1 added-1 removed
modified:index.js+1 added-1 removed
modified:README.md+11 added-9 removed
98 unmodified lines
99The main module uses URL-friendly symbols (`A-Za-z0-9_~`) and returns an ID100with 21 characters (to have a collision probability similar to UUID v4).101102```js103var nanoid = require('nanoid')103const nanoid = require('nanoid')104model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"105```106107Symbols `-,.()` are not encoded in the URL. If used at the end of a link
22 unmodified lines
130If you don’t need unpredictable IDs, but you need React Native131or Web Workers support, you can use non‑secure ID generator.132133```js134var nanoid = require('nanoid/non-secure')134const nanoid = require('nanoid/non-secure')135model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"136```137138
4 unmodified lines
143for hardware random generator, your other code could be executed durring144the entropy collection.145146```js147var nanoid = require('nanoid/async')148nanoid.then(function (id) {147const nanoid = require('nanoid/async')148nanoid.then(id => {149 model.id = id150})151```152
6 unmodified lines
159If you want to change the ID's alphabet or length160you can use the low-level `generate` module.161162```js163var generate = require('nanoid/generate')163const generate = require('nanoid/generate')164model.id = generate('1234567890abcdef', 10) //=> "4f90d13a42"165```166167Check safety of your custom alphabet and ID length
12 unmodified lines
180You can replace the default safe random generator using the `format` module.181For instance, to use a seed-based generator.182183```js184var format = require('nanoid/format')184const format = require('nanoid/format')185186function random (size) {187 var result = []188 for (var i = 0; i < size; i++) result.push(randomByte())187 const result = []188 for (let i = 0; i < size; i++) {189 result.push(randomByte())190 }191 return result192}193194format(random, "abcdef", 10) //=> "fbaefaadeb"
5 unmodified lines
200If you want to use the same URL-friendly symbols with `format`,201you can get the default alphabet from the `url` file.202203```js202var url = require('nanoid/url')204const url = require('nanoid/url')205format(random, url, 10) //=> "93ce_Ltuub"206```207208
32 unmodified lines
modified:url.js+1 added-1 removed