summaryrefslogtreecommitdiff
path: root/node_modules/nanoid/async/index.native.js
diff options
context:
space:
mode:
authorakiyamn2023-09-24 23:22:21 +1000
committerakiyamn2023-09-24 23:22:21 +1000
commit4e87195739f2a5d9a05451b48773c8afdc680765 (patch)
tree9cba501844a4a11dcbdffc4050ed8189561c55ed /node_modules/nanoid/async/index.native.js
downloadprice-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.tar.gz
price-tracker-worker-4e87195739f2a5d9a05451b48773c8afdc680765.zip
Initial commit (by create-cloudflare CLI)
Diffstat (limited to 'node_modules/nanoid/async/index.native.js')
-rw-r--r--node_modules/nanoid/async/index.native.js26
1 files changed, 26 insertions, 0 deletions
diff --git a/node_modules/nanoid/async/index.native.js b/node_modules/nanoid/async/index.native.js
new file mode 100644
index 0000000..5cb3d57
--- /dev/null
+++ b/node_modules/nanoid/async/index.native.js
@@ -0,0 +1,26 @@
+import { getRandomBytesAsync } from 'expo-random'
+import { urlAlphabet } from '../url-alphabet/index.js'
+let random = getRandomBytesAsync
+let customAlphabet = (alphabet, defaultSize = 21) => {
+ let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
+ let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
+ let tick = (id, size = defaultSize) =>
+ random(step).then(bytes => {
+ let i = step
+ while (i--) {
+ id += alphabet[bytes[i] & mask] || ''
+ if (id.length === size) return id
+ }
+ return tick(id, size)
+ })
+ return size => tick('', size)
+}
+let nanoid = (size = 21) =>
+ random(size).then(bytes => {
+ let id = ''
+ while (size--) {
+ id += urlAlphabet[bytes[size] & 63]
+ }
+ return id
+ })
+export { nanoid, customAlphabet, random }