Skip to content
Myr

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

9 files+33 added−2 removed

modified:async/format.js+1 added
modified:test/async-generate.test.js+6 added
modified:test/async.test.js+7 added
47 unmodified lines
48 expect(id).toHaveLength(10)49 })50})5152it('throws on string', function () {53 expect.assertions(1)54 return async('10').catch(function (error) {55 expect(error).toBeDefined()56 })57})5859it('has no collisions', function () {60 return Promise.all(times(100 * 1000, function () {61 return async()62 })).then(function (ids) {
46 unmodified lines
modified:test/generate.test.js+4 added
modified:test/index.test.js+4 added
modified:test/non-secure-generate.test.js+4 added
2 unmodified lines
3it('has options', function () {4 expect(generate('a', 5)).toEqual('aaaaa')5})67it('accepts string', function () {8 expect(generate('a', '5')).toEqual('aaaaa')9})1011it('has flat distribution', function () {12 var COUNT = 100 * 100013 var LENGTH = 514 var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
22 unmodified lines
modified:test/non-secure.test.js+4 added
13 unmodified lines
14it('changes ID length', function () {15 expect(nonSecure(10)).toHaveLength(10)16})1718it('accepts string', function () {19 expect(nonSecure('10')).toHaveLength(10)20})2122it('has no collisions', function () {23 var used = { }24 for (var i = 0; i < 100 * 1000; i++) {25 var id = nonSecure()
30 unmodified lines
modified:format.js+1 added
27 unmodified lines
28 */29module.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 = +size3334 var id = ''35 while (true) {36 var bytes = random(step)
15 unmodified lines
modified:package.json+2 added-2 removed
63 unmodified lines
64 "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 lines
84 "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