aboutsummaryrefslogtreecommitdiffstats
path: root/vanilla/node_modules/@exodus/bytes/fallback/percent.js
diff options
context:
space:
mode:
authorAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
committerAdam Mathes <adam@adammathes.com>2026-02-13 21:34:48 -0800
commit76cb9c2a39d477a64824a985ade40507e3bbade1 (patch)
tree41e997aa9c6f538d3a136af61dae9424db2005a9 /vanilla/node_modules/@exodus/bytes/fallback/percent.js
parent819a39a21ac992b1393244a4c283bbb125208c69 (diff)
downloadneko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.gz
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.tar.bz2
neko-76cb9c2a39d477a64824a985ade40507e3bbade1.zip
feat(vanilla): add testing infrastructure and tests (NK-wjnczv)
Diffstat (limited to 'vanilla/node_modules/@exodus/bytes/fallback/percent.js')
-rw-r--r--vanilla/node_modules/@exodus/bytes/fallback/percent.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/vanilla/node_modules/@exodus/bytes/fallback/percent.js b/vanilla/node_modules/@exodus/bytes/fallback/percent.js
new file mode 100644
index 0000000..c024d90
--- /dev/null
+++ b/vanilla/node_modules/@exodus/bytes/fallback/percent.js
@@ -0,0 +1,31 @@
+import { decodeAscii, encodeLatin1 } from './latin1.js'
+import { decode2string } from './platform.js'
+
+const ERR = 'percentEncodeSet must be a string of unique increasing codepoints in range 0x20 - 0x7e'
+const percentMap = new Map()
+let hex, base
+
+export function percentEncoder(set, spaceAsPlus = false) {
+ if (typeof set !== 'string' || /[^\x20-\x7E]/.test(set)) throw new TypeError(ERR)
+ if (typeof spaceAsPlus !== 'boolean') throw new TypeError('spaceAsPlus must be boolean')
+ const id = set + +spaceAsPlus
+ const cached = percentMap.get(id)
+ if (cached) return cached
+
+ const n = encodeLatin1(set).sort() // string checked above to be ascii
+ if (decodeAscii(n) !== set || new Set(n).size !== n.length) throw new TypeError(ERR)
+
+ if (!base) {
+ hex = Array.from({ length: 256 }, (_, i) => `%${i.toString(16).padStart(2, '0').toUpperCase()}`)
+ base = hex.map((h, i) => (i < 0x20 || i > 0x7e ? h : String.fromCharCode(i)))
+ }
+
+ const map = base.slice() // copy
+ for (const c of n) map[c] = hex[c]
+ if (spaceAsPlus) map[0x20] = '+' // overrides whatever percentEncodeSet thinks about it
+
+ // Input is not typechecked, for internal use only
+ const percentEncode = (u8, start = 0, end = u8.length) => decode2string(u8, start, end, map)
+ percentMap.set(id, percentEncode)
+ return percentEncode
+}