diff options
author | bptato <nincsnevem662@gmail.com> | 2023-09-07 22:20:40 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-09-07 22:20:40 +0200 |
commit | 472145622f03f643090f55d168e021f82a5dd2b6 (patch) | |
tree | e798eeae99d9d4264d95d5d84fabdd5ecfa038bf /src/js | |
parent | 121bb912080570854f5d11683d961f670a8ba749 (diff) | |
download | chawan-472145622f03f643090f55d168e021f82a5dd2b6.tar.gz |
Add btoa, atob
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/base64.nim | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/js/base64.nim b/src/js/base64.nim new file mode 100644 index 00000000..9b61f604 --- /dev/null +++ b/src/js/base64.nim @@ -0,0 +1,24 @@ +import unicode +import std/base64 + +import js/domexception +import utils/opt + +proc atob*(data: string): DOMResult[string] = + try: + return ok(base64.decode(data)) + except ValueError: + return err(newDOMException("Invalid character in string", + "InvalidCharacterError")) + +proc btoa*(data: string): DOMResult[string] = + # For some incomprehensible reason, this must return a DOMException + # for strings with chars not in latin-1. + #TODO this could be a lot more efficient if we just directly passed + # is_wide_char from QJS strings. + for r in data.runes: + if int32(r) <= 0xFF: + continue + return err(newDOMException("Invalid character in string", + "InvalidCharacterError")) + return ok(base64.encode(data)) |