Skip to content
Myr

Add nanoid/async

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 e31f74d · Browse files · 7 files+203 added−1 removed
This commit

State

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

Diff stats

7 files+203 added−1 removed

  • tests:+125 added−0 removed
  • other:+78 added−1 removed

7 files+203 added−1 removed

added:test/async.test.js+81 added
1var crypto = require('crypto')23var async = require('../async')4var url = require('../url')56function times (size, callback) {7 var array = []8 for (var i = 0; i < 100; i++) {9 array.push(1)10 }11 return array.map(callback)12}1314var originRandom = crypto.randomBytes15afterEach(function () {16 crypto.randomBytes = originRandom17})1819it('generates URL-friendly IDs', function () {20 return Promise.all(times(100, function () {21 return async().then(function (id) {22 expect(id).toHaveLength(21)23 expect(typeof id).toEqual('string')24 for (var j = 0; j < id.length; j++) {25 expect(url).toContain(id[j])26 }27 })28 }))29})3031it('changes ID length', function () {32 return async(10).then(function (id) {33 expect(id).toHaveLength(10)34 })35})3637it('has no collisions', function () {38 return Promise.all(times(100 * 1000, function () {39 return async()40 })).then(function (ids) {41 ids.reduce(function (used, id) {42 expect(used[id]).not.toBeDefined()43 used[id] = true44 return used45 }, [])46 })47})4849it('has flat distribution', function () {50 var COUNT = 100 * 100051 var LENGTH = async().length5253 var chars = { }54 return Promise.all(times(COUNT, function () {55 var id = async()56 for (var j = 0; j < id.length; j++) {57 var char = id[j]58 if (!chars[char]) chars[char] = 059 chars[char] += 160 }61 })).then(function () {62 var max = 063 var min = Number.MAX_SAFE_INTEGER64 for (var k in chars) {65 var distribution = (chars[k] * url.length) / (COUNT * LENGTH)66 if (distribution > max) max = distribution67 if (distribution < min) min = distribution68 }69 expect(max - min).toBeLessThanOrEqual(0.05)70 })71})7273it('rejects Promise on error', function () {74 var error = new Error('test')75 crypto.randomBytes = function (size, callback) {76 callback(error)77 }78 return async().catch(function (e) {79 expect(e).toBe(error)80 })81})
modified:test/benchmark+17 added
7 unmodified lines
89var nonSecure = require('../non-secure')10var generate = require('../generate')11var nanoid = require('../')12var async = require('../async')1314var suite = new benchmark.Suite()1516function formatNumber (number) {
17 unmodified lines
34 })35 .add('shortid', function () {36 shortid()37 })38 .add('uid', {39 defer: true,40 fn: function (defer) {41 uid(21).then(function () {42 defer.resolve()43 })44 }45 })46 .add('nanoid/async', {47 defer: true,48 fn: function (defer) {49 async().then(function () {50 defer.resolve()51 })52 }53 })54 .add('rndm', function () {55 rndm(21)56 })57 .add('nanoid/non-secure', function () {
8 unmodified lines
modified:test/browser.test.js+27 added
added:async.browser.js+13 added
added:async.js+39 added
modified:package.json+6 added-1 removed
11 unmodified lines
12 "license": "MIT",13 "repository": "ai/nanoid",14 "browser": {15 "./random.js": "./random.browser.js",16 "./index.js": "./index.browser.js"16 "./index.js": "./index.browser.js",17 "./async.js": "./async.browser.js"18 },19 "devDependencies": {20 "benchmark": "^2.1.4",21 "chalk": "^2.4.1",
46 unmodified lines
68 },69 {70 "path": "non-secure.js",71 "limit": "118 B"72 },73 {74 "path": "async.js",75 "limit": "157 B"76 }77 ],78 "eslintConfig": {79 "extends": "eslint-config-logux/browser",
54 unmodified lines
modified:README.md+20 added
83 unmodified lines
84nanoid/generate 352,418 ops/sec85uid.sync 332,502 ops/sec86uuid/v4 345,867 ops/sec87shortid 34,193 ops/sec88uid 78,343 ops/sec89nanoid/async 79,173 ops/sec90rndm 2,557,778 ops/sec91nanoid/non-secure 2,578,934 ops/sec92```93
41 unmodified lines
135model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"136```137138139## Async140141To generate hardware random bytes, CPU will collect electromagnetic noise.142During the collection, CPU doesn’t work. So if we will use asynchronous API143for hardware random generator, your other code could be executed durring144the entropy collection.145146```js147var nanoid = require('nanoid/async')148nanoid.then(function (id) {149 model.id = id150})151```152153Unfortunately, you will not have any benefits in browser, since Web Crypto API154doesn’t have asynchronous API.155156157### Custom Alphabet or Length158159If you want to change the ID's alphabet or length160you can use the low-level `generate` module.
78 unmodified lines