-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.ts
38 lines (36 loc) · 1.4 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { NumberSystem } from ".";
import * as util from "./util";
/** Creates a new number system object with the given radix/base. */
export function base(radix: number): NumberSystem {
const rdx = Math.floor(Math.min(36, Math.max(2, radix)));
const max = (255).toString(rdx).length;
const charset = "0123456789abcdefghijklmnopqrstuvwxyz".slice(0, rdx);
return {
charset,
radix: rdx,
regex: new RegExp(`[${charset}]`, "gi"),
encode(data, encoding = "utf8") {
// For each byte of data, encode it into a character.
return Buffer.from(data.toString(encoding), encoding)
.toJSON()
.data.map(x => x.toString(rdx).padStart(max, "0"))
.join("");
},
decode(data, encoding) {
// For each character of data, decode it into a byte.
const decoded = Buffer.from(
util
.padStart(
(
data.toString().toLowerCase().match(this.regex) ??
[]
).join(""),
max
)
.map(x => parseInt(x, rdx))
);
// Return the decoded buffer, or a string if a character encoding is provided.
return encoding ? decoded.toString(encoding) : decoded;
}
};
}