Skip to content
Myr

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

13 files+43 added−58 removed

modified:async/index.browser.js+1 added-1 removed
modified:async/index.d.ts+3 added-3 removed
modified:async/index.js+1 added-1 removed
modified:non-secure/index.d.ts+3 added-3 removed
14 unmodified lines
15/**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 */28export function nanoid2 (alphabet: string, size?: number): string28export function nanoid2 (size: number, alphabet: string): string
modified:non-secure/index.js+1 added-1 removed
1let nanoid2 = (alphabet, size = 21) => {1let 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 lines
modified:test/async.test.js+2 added-7 removed
106 unmodified lines
107 })108109 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 })114115 it('accepts string', async () => {116 let id = await nanoid2('a', '5')117 expect(id).toEqual('aaaaa')118 })119115 it('has flat distribution', async () => {116 let COUNT = 100 * 1000117 let LENGTH = 5118 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'119120 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] += 1
15 unmodified lines
modified:test/benchmark+2 added-2 removed
23 unmodified lines
24 .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 lines
49 })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 lines
modified:test/index.test.js+6 added-10 removed
72 unmodified lines
73 })7475 describe('nanoid2', () => {76 it('has options', () => {77 expect(nanoid2('a', 5)).toEqual('aaaaa')77 expect(nanoid2(5, 'a')).toEqual('aaaaa')78 })7980 it('accepts string', () => {81 expect(nanoid2('a', '5')).toEqual('aaaaa')82 })8380 it('has flat distribution', () => {81 let COUNT = 100 * 100082 let LENGTH = 583 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'8485 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 lines
113 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 })120121 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 })126127 describe('urlAlphabet', () => {
31 unmodified lines
modified:test/non-secure.test.js+2 added-6 removed
56 unmodified lines
57})5859describe('nanoid2', () => {60 it('has options', () => {61 expect(nanoid2('a', 5)).toEqual('aaaaa')61 expect(nanoid2(5, 'a')).toEqual('aaaaa')62 })6364 it('accepts string', () => {65 expect(nanoid2('a', '5')).toEqual('aaaaa')66 })6764 it('has flat distribution', () => {65 let COUNT = 100 * 100066 let LENGTH = 567 let ALPHABET = 'abcdefghijklmnopqrstuvwxyz'6869 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 lines
modified:index.browser.js+2 added-2 removed
49 unmodified lines
50// }5152let random = bytes => self.crypto.getRandomValues(new Uint8Array(bytes))5354let nanoid3 = (getRandom, alphabet, size) => {54let 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 lines
88 }89 }90}9192let nanoid2 = (alphabet, size) => nanoid3(random, alphabet, size)92let nanoid2 = (size, alphabet) => nanoid3(size, alphabet, random)9394let nanoid = (size = 21) => {95 let id = ''96 let bytes = self.crypto.getRandomValues(new Uint8Array(size))
25 unmodified lines
modified:index.d.ts+13 added-15 removed
24 unmodified lines
25 * @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 */32export function nanoid2 (alphabet: string, size?: number): string32export function nanoid2 (size: number, alphabet: string): string3334/**35 * Generate unique ID with custom random generator and alphabet.36 *
2 unmodified lines
39 *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 */57export function nanoid3 (60 random: (bytes: number) => Uint8Array,58 size: number,59 alphabet: string,62 size?: number60 random: (bytes: number) => Uint8Array,61): string6263/**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 */71export const urlAlphabet: string72
1 unmodified line
74 * 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 lines
modified:index.js+2 added-2 removed
15 unmodified lines
16 }17 return crypto.randomFillSync(buffer)18}1920let nanoid3 = (getRandom, alphabet, size = 21) => {20let 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 lines
50 }51 }52}5354let nanoid2 = nanoid3.bind(null, random)54let nanoid2 = (size, alphabet) => nanoid3(size, alphabet, random)5556let nanoid = (size = 21) => {57 let bytes = random(size)58 let id = ''
12 unmodified lines
modified:README.md+5 added-5 removed
328 unmodified lines
329to `nanoid`, which accepts only 1 argument (optional size).330331```js332import { nanoid2 } from 'nanoid'333model.id = nanoid2('1234567890abcdef', 10) //=> "4f90d13a42"333model.id = nanoid2(10, '1234567890abcdef') //=> "4f90d13a42"334```335336Check the safety of your custom alphabet and ID length337in our [ID collision probability] calculator.
6 unmodified lines
344345```js346import { nanoid2 } from 'nanoid/async'347async function createUser () {348 user.id = await nanoid2('1234567890abcdef', 10)348 user.id = await nanoid2(10, '1234567890abcdef')349}350```351352```js353import { nanoid2 } from 'nanoid/non-secure'354user.id = nanoid2('1234567890abcdef', 10)354user.id = nanoid2(10, '1234567890abcdef')355```356357[ID collision probability]: https://alex7kom.github.io/nano-nanoid-cc/358[`nanoid-dictionary`]: https://github.com/CyberAP/nanoid-dictionary
17 unmodified lines
376 }377 return result378}379380nanoid3(random, 'abcdef', 10) //=> "fbaefaadeb"380nanoid3(10, 'abcdef', random) //=> "fbaefaadeb"381```382383`random` callback must accept the array size and return an array384with random numbers.
2 unmodified lines
387you can get the default alphabet from the `urlAlphabet`.388389```js390const { nanoid3, urlAlphabet } = require('nanoid')391nanoid3(random, urlAlphabet, 10) //=> "93ce_Ltuub"391nanoid3(10, urlAlphabet, random) //=> "93ce_Ltuub"392```393394Asynchronous and non-secure APIs are not available for `nanoid3`.