encode.test.ts
This file
Path
src/utils/encode.test.ts
62 lines · 2.0 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 31d2ea4 point at it.
fix(jsx): use self closing tag only if element has no children (#2384)Taku Amanobefore adoption31d2ea4
lines 1–62why, one step back1/* eslint-disable @typescript-eslint/ban-ts-comment */2import { decodeBase64Url, encodeBase64Url } from './encode'34const toURLBase64 = (base64String: string): string =>5 base64String.replace(/\+|\//g, (m) => ({ '+': '-', '/': '_' }[m] ?? m))67const str2UInt8Array = (s: string): Uint8Array => {8 const buffer = new Uint8Array(new ArrayBuffer(s.length))9 for (let i = 0, len = buffer.byteLength; i < len; i++) {10 buffer[i] = s.charCodeAt(i)11 }12 return buffer13}1415describe('base64', () => {16 const utf8Encoder = new TextEncoder()17 describe.each([18 // basic19 [utf8Encoder.encode('Hello, 世界'), 'SGVsbG8sIOS4lueVjA=='],20 [utf8Encoder.encode('炎'), '54KO'],21 [utf8Encoder.encode('🔥'), '8J+UpQ=='],22 [23 utf8Encoder.encode('http://github.com/honojs/hono'),24 'aHR0cDovL2dpdGh1Yi5jb20vaG9ub2pzL2hvbm8=',25 ],2627 // RFC 3548 examples28 [str2UInt8Array('\x14\xfb\x9c\x03\xd9\x7e'), 'FPucA9l+'],29 [str2UInt8Array('\x14\xfb\x9c\x03\xd9'), 'FPucA9k='],30 [str2UInt8Array('\x14\xfb\x9c\x03'), 'FPucAw=='],3132 // RFC 4648 examples33 [str2UInt8Array(''), ''],34 [str2UInt8Array('f'), 'Zg=='],35 [str2UInt8Array('fo'), 'Zm8='],36 [str2UInt8Array('foo'), 'Zm9v'],37 [str2UInt8Array('foob'), 'Zm9vYg=='],38 [str2UInt8Array('fooba'), 'Zm9vYmE='],39 [str2UInt8Array('foobar'), 'Zm9vYmFy'],4041 // Wikipedia examples42 [str2UInt8Array('sure.'), 'c3VyZS4='],43 [str2UInt8Array('sure'), 'c3VyZQ=='],44 [str2UInt8Array('sur'), 'c3Vy'],45 [str2UInt8Array('su'), 'c3U='],46 [str2UInt8Array('leasure.'), 'bGVhc3VyZS4='],47 [str2UInt8Array('easure.'), 'ZWFzdXJlLg=='],48 [str2UInt8Array('asure.'), 'YXN1cmUu'],49 [str2UInt8Array('sure.'), 'c3VyZS4='],50 ])('%s, %s', (stdDecoded, stdEncoded) => {51 it('encode', () => {52 const got = encodeBase64Url(stdDecoded)53 const want = toURLBase64(stdEncoded)54 expect(got).toStrictEqual(want)55 })56 it('decode', () => {57 const got = decodeBase64Url(toURLBase64(stdEncoded))58 const want = stdDecoded59 expect(got).toStrictEqual(want)60 })61 })62})