Change argument order
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 4d835c0 · Browse files · 13 files·+43 added−58 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
13 files+43 added−58 removed
- tests:+12 added−25 removed
- other:+31 added−33 removed
Provenance
- Commit
b488b48- Tree
ccc8b51- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
4d835c0
13 files changed+43 added−58 removed
modified:async/index.browser.js+1 added-1 removed
11234560 unmodified lineslet nanoid2 = (alphabet, size = 21) => {let nanoid2 = (size, alphabet) => { // We can’t use bytes bigger than the alphabet. To make bytes values closer // to the alphabet, we apply bitmask on them. We look for the closest // `2 ** x - 1` number, which will be bigger than alphabet size. If we have // 30 symbols in the alphabet, we will take 31 (00011111).60 unmodified linesmodified:async/index.d.ts+3 added-3 removed
21 unmodified lines222324252627272829303132323334353637373839404110 unmodified lines21 unmodified lines * * Alphabet must contain 256 symbols or less. Otherwise, the generator * will not be secure. * * @param size The number of symbols in ID. * @param alphabet Symbols to be used in ID. * @param size The number of symbols in ID. Defauls is 21. * @return Promise with random string. * * ```js * const { nanoid2 } = require('nanoid/async') * generate('0123456789абвгдеё', 5).then(id => { * generate(5, '0123456789абвгдеё').then(id => { * model.id = id //=> "8ё56а" * }) * ``` */export function nanoid2 (alphabet: string, size?: number): Promise<string>export function nanoid2 (size: number, alphabet: string): Promise<string>
/** * Return array with random bytes from hardware random generator. *10 unmodified linesmodified:async/index.js+1 added-1 removed
14 unmodified lines1516171819192021222344 unmodified lines14 unmodified lines } })})
let nanoid2 = (alphabet, size = 21) => {let nanoid2 = (size, alphabet) => { // We can’t use bytes bigger than the alphabet. To make bytes values closer // to the alphabet, we apply bitmask on them. We look for the closest // `2 ** x - 1` number, which will be bigger than alphabet size. If we have // 30 symbols in the alphabet, we will take 31 (00011111).44 unmodified linesmodified:non-secure/index.d.ts+3 added-3 removed
14 unmodified lines15 /**16 * Generate unique ID with custom alphabet. This method use non-secure17 * predictable random generator with bigger collision probability.18 *19+ * @param size The number of symbols in ID.20 * @param alphabet Symbols to be used in ID.20- * @param size The number of symbols in ID. Defauls is 21.21 * @returns Random string.22 *23 * ```js24 * const { nanoid2 } = require('nanoid/non-secure')25- * model.id = generate('0123456789абвгдеё', 5) //=> "8ё56а"25+ * model.id = generate(5, '0123456789абвгдеё') //=> "8ё56а"26 * ```27 */28-export function nanoid2 (alphabet: string, size?: number): string28+export function nanoid2 (size: number, alphabet: string): stringmodified:non-secure/index.js+1 added-1 removed
1-let nanoid2 = (alphabet, size = 21) => {1+let nanoid2 = (size, alphabet) => {2 let id = ''3 // Complete alternative for `for (var i = 0; i < size; i++)`4 while (size--) {5 // `| 0` is compact and faster alternative for `Math.floor()`27 unmodified linesmodified:test/async.test.js+2 added-7 removed
106 unmodified lines107 })108 109 describe('nanoid2', () => {110 it('has options', async () => {111- let id = await nanoid2('a', 5)111+ let id = await nanoid2(5, 'a')112 expect(id).toEqual('aaaaa')113 })114 115- it('accepts string', async () => {116- let id = await nanoid2('a', '5')117- expect(id).toEqual('aaaaa')118- })119-115 it('has flat distribution', async () => {116 let COUNT = 100 * 1000117 let LENGTH = 5118 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'119 120 let chars = { }121 await Promise.all(times(100, async () => {127- let id = await nanoid2(ALPHABET, LENGTH)122+ let id = await nanoid2(LENGTH, ALPHABET)123 expect(id).toHaveLength(LENGTH)124 for (let char of id) {125 if (!chars[char]) chars[char] = 0126 chars[char] += 115 unmodified linesmodified:test/benchmark+2 added-2 removed
23 unmodified lines24 .add('nanoid', () => {25 nanoid()26 })27 .add('nanoid2', () => {28- nanoid2('1234567890abcdef-', 10)28+ nanoid2(10, '1234567890abcdef-')29 })30 .add('uid.sync', () => {31 uid.sync(14)32 })16 unmodified lines49 })50 .add('async nanoid2', {51 defer: true,52 fn (defer) {53- asyncNanoid2('1234567890abcdef-', 10).then(() => {53+ asyncNanoid2(10, '1234567890abcdef-').then(() => {54 defer.resolve()55 })56 }57 })33 unmodified linesmodified:test/index.test.js+6 added-10 removed
72 unmodified lines73 })74 75 describe('nanoid2', () => {76 it('has options', () => {77- expect(nanoid2('a', 5)).toEqual('aaaaa')77+ expect(nanoid2(5, 'a')).toEqual('aaaaa')78 })79 80- it('accepts string', () => {81- expect(nanoid2('a', '5')).toEqual('aaaaa')82- })83-80 it('has flat distribution', () => {81 let COUNT = 100 * 100082 let LENGTH = 583 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'84 85 let chars = { }86 for (let i = 0; i < COUNT; i++) {91- let id = nanoid2(ALPHABET, LENGTH)87+ let id = nanoid2(LENGTH, ALPHABET)88 for (let char of id) {89 if (!chars[char]) chars[char] = 090 chars[char] += 191 }21 unmodified lines113 bytes = bytes.concat(sequence.slice(0, size - i))114 }115 return bytes116 }121- expect(nanoid3(customRandom, 'abcde', 4)).toEqual('adca')122- expect(nanoid3(customRandom, 'abcde', 18)).toEqual('cbadcbadcbadcbadcc')117+ expect(nanoid3(4, 'abcde', customRandom)).toEqual('adca')118+ expect(nanoid3(18, 'abcde', customRandom)).toEqual('cbadcbadcbadcbadcc')119 })120 121 it('respects size', () => {126- expect(nanoid3(random, 'abcde', 4)).toHaveLength(4)127- expect(nanoid3(random, 'abcde', 20)).toHaveLength(20)122+ expect(nanoid3(4, 'abcde', random)).toHaveLength(4)123+ expect(nanoid3(20, 'abcde', random)).toHaveLength(20)124 })125 })126 127 describe('urlAlphabet', () => {31 unmodified linesmodified:test/non-secure.test.js+2 added-6 removed
56 unmodified lines57 })58 59 describe('nanoid2', () => {60 it('has options', () => {61- expect(nanoid2('a', 5)).toEqual('aaaaa')61+ expect(nanoid2(5, 'a')).toEqual('aaaaa')62 })63 64- it('accepts string', () => {65- expect(nanoid2('a', '5')).toEqual('aaaaa')66- })67-64 it('has flat distribution', () => {65 let COUNT = 100 * 100066 let LENGTH = 567 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'68 69 let chars = { }70 for (let i = 0; i < COUNT; i++) {75- let id = nanoid2(ALPHABET, LENGTH)71+ let id = nanoid2(LENGTH, ALPHABET)72 for (let char of id) {73 if (!chars[char]) chars[char] = 074 chars[char] += 175 }14 unmodified linesmodified:index.browser.js+2 added-2 removed
49 unmodified lines50 // }51 52 let random = bytes => self.crypto.getRandomValues(new Uint8Array(bytes))53 54-let nanoid3 = (getRandom, alphabet, size) => {54+let nanoid3 = (size, alphabet, getRandom) => {55 // We can’t use bytes bigger than the alphabet. To make bytes values closer56 // to the alphabet, we apply bitmask on them. We look for the closest57 // `2 ** x - 1` number, which will be bigger than alphabet size. If we have58 // 30 symbols in the alphabet, we will take 31 (00011111).29 unmodified lines88 }89 }90 }91 92-let nanoid2 = (alphabet, size) => nanoid3(random, alphabet, size)92+let nanoid2 = (size, alphabet) => nanoid3(size, alphabet, random)93 94 let nanoid = (size = 21) => {95 let id = ''96 let bytes = self.crypto.getRandomValues(new Uint8Array(size))25 unmodified linesmodified:index.d.ts+13 added-15 removed
24 unmodified lines25 * @returns Random string.26 *27 * ```js28 * const { nanoid2 } = require('nanoid')29- * model.id = nanoid2('0123456789абвгдеё', 5) //=> "8ё56а"29+ * model.id = nanoid2(5, '0123456789абвгдеё') //=> "8ё56а"30 * ```31 */32-export function nanoid2 (alphabet: string, size?: number): string32+export function nanoid2 (size: number, alphabet: string): string33 34 /**35 * Generate unique ID with custom random generator and alphabet.36 *2 unmodified lines39 *40 * ```js41 * import { nanoid3 } from 'nanoid/format'42 *43- * function random (size) {44- * const result = []43+ * nanoid3(5, "abcdef", size => {44+ * const random = []45 * for (let i = 0; i < size; i++) {46- * result.push(randomByte())46+ * random.push(randomByte())47 * }48- * return result49- * }50- *51- * nanoid3(random, "abcdef", 5) //=> "fbaef"48+ * return random49+ * }) //=> "fbaef"50 * ```51 *54- * @param random The random bytes generator.55- * @param alphabet Symbols to be used in new random string.52 * @param size The number of symbols in new random string.53+ * @param alphabet Symbols to be used in new random string.54+ * @param random The random bytes generator.55 * @returns Random string.56 */57 export function nanoid3 (60- random: (bytes: number) => Uint8Array,58+ size: number,59 alphabet: string,62- size?: number60+ random: (bytes: number) => Uint8Array,61 ): string62 63 /**64 * URL safe symbols.65 *66 * ```js67 * import { urlAlphabet } from 'nanoid'70- * generate(url, 10) //=> "Uakgb_J5m9"68+ * generate(10, url) //=> "Uakgb_J5m9"69 * ```70 */71 export const urlAlphabet: string72 1 unmodified line74 * Return array with random bytes from hardware random generator.75 *76 * ```js77 * import { nanoid3, random } from 'nanoid'80- * nanoid3(random, "abcdef", 5) //=> "fbaef"78+ * nanoid3(5, "abcdef", random) //=> "fbaef"79 * ```80 *81 * @param bytes The size of array.82 * @returns Arrays with random bytes.2 unmodified linesmodified:index.js+2 added-2 removed
15 unmodified lines16 }17 return crypto.randomFillSync(buffer)18 }19 20-let nanoid3 = (getRandom, alphabet, size = 21) => {20+let nanoid3 = (size, alphabet, getRandom) => {21 // We can’t use bytes bigger than the alphabet. To make bytes values closer22 // to the alphabet, we apply bitmask on them. We look for the closest23 // `2 ** x - 1` number, which will be bigger than alphabet size. If we have24 // 30 symbols in the alphabet, we will take 31 (00011111).25 unmodified lines50 }51 }52 }53 54-let nanoid2 = nanoid3.bind(null, random)54+let nanoid2 = (size, alphabet) => nanoid3(size, alphabet, random)55 56 let nanoid = (size = 21) => {57 let bytes = random(size)58 let id = ''12 unmodified linesmodified:README.md+5 added-5 removed
328 unmodified lines329 to `nanoid`, which accepts only 1 argument (optional size).330 331 ```js332 import { nanoid2 } from 'nanoid'333-model.id = nanoid2('1234567890abcdef', 10) //=> "4f90d13a42"333+model.id = nanoid2(10, '1234567890abcdef') //=> "4f90d13a42"334 ```335 336 Check the safety of your custom alphabet and ID length337 in our [ID collision probability] calculator.6 unmodified lines344 345 ```js346 import { nanoid2 } from 'nanoid/async'347 async function createUser () {348- user.id = await nanoid2('1234567890abcdef', 10)348+ user.id = await nanoid2(10, '1234567890abcdef')349 }350 ```351 352 ```js353 import { nanoid2 } from 'nanoid/non-secure'354-user.id = nanoid2('1234567890abcdef', 10)354+user.id = nanoid2(10, '1234567890abcdef')355 ```356 357 [ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/358 [`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary17 unmodified lines376 }377 return result378 }379 380-nanoid3(random, 'abcdef', 10) //=> "fbaefaadeb"380+nanoid3(10, 'abcdef', random) //=> "fbaefaadeb"381 ```382 383 `random` callback must accept the array size and return an array384 with random numbers.2 unmodified lines387 you can get the default alphabet from the `urlAlphabet`.388 389 ```js390 const { nanoid3, urlAlphabet } = require('nanoid')391-nanoid3(random, urlAlphabet, 10) //=> "93ce_Ltuub"391+nanoid3(10, urlAlphabet, random) //=> "93ce_Ltuub"392 ```393 394 Asynchronous and non-secure APIs are not available for `nanoid3`.