Last change
on this file since 6a80231 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
759 bytes
|
Line | |
---|
1 | import { getRandomBytesAsync } from 'expo-random'
|
---|
2 | import { urlAlphabet } from '../url-alphabet/index.js'
|
---|
3 | let random = getRandomBytesAsync
|
---|
4 | let customAlphabet = (alphabet, size) => {
|
---|
5 | let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
|
---|
6 | let step = Math.ceil((1.6 * mask * size) / alphabet.length)
|
---|
7 | let tick = id =>
|
---|
8 | random(step).then(bytes => {
|
---|
9 | let i = step
|
---|
10 | while (i--) {
|
---|
11 | id += alphabet[bytes[i] & mask] || ''
|
---|
12 | if (id.length === size) return id
|
---|
13 | }
|
---|
14 | return tick(id)
|
---|
15 | })
|
---|
16 | return () => tick('')
|
---|
17 | }
|
---|
18 | let nanoid = (size = 21) =>
|
---|
19 | random(size).then(bytes => {
|
---|
20 | let id = ''
|
---|
21 | while (size--) {
|
---|
22 | id += urlAlphabet[bytes[size] & 63]
|
---|
23 | }
|
---|
24 | return id
|
---|
25 | })
|
---|
26 | export { nanoid, customAlphabet, random }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.