Skip to content
Myr

Add non-secure generator

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 889c1cc · Browse files · 6 files+109 added−11 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+109 added−11 removed

  • tests:+64 added−6 removed
  • other:+45 added−5 removed

6 files+109 added−11 removed

modified:test/demo/index.js+8 added-2 removed
modified:test/benchmark+10 added-4 removed
2 unmodified lines
3var shortid = require('shortid')4var uuid4 = require('uuid/v4')5var chalk = require('chalk')67var nonSecure = require('../non-secure')8var generate = require('../generate')9var nanoid = require('../')1011var suite = new benchmark.Suite()1213function formatNumber (number) {13 return String(number).replace(/\d\d\d$/, ',$&')14 return String(number)15 .replace(/\d\d\d$/, ',$&')16 .replace(/^(\d)(\d\d\d)/, '$1,$2')17}1819suite20 .add('nanoid', function () {21 nanoid()22 })23 .add('nanoid/non-secure', function () {24 nonSecure()25 })26 .add('nanoid/generate', function () {27 generate('1234567890abcdef-', 10)28 })29 .add('uuid/v4', function () {
2 unmodified lines
32 .add('shortid', function () {33 shortid()34 })35 .on('cycle', function (event) {30 var name = event.target.name.padEnd('nanoid/generate'.length)31 var hz = formatNumber(event.target.hz.toFixed(0)).padStart(7)32 process.stdout.write(name + ' ' + chalk.bold(hz) + ' ops/sec \n')36 var name = event.target.name.padEnd('nanoid/non-secure'.length)37 var hz = formatNumber(event.target.hz.toFixed(0)).padStart(9)38 process.stdout.write(name + ' ' + chalk.bold(hz) + ' ops/sec\n')39 })40 .run()
added:test/non-secure.test.js+46 added
modified:.size-limit+4 added
4 unmodified lines
5 },6 {7 "path": "generate.js",8 "limit": "179 B"9 },10 {11 "path": "non-secure.js",12 "limit": "135 B"13 }14]
added:non-secure.js+19 added
modified:README.md+22 added-5 removed
77 unmodified lines
7879## Benchmark8081```82$ ./benchmark83nanoid 332,991 ops/sec84nanoid/generate 326,636 ops/sec85uuid/v4 315,344 ops/sec86shortid 33,717 ops/sec82$ ./test/benchmark83nanoid 353,737 ops/sec84nanoid/non-secure 2,043,665 ops/sec85nanoid/generate 348,442 ops/sec86uuid/v4 350,075 ops/sec87shortid 85,107 ops/sec88```899091## Usage
23 unmodified lines
115116[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/117118119### React Native and Web Workers120121React Native and Web Worker don’t have access to secure random generator.122123Security is important in ID, when ID should be unpredictable. For instance,124in “access by URL” link generation.125126If you don’t need unpredictable IDs, but you need React Native127or Web Workers support, you can use non-secure ID generator:128129```js130var nanoid = require('nanoid/non-secure')131model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"132```133134135### Custom Alphabet or Length136137If you want to change the ID's alphabet or length138you can use the low-level `generate` module.
77 unmodified lines