Skip to content

Commit fa58a81

Browse files
committed
fix: use TextEncoder fallbacks where possible
1 parent b29221b commit fa58a81

File tree

4 files changed

+27
-7
lines changed

4 files changed

+27
-7
lines changed

src/common/data/basex-secure.spec.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { base64 } from './basex-secure'
1+
import { base64, base32agnoster } from './basex-secure'
22

33
describe('baseX-secure', () => {
44

@@ -10,4 +10,12 @@ describe('baseX-secure', () => {
1010
expect(base64.encode(bin)).toBe(btoa(str))
1111
expect(base64.decode(btoa(str))).toEqual(bin)
1212
})
13+
14+
it('should support 32-agnoster like for Swift', () => {
15+
let originalString = "Hello"
16+
let data = new TextEncoder().encode(originalString)
17+
let encodedString = base32agnoster.encode(data)
18+
expect(encodedString).toBe("91jprv3f")
19+
})
20+
1321
})

src/common/data/basex-secure.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */
22

3+
import { stringToUInt8Array, Uint8ArrayToString } from "./bin"
4+
35
// Utilities
46
/**
57
* @__NO_SIDE_EFFECTS__
@@ -347,6 +349,11 @@ export const base32hexnopad: BytesCoder = /* @__PURE__ */ chain(
347349
alphabet('0123456789ABCDEFGHIJKLMNOPQRSTUV'),
348350
join('')
349351
);
352+
export const base32agnoster: BytesCoder = /* @__PURE__ */ chain(
353+
radix2(5),
354+
alphabet('0123456789abcdefghjkmnpqrtuvwxyz'),
355+
join('')
356+
);
350357
export const base32crockford: BytesCoder = /* @__PURE__ */ chain(
351358
radix2(5),
352359
alphabet('0123456789ABCDEFGHJKMNPQRSTVWXYZ'),
@@ -574,12 +581,9 @@ function genBech32(encoding: 'bech32' | 'bech32m'): Bech32 {
574581
export const bech32: Bech32 = /* @__PURE__ */ genBech32('bech32');
575582
export const bech32m: Bech32 = /* @__PURE__ */ genBech32('bech32m');
576583

577-
declare const TextEncoder: any;
578-
declare const TextDecoder: any;
579-
580584
export const utf8: BytesCoder = {
581-
encode: (data) => new TextDecoder().decode(data),
582-
decode: (str) => new TextEncoder().encode(str),
585+
encode: (data) => Uint8ArrayToString(data),
586+
decode: (str) => stringToUInt8Array(str),
583587
};
584588

585589
export const hex: BytesCoder = /* @__PURE__ */ chain(

src/common/data/basex.spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ describe('baseX', () => {
109109
expect(decode('o1y89')).toEqual(new Uint8Array([1, 9, 0, 255]))
110110
})
111111

112+
it('should support 32-agnoster like for Swift', () => {
113+
const { encode, decode } = useBase('32-agnoster')
114+
let originalString = "Hello"
115+
let data = new TextEncoder().encode(originalString)
116+
let encodedString = encode(data)
117+
expect(encodedString).toBe("91jprv3f")
118+
})
119+
112120
// it('should do the same as atob and btoa', () => {
113121
// const { encode, decode } = useBase(64)
114122
// const str = 'Hello, World!'

src/common/data/bin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* eslint-disable no-control-regex */
33
/* eslint-disable prefer-spread */
44

5-
import { base64, base64nopad, base64urlnopad, str } from './basex-secure'
5+
import { base64, base64nopad, base64urlnopad } from './basex-secure'
66
import { jsonStringifySafe } from './json'
77

88
export type BinInput = Uint8Array | ArrayBuffer | string | number[]

0 commit comments

Comments
 (0)