Created
September 14, 2023 07:46
-
-
Save axyz/78271988f6597ac60fc5a54748ec47d0 to your computer and use it in GitHub Desktop.
Encode code string in a compressed URL string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { strToU8, strFromU8, compressSync, decompressSync } from "fflate"; | |
const urlEncode = (str: string) => | |
btoa(str).replace( | |
/[/+=]/g, | |
(a) => ({ "/": "-", "+": "_", "=": "~" }[a] || a) | |
); | |
const urlDecode = (str: string) => | |
atob(str.replace(/[-_~]/g, (a) => ({ "-": "/", _: "+", "~": "=" }[a] || a))); | |
const deflate = (str: string) => | |
strFromU8(compressSync(strToU8(str), { level: 9 }), true); | |
const inflate = (str: string) => strFromU8(decompressSync(strToU8(str, true))); | |
export function encode(string: string) { | |
return urlEncode(deflate(string)); | |
} | |
export function decode(string: string) { | |
return inflate(urlDecode(string)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Go implementation