source: trip-planner-front/node_modules/nanoid/index.browser.cjs@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.9 KB
Line 
1let { urlAlphabet } = require('./url-alphabet/index.cjs')
2if (process.env.NODE_ENV !== 'production') {
3 if (
4 typeof navigator !== 'undefined' &&
5 navigator.product === 'ReactNative' &&
6 typeof crypto === 'undefined'
7 ) {
8 throw new Error(
9 'React Native does not have a built-in secure random generator. ' +
10 'If you don’t need unpredictable IDs use `nanoid/non-secure`. ' +
11 'For secure IDs, import `react-native-get-random-values` ' +
12 'before Nano ID.'
13 )
14 }
15 if (typeof msCrypto !== 'undefined' && typeof crypto === 'undefined') {
16 throw new Error(
17 'Import file with `if (!window.crypto) window.crypto = window.msCrypto`' +
18 ' before importing Nano ID to fix IE 11 support'
19 )
20 }
21 if (typeof crypto === 'undefined') {
22 throw new Error(
23 'Your browser does not have secure random generator. ' +
24 'If you don’t need unpredictable IDs, you can use nanoid/non-secure.'
25 )
26 }
27}
28let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
29let customRandom = (alphabet, size, getRandom) => {
30 let mask = (2 << (Math.log(alphabet.length - 1) / Math.LN2)) - 1
31 let step = -~((1.6 * mask * size) / alphabet.length)
32 return () => {
33 let id = ''
34 while (true) {
35 let bytes = getRandom(step)
36 let j = step
37 while (j--) {
38 id += alphabet[bytes[j] & mask] || ''
39 if (id.length === size) return id
40 }
41 }
42 }
43}
44let customAlphabet = (alphabet, size) => customRandom(alphabet, size, random)
45let nanoid = (size = 21) => {
46 let id = ''
47 let bytes = crypto.getRandomValues(new Uint8Array(size))
48 while (size--) {
49 let byte = bytes[size] & 63
50 if (byte < 36) {
51 id += byte.toString(36)
52 } else if (byte < 62) {
53 id += (byte - 26).toString(36).toUpperCase()
54 } else if (byte < 63) {
55 id += '_'
56 } else {
57 id += '-'
58 }
59 }
60 return id
61}
62module.exports = { nanoid, customAlphabet, customRandom, urlAlphabet, random }
Note: See TracBrowser for help on using the repository browser.