Allow to pass string as ID size
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 5de23bd · Browse files · 9 files·+33 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+33 added−2 removed
- tests:+29 added−0 removed
- other:+4 added−2 removed
Provenance
- Commit
8317d9c- Tree
98edf5e- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
5de23bd
9 files changed+33 added−2 removed
modified:async/format.js+1 added
29 unmodified lines30313233343536373818 unmodified lines29 unmodified lines */module.exports = function (random, alphabet, size) { var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1 var step = Math.ceil(1.6 * mask * size / alphabet.length) size = +size
function tick (id) { return random(step).then(function (bytes) { for (var i = 0; i < step; i++) {18 unmodified linesmodified:test/async-generate.test.js+6 added
12 unmodified lines131415161718192021222324252623 unmodified lines12 unmodified lines expect(id).toEqual('aaaaa') })})
it('accepts string', function () { return generate('a', '5').then(function (id) { expect(id).toEqual('aaaaa') })})
it('has flat distribution', function () { var COUNT = 100 * 1000 var LENGTH = 5 var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'23 unmodified linesmodified:test/async.test.js+7 added
47 unmodified lines48 expect(id).toHaveLength(10)49 })50 })51 52+it('throws on string', function () {53+ expect.assertions(1)54+ return async('10').catch(function (error) {55+ expect(error).toBeDefined()56+ })57+})58+59 it('has no collisions', function () {60 return Promise.all(times(100 * 1000, function () {61 return async()62 })).then(function (ids) {46 unmodified linesmodified:test/generate.test.js+4 added
2 unmodified lines3456789101112131422 unmodified lines2 unmodified linesit('has options', function () { expect(generate('a', 5)).toEqual('aaaaa')})
it('accepts string', function () { expect(generate('a', '5')).toEqual('aaaaa')})
it('has flat distribution', function () { var COUNT = 100 * 1000 var LENGTH = 5 var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'22 unmodified linesmodified:test/index.test.js+4 added
14 unmodified lines15161718192021222324252630 unmodified lines14 unmodified linesit('changes ID length', function () { expect(nanoid(10)).toHaveLength(10)})
it('accepts string', function () { expect(nanoid('10')).toHaveLength(10)})
it('has no collisions', function () { var used = { } for (var i = 0; i < 100 * 1000; i++) { var id = nanoid()30 unmodified linesmodified:test/non-secure-generate.test.js+4 added
2 unmodified lines3 it('has options', function () {4 expect(generate('a', 5)).toEqual('aaaaa')5 })6 7+it('accepts string', function () {8+ expect(generate('a', '5')).toEqual('aaaaa')9+})10+11 it('has flat distribution', function () {12 var COUNT = 100 * 100013 var LENGTH = 514 var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'22 unmodified linesmodified:test/non-secure.test.js+4 added
13 unmodified lines14 it('changes ID length', function () {15 expect(nonSecure(10)).toHaveLength(10)16 })17 18+it('accepts string', function () {19+ expect(nonSecure('10')).toHaveLength(10)20+})21+22 it('has no collisions', function () {23 var used = { }24 for (var i = 0; i < 100 * 1000; i++) {25 var id = nonSecure()30 unmodified linesmodified:format.js+1 added
27 unmodified lines28 */29 module.exports = function (random, alphabet, size) {30 var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 131 var step = Math.ceil(1.6 * mask * size / alphabet.length)32+ size = +size33 34 var id = ''35 while (true) {36 var bytes = random(step)15 unmodified linesmodified:package.json+2 added-2 removed
63 unmodified lines64 "limit": "141 B"65 },66 {67 "path": "generate.js",68- "limit": "177 B"68+ "limit": "180 B"69 },70 {71 "path": "url.js",72 "limit": "64 B"11 unmodified lines84 "limit": "153 B"85 },86 {87 "path": "async/generate.js",88- "limit": "198 B"88+ "limit": "200 B"89 }90 ],91 "eslintConfig": {92 "extends": "@logux/eslint-config/browser",64 unmodified lines