Make ArbitraryBase Unicode-aware (#1299)

* Make ArbitraryBase Unicode-aware

https://mathiasbynens.be/notes/javascript-unicode#counting-symbols

* Fix performance bug and add Unicode test

* Add BigInt version and push output chars to array
This commit is contained in:
lionel-rowe
2023-02-23 22:34:47 +08:00
committed by GitHub
parent 6aa3314b93
commit 0c427580f1
2 changed files with 89 additions and 20 deletions

View File

@@ -1,4 +1,4 @@
import { convertArbitraryBase } from '../ArbitraryBase'
import { convertArbitraryBase, convertArbitraryBaseBigIntVersion } from '../ArbitraryBase'
test('Check the answer of convertArbitraryBase(98, 0123456789, 01234567) is 142', () => {
const res = convertArbitraryBase('98', '0123456789', '01234567')
@@ -34,3 +34,23 @@ test('Check the answer of convertArbitraryBase(111, 0123456789, abcdefgh) is bfh
const res = convertArbitraryBase('111', '0123456789', 'abcdefgh')
expect(res).toBe('bfh')
})
test('Unicode awareness', () => {
const res = convertArbitraryBase('98', '0123456789', '💝🎸🦄')
expect(res).toBe('🎸💝🎸🦄🦄')
})
test('zero', () => {
const res = convertArbitraryBase('0', '0123456789', 'abc')
expect(res).toBe('a')
})
test('BigInt version with input string of arbitrary length', () => {
const resBigIntVersion = convertArbitraryBaseBigIntVersion(
String(10n ** 100n),
'0123456789',
'0123456789abcdefghijklmnopqrstuvwxyz'
)
expect(resBigIntVersion).toBe((10n ** 100n).toString(36))
})