Add non-secure/generate
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 b3fa096 · Browse files · 4 files·+70 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
4 files+70 added−1 removed
- tests:+32 added−0 removed
- other:+38 added−1 removed
Provenance
- Commit
4b39c0b- Tree
85c81b9- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
b3fa096
4 files changed+70 added−1 removed
added:non-secure/generate.js+27 added
123456789101112131415161718192021222324252627/** * Generate URL-friendly unique ID. This method use non-secure predictable * random generator. * * By default, ID will have 21 symbols to have a collision probability similar * to UUID v4. * * @param {string} alphabet Symbols to be used in ID. * @param {number} [size=21] The number of symbols in ID. * * @return {string} Random string. * * @example * const nanoid = require('nanoid/non-secure') * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL" * * @name nonSecure * @function */module.exports = function (alphabet, size) { size = size || 21 var id = '' while (0 < size--) { id += alphabet[Math.random() * alphabet.length | 0] } return id}added:test/non-secure-generate.test.js+32 added
1234567891011121314151617181920212223242526272829303132var generate = require('../non-secure/generate')
it('has options', function () { expect(generate('a', 5)).toEqual('aaaaa')})
it('has flat distribution', function () { var COUNT = 100 * 1000 var LENGTH = 5 var ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
var chars = { } for (var i = 0; i < COUNT; i++) { var id = generate(ALPHABET, LENGTH) for (var j = 0; j < id.length; j++) { var char = id[j] if (!chars[char]) chars[char] = 0 chars[char] += 1 } }
expect(Object.keys(chars)).toHaveLength(ALPHABET.length)
var max = 0 var min = Number.MAX_SAFE_INTEGER for (var k in chars) { var distribution = (chars[k] * ALPHABET.length) / (COUNT * LENGTH) if (distribution > max) max = distribution if (distribution < min) min = distribution } expect(max - min).toBeLessThanOrEqual(0.05)})modified:package.json+4 added
73 unmodified lines74 "path": "non-secure/index.js",75 "limit": "105 B"76 },77 {78+ "path": "non-secure/generate.js",79+ "limit": "47 B"80+ },81+ {82 "path": "async/index.js",83 "limit": "153 B"84 },85 {101 unmodified linesmodified:README.md+7 added-1 removed
193 unmodified lines194 195 Alphabet must contain 256 symbols or less.196 Otherwise, the generator will not be secure.197 198-Asynchronous API is also available:198+Asynchronous and non-secure API is also available:199 200 ```js201 const generate = require('nanoid/async/generate')202 async function createUser () {203 user.id = await generate('1234567890abcdef', 10)204 }205 ```206 207+```js208+const generate = require('nanoid/non-secure/generate')209+210+user.id = generate('1234567890abcdef', 10)211+```212+213 [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/214 [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary215 216 79 unmodified lines