@pfeiferio/custom-base 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +112 -0
- package/dist/Charset.d.ts +30 -0
- package/dist/Charset.d.ts.map +1 -0
- package/dist/Charset.js +55 -0
- package/dist/Charset.js.map +1 -0
- package/dist/decodeBigInt.d.ts +12 -0
- package/dist/decodeBigInt.d.ts.map +1 -0
- package/dist/decodeBigInt.js +28 -0
- package/dist/decodeBigInt.js.map +1 -0
- package/dist/decodeNumber.d.ts +14 -0
- package/dist/decodeNumber.d.ts.map +1 -0
- package/dist/decodeNumber.js +30 -0
- package/dist/decodeNumber.js.map +1 -0
- package/dist/encodeBigInt.d.ts +11 -0
- package/dist/encodeBigInt.d.ts.map +1 -0
- package/dist/encodeBigInt.js +26 -0
- package/dist/encodeBigInt.js.map +1 -0
- package/dist/encodeNumber.d.ts +12 -0
- package/dist/encodeNumber.d.ts.map +1 -0
- package/dist/encodeNumber.js +26 -0
- package/dist/encodeNumber.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Pascal Pfeifer <pascal@pfeifer.zone>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# @pfeiferio/custom-base
|
|
2
|
+
|
|
3
|
+
Custom base encoding and decoding for numbers and BigInts using arbitrary charsets.
|
|
4
|
+
|
|
5
|
+
This library allows you to encode and decode numeric values using **any character set**.
|
|
6
|
+
It supports both JavaScript `number` and `bigint` types and is fully ESM-based with no dependencies.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## Features
|
|
11
|
+
|
|
12
|
+
- Encode / decode `number` and `bigint`
|
|
13
|
+
- Arbitrary custom charsets
|
|
14
|
+
- Built-in presets (base2, base16, base42, base128)
|
|
15
|
+
- Cached and validated charset handling
|
|
16
|
+
- Zero dependencies
|
|
17
|
+
- Native Node.js test runner
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @pfeiferio/custom-base
|
|
25
|
+
````
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### Encoding and decoding numbers
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
import { encodeNumber, decodeNumber, presets } from '@pfeiferio/custom-base'
|
|
35
|
+
|
|
36
|
+
const encoded = encodeNumber(255, presets.base16)
|
|
37
|
+
const decoded = decodeNumber(encoded, presets.base16)
|
|
38
|
+
|
|
39
|
+
console.log(encoded) // "ff"
|
|
40
|
+
console.log(decoded) // 255
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
### Encoding and decoding BigInts
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { encodeBigInt, decodeBigInt, presets } from '@pfeiferio/custom-base'
|
|
49
|
+
|
|
50
|
+
const value = 12345678901234567890n
|
|
51
|
+
|
|
52
|
+
const encoded = encodeBigInt(value, presets.base42)
|
|
53
|
+
const decoded = decodeBigInt(encoded, presets.base42)
|
|
54
|
+
|
|
55
|
+
console.log(decoded === value) // true
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### Custom charsets
|
|
61
|
+
|
|
62
|
+
You can provide your own charset as a string:
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import { encodeNumber } from '@pfeiferio/custom-base'
|
|
66
|
+
|
|
67
|
+
const charset = 'ABCDEFG123456'
|
|
68
|
+
const encoded = encodeNumber(1337, charset)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
### Charset normalization
|
|
74
|
+
|
|
75
|
+
Internally, charsets are compiled, validated and cached.
|
|
76
|
+
You can normalize inputs manually if needed:
|
|
77
|
+
|
|
78
|
+
```js
|
|
79
|
+
import { ensureCharset, presets } from '@pfeiferio/custom-base'
|
|
80
|
+
|
|
81
|
+
const charset = ensureCharset(presets.base2)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
The returned `Charset` object is immutable and guaranteed to be valid.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Presets
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
import { presets } from '@pfeiferio/custom-base'
|
|
92
|
+
|
|
93
|
+
presets.base2
|
|
94
|
+
presets.base16
|
|
95
|
+
presets.base42
|
|
96
|
+
presets.base128
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
All presets are non-standard and provided purely for convenience.
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Node.js Support
|
|
104
|
+
|
|
105
|
+
* Node.js ≥ 18
|
|
106
|
+
* ESM only
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type Presets = {
|
|
2
|
+
readonly base128: string;
|
|
3
|
+
readonly base42: string;
|
|
4
|
+
readonly base2: string;
|
|
5
|
+
readonly base16: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const presets: Presets;
|
|
8
|
+
interface CharsMapPartial<T> {
|
|
9
|
+
readonly map: Readonly<Record<string, T>>;
|
|
10
|
+
readonly base: T;
|
|
11
|
+
}
|
|
12
|
+
export interface Charset {
|
|
13
|
+
readonly chars: string;
|
|
14
|
+
readonly int: CharsMapPartial<number>;
|
|
15
|
+
readonly bigInt: CharsMapPartial<bigint>;
|
|
16
|
+
/** internal brand – do not rely on */
|
|
17
|
+
readonly [charsetBrand]: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare const charsetBrand: unique symbol;
|
|
20
|
+
export declare const isCharset: (map: any) => map is Charset;
|
|
21
|
+
export declare const ensureCharset: (charset: string | Charset) => Charset;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a map where each character in the given character set is assigned its index within the set.
|
|
24
|
+
* This is useful for converting characters to their numeric values in custom numeral systems.
|
|
25
|
+
* @param {string} chars - The character set to map.
|
|
26
|
+
* @returns {Charset}
|
|
27
|
+
*/
|
|
28
|
+
export declare const createCharsMap: (chars: string) => Charset;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=Charset.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Charset.d.ts","sourceRoot":"","sources":["../src/Charset.ts"],"names":[],"mappings":"AAAA,KAAK,OAAO,GAAG;IACb,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB,CAAA;AACD,eAAO,MAAM,OAAO,EAAE,OAMrB,CAAA;AAKD,UAAU,eAAe,CAAC,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAA;IACzC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAA;CACjB;AAED,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;IACtB,QAAQ,CAAC,GAAG,EAAE,eAAe,CAAC,MAAM,CAAC,CAAA;IACrC,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAC,MAAM,CAAC,CAAA;IAExC,sCAAsC;IACtC,QAAQ,CAAC,CAAC,YAAY,CAAC,EAAE,OAAO,CAAA;CACjC;AAED,QAAA,MAAM,YAAY,EAAE,OAAO,MAAiB,CAAA;AAG5C,eAAO,MAAM,SAAS,GAAI,KAAK,GAAG,KAAG,GAAG,IAAI,OAE3C,CAAA;AAGD,eAAO,MAAM,aAAa,GAAI,SAAS,MAAM,GAAG,OAAO,KAAG,OAGzD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,cAAc,GAAI,OAAO,MAAM,KAAG,OA+B9C,CAAA"}
|
package/dist/Charset.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export const presets = Object.freeze({
|
|
2
|
+
base128: '0123456789!"#$%&\'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃ',
|
|
3
|
+
base42: 'ktTnDFVJHM3Nm2SQCqd51g9XcWl70LwrjfhRKGs4v8',
|
|
4
|
+
base2: '01',
|
|
5
|
+
base16: '0123456789abcdef'
|
|
6
|
+
});
|
|
7
|
+
// Object to cache character maps for different character sets.
|
|
8
|
+
const charsetMap = {};
|
|
9
|
+
const charsetBrand = Symbol();
|
|
10
|
+
export const isCharset = (map) => {
|
|
11
|
+
return map?.[charsetBrand] === true;
|
|
12
|
+
};
|
|
13
|
+
export const ensureCharset = (charset) => {
|
|
14
|
+
if (isCharset(charset))
|
|
15
|
+
return charset;
|
|
16
|
+
return charsetMap[charset] ??= createCharsMap(charset);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Creates a map where each character in the given character set is assigned its index within the set.
|
|
20
|
+
* This is useful for converting characters to their numeric values in custom numeral systems.
|
|
21
|
+
* @param {string} chars - The character set to map.
|
|
22
|
+
* @returns {Charset}
|
|
23
|
+
*/
|
|
24
|
+
export const createCharsMap = (chars) => {
|
|
25
|
+
const mapInt = {};
|
|
26
|
+
const mapBigInt = {};
|
|
27
|
+
const base = chars.length;
|
|
28
|
+
for (let i = 0; i < base; i++) {
|
|
29
|
+
mapInt[chars[i]] = i;
|
|
30
|
+
mapBigInt[chars[i]] = BigInt(i);
|
|
31
|
+
}
|
|
32
|
+
const result = {
|
|
33
|
+
chars,
|
|
34
|
+
int: {
|
|
35
|
+
map: mapInt,
|
|
36
|
+
base
|
|
37
|
+
},
|
|
38
|
+
bigInt: {
|
|
39
|
+
map: mapBigInt,
|
|
40
|
+
base: BigInt(base)
|
|
41
|
+
},
|
|
42
|
+
[charsetBrand]: true
|
|
43
|
+
};
|
|
44
|
+
Object.defineProperty(result, charsetBrand, {
|
|
45
|
+
value: true,
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: false
|
|
48
|
+
});
|
|
49
|
+
return charsetMap[chars] = Object.freeze(result);
|
|
50
|
+
};
|
|
51
|
+
ensureCharset(presets.base2);
|
|
52
|
+
ensureCharset(presets.base16);
|
|
53
|
+
ensureCharset(presets.base42);
|
|
54
|
+
ensureCharset(presets.base128);
|
|
55
|
+
//# sourceMappingURL=Charset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Charset.js","sourceRoot":"","sources":["../src/Charset.ts"],"names":[],"mappings":"AAMA,MAAM,CAAC,MAAM,OAAO,GAAY,MAAM,CAAC,MAAM,CAAC;IAC1C,OAAO,EAAE,kIAAkI;IAC3I,MAAM,EAAE,4CAA4C;IACpD,KAAK,EAAE,IAAI;IACX,MAAM,EAAE,kBAAkB;CAC3B,CACF,CAAA;AAED,+DAA+D;AAC/D,MAAM,UAAU,GAA4B,EAAE,CAAA;AAgB9C,MAAM,YAAY,GAAkB,MAAM,EAAE,CAAA;AAG5C,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAQ,EAAkB,EAAE;IACpD,OAAO,GAAG,EAAE,CAAC,YAAY,CAAC,KAAK,IAAI,CAAA;AACrC,CAAC,CAAA;AAGD,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,OAAyB,EAAW,EAAE;IAClE,IAAI,SAAS,CAAC,OAAO,CAAC;QAAE,OAAO,OAAO,CAAA;IACtC,OAAO,UAAU,CAAC,OAAO,CAAC,KAAK,cAAc,CAAC,OAAO,CAAC,CAAA;AACxD,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAW,EAAE;IAEvD,MAAM,MAAM,GAA2B,EAAE,CAAA;IACzC,MAAM,SAAS,GAA2B,EAAE,CAAA;IAC5C,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAA;IAEzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC;QAC9B,MAAM,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,GAAG,CAAC,CAAA;QACrB,SAAS,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAA;IAClC,CAAC;IAED,MAAM,MAAM,GAAG;QACb,KAAK;QACL,GAAG,EAAE;YACH,GAAG,EAAE,MAAM;YACX,IAAI;SACL;QACD,MAAM,EAAE;YACN,GAAG,EAAE,SAAS;YACd,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC;SACnB;QACD,CAAC,YAAY,CAAC,EAAE,IAAI;KACrB,CAAA;IAED,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE;QAC1C,KAAK,EAAE,IAAI;QACX,UAAU,EAAE,KAAK;QACjB,YAAY,EAAE,KAAK;KACpB,CAAC,CAAA;IAEF,OAAO,UAAU,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;AAClD,CAAC,CAAA;AAED,aAAa,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC5B,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;AAC7B,aAAa,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;AAC7B,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Charset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string representation in a custom base back to its integer form as a BigInt.
|
|
4
|
+
* This function is essential for handling very large numbers that exceed the safe integer limit of Number in JavaScript.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} encodedString - The string in the custom base to be converted back to a BigInt.
|
|
7
|
+
* @param {string} charset - The charset used for the custom base, matching the one used for conversion.
|
|
8
|
+
* @return {bigint} The BigInt value represented by the custom base string.
|
|
9
|
+
* @throws {Error} If an invalid character (not found in charset) is encountered in the input string.
|
|
10
|
+
*/
|
|
11
|
+
export declare const decodeBigInt: (encodedString: string, charset: string | Charset) => bigint;
|
|
12
|
+
//# sourceMappingURL=decodeBigInt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeBigInt.d.ts","sourceRoot":"","sources":["../src/decodeBigInt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,OAAO,EAAgB,MAAM,cAAc,CAAC;AAEzD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,GAAI,eAAe,MAAM,EAAE,SAAS,MAAM,GAAG,OAAO,KAAG,MAiB/E,CAAA"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ensureCharset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string representation in a custom base back to its integer form as a BigInt.
|
|
4
|
+
* This function is essential for handling very large numbers that exceed the safe integer limit of Number in JavaScript.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} encodedString - The string in the custom base to be converted back to a BigInt.
|
|
7
|
+
* @param {string} charset - The charset used for the custom base, matching the one used for conversion.
|
|
8
|
+
* @return {bigint} The BigInt value represented by the custom base string.
|
|
9
|
+
* @throws {Error} If an invalid character (not found in charset) is encountered in the input string.
|
|
10
|
+
*/
|
|
11
|
+
export const decodeBigInt = (encodedString, charset) => {
|
|
12
|
+
charset = ensureCharset(charset);
|
|
13
|
+
const { map, base } = charset.bigInt;
|
|
14
|
+
let currentMultiplier = 1n;
|
|
15
|
+
const customBaseStringAsArray = encodedString.split('');
|
|
16
|
+
let result = 0n;
|
|
17
|
+
let length = encodedString.length - 1;
|
|
18
|
+
for (let i = length; i >= 0; i -= 1) {
|
|
19
|
+
const char = customBaseStringAsArray[i];
|
|
20
|
+
if (map[char] === undefined) {
|
|
21
|
+
throw new Error(`Invalid character "${customBaseStringAsArray[i]}" in input string.`);
|
|
22
|
+
}
|
|
23
|
+
result += currentMultiplier * map[char];
|
|
24
|
+
currentMultiplier *= base;
|
|
25
|
+
}
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=decodeBigInt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeBigInt.js","sourceRoot":"","sources":["../src/decodeBigInt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,aAAa,EAAC,MAAM,cAAc,CAAC;AAEzD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,aAAqB,EAAE,OAAyB,EAAU,EAAE;IAEvF,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;IAChC,MAAM,EAAC,GAAG,EAAE,IAAI,EAAC,GAAG,OAAO,CAAC,MAAM,CAAA;IAClC,IAAI,iBAAiB,GAAG,EAAE,CAAA;IAC1B,MAAM,uBAAuB,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACvD,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,IAAI,MAAM,GAAG,aAAa,CAAC,MAAM,GAAG,CAAC,CAAA;IACrC,KAAK,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,GAAW,uBAAuB,CAAC,CAAC,CAAE,CAAA;QAChD,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,sBAAsB,uBAAuB,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC;QACxF,CAAC;QACD,MAAM,IAAI,iBAAiB,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;QACvC,iBAAiB,IAAI,IAAI,CAAA;IAC3B,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Charset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string representation in a custom base back to its integer form.
|
|
4
|
+
* This function reverses the conversion process of `convertIntToCustomBase`, turning a custom base
|
|
5
|
+
* string back into a standard integer. It's essential for the input string to use the same charset
|
|
6
|
+
* as used for the conversion to the custom base.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} encodedString - The string in the custom base to be converted back to an integer.
|
|
9
|
+
* @param {string|Charset} charset - The charset used for the custom base, matching the one used for conversion.
|
|
10
|
+
* @return {number} The integer value represented by the custom base string.
|
|
11
|
+
* @throws {Error} If an invalid character (not found in charset) is encountered in the input string.
|
|
12
|
+
*/
|
|
13
|
+
export declare const decodeNumber: (encodedString: string, charset: string | Charset) => number;
|
|
14
|
+
//# sourceMappingURL=decodeNumber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeNumber.d.ts","sourceRoot":"","sources":["../src/decodeNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,OAAO,EAAgB,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,eAAe,MAAM,EAAE,SAAS,MAAM,GAAG,OAAO,KAAG,MAiB/E,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ensureCharset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string representation in a custom base back to its integer form.
|
|
4
|
+
* This function reverses the conversion process of `convertIntToCustomBase`, turning a custom base
|
|
5
|
+
* string back into a standard integer. It's essential for the input string to use the same charset
|
|
6
|
+
* as used for the conversion to the custom base.
|
|
7
|
+
*
|
|
8
|
+
* @param {string} encodedString - The string in the custom base to be converted back to an integer.
|
|
9
|
+
* @param {string|Charset} charset - The charset used for the custom base, matching the one used for conversion.
|
|
10
|
+
* @return {number} The integer value represented by the custom base string.
|
|
11
|
+
* @throws {Error} If an invalid character (not found in charset) is encountered in the input string.
|
|
12
|
+
*/
|
|
13
|
+
export const decodeNumber = (encodedString, charset) => {
|
|
14
|
+
charset = ensureCharset(charset);
|
|
15
|
+
const { map, base } = charset.int;
|
|
16
|
+
let currentMultiplier = 1;
|
|
17
|
+
const customBaseStringAsArray = encodedString.split('');
|
|
18
|
+
let result = 0;
|
|
19
|
+
let length = encodedString.length - 1;
|
|
20
|
+
for (let i = length; i >= 0; i -= 1) {
|
|
21
|
+
const char = customBaseStringAsArray[i];
|
|
22
|
+
if (map[char] === undefined) {
|
|
23
|
+
throw new Error(`Invalid character "${customBaseStringAsArray[i]}" in input string.`);
|
|
24
|
+
}
|
|
25
|
+
result += currentMultiplier * map[char];
|
|
26
|
+
currentMultiplier *= base;
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=decodeNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decodeNumber.js","sourceRoot":"","sources":["../src/decodeNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,aAAa,EAAC,MAAM,cAAc,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,aAAqB,EAAE,OAAyB,EAAU,EAAE;IAEvF,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;IAChC,MAAM,EAAC,GAAG,EAAE,IAAI,EAAC,GAAG,OAAO,CAAC,GAAG,CAAA;IAC/B,IAAI,iBAAiB,GAAG,CAAC,CAAA;IACzB,MAAM,uBAAuB,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACvD,IAAI,MAAM,GAAG,CAAC,CAAA;IACd,IAAI,MAAM,GAAG,aAAa,CAAC,MAAM,GAAG,CAAC,CAAA;IACrC,KAAK,IAAI,CAAC,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,GAAW,uBAAuB,CAAC,CAAC,CAAE,CAAA;QAChD,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;YAC5B,MAAM,IAAI,KAAK,CAAC,sBAAsB,uBAAuB,CAAC,CAAC,CAAC,oBAAoB,CAAC,CAAC;QACxF,CAAC;QACD,MAAM,IAAI,iBAAiB,GAAG,GAAG,CAAC,IAAI,CAAC,CAAA;QACvC,iBAAiB,IAAI,IAAI,CAAA;IAC3B,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Charset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a bigint to a string representation in a custom base using a predefined charset.
|
|
4
|
+
* This function allows converting big integers into a string format based on any custom numeric system,
|
|
5
|
+
* as long as the charset provides the symbols for that base.
|
|
6
|
+
* @param {bigint} number
|
|
7
|
+
* @param charset
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export declare const encodeBigInt: (number: bigint, charset: string | Charset) => string;
|
|
11
|
+
//# sourceMappingURL=encodeBigInt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeBigInt.d.ts","sourceRoot":"","sources":["../src/encodeBigInt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,OAAO,EAAgB,MAAM,cAAc,CAAC;AAEzD;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,SAAS,MAAM,GAAG,OAAO,KAAG,MAkBxE,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ensureCharset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts a bigint to a string representation in a custom base using a predefined charset.
|
|
4
|
+
* This function allows converting big integers into a string format based on any custom numeric system,
|
|
5
|
+
* as long as the charset provides the symbols for that base.
|
|
6
|
+
* @param {bigint} number
|
|
7
|
+
* @param charset
|
|
8
|
+
* @returns {string}
|
|
9
|
+
*/
|
|
10
|
+
export const encodeBigInt = (number, charset) => {
|
|
11
|
+
charset = ensureCharset(charset);
|
|
12
|
+
const base = charset.bigInt.base;
|
|
13
|
+
const characters = charset.chars;
|
|
14
|
+
let result = '';
|
|
15
|
+
if (number < base) {
|
|
16
|
+
return characters.charAt(Number(number));
|
|
17
|
+
}
|
|
18
|
+
while (number > 0) {
|
|
19
|
+
const index = number % base;
|
|
20
|
+
result = characters.charAt(Number(index)) + result;
|
|
21
|
+
number /= base;
|
|
22
|
+
// number = Math.floor(number / base);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=encodeBigInt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeBigInt.js","sourceRoot":"","sources":["../src/encodeBigInt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,aAAa,EAAC,MAAM,cAAc,CAAC;AAEzD;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAc,EAAE,OAAyB,EAAU,EAAE;IAEhF,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;IAChC,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAA;IAChC,MAAM,UAAU,GAAG,OAAO,CAAC,KAAK,CAAA;IAChC,IAAI,MAAM,GAAG,EAAE,CAAA;IAEf,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;QAClB,OAAO,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;IAC1C,CAAC;IAED,OAAO,MAAM,GAAG,CAAC,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC;QAC5B,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,GAAG,MAAM,CAAA;QAClD,MAAM,IAAI,IAAI,CAAC;QACf,sCAAsC;IACxC,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Charset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts an integer to a string representation in a custom base using a predefined charset.
|
|
4
|
+
* This function allows converting numbers into a string format based on any custom numeric system,
|
|
5
|
+
* as long as the charset provides the symbols for that base.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} number - The integer value to be converted into the custom base.
|
|
8
|
+
* @param {string} charset - A string representing all the symbols used in the custom base, ordered from 0 upwards.
|
|
9
|
+
* @return {string} The string representation of the integer in the custom base.
|
|
10
|
+
*/
|
|
11
|
+
export declare const encodeNumber: (number: number, charset: string | Charset) => string;
|
|
12
|
+
//# sourceMappingURL=encodeNumber.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeNumber.d.ts","sourceRoot":"","sources":["../src/encodeNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,OAAO,EAAY,MAAM,cAAc,CAAC;AAErD;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,GAAI,QAAQ,MAAM,EAAE,SAAS,MAAM,GAAG,OAAO,KAAG,MAkBxE,CAAA"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isCharset } from "./Charset.js";
|
|
2
|
+
/**
|
|
3
|
+
* Converts an integer to a string representation in a custom base using a predefined charset.
|
|
4
|
+
* This function allows converting numbers into a string format based on any custom numeric system,
|
|
5
|
+
* as long as the charset provides the symbols for that base.
|
|
6
|
+
*
|
|
7
|
+
* @param {number} number - The integer value to be converted into the custom base.
|
|
8
|
+
* @param {string} charset - A string representing all the symbols used in the custom base, ordered from 0 upwards.
|
|
9
|
+
* @return {string} The string representation of the integer in the custom base.
|
|
10
|
+
*/
|
|
11
|
+
export const encodeNumber = (number, charset) => {
|
|
12
|
+
const isMap = isCharset(charset);
|
|
13
|
+
const base = isMap ? charset.int.base : charset.length;
|
|
14
|
+
const characters = isMap ? charset.chars : charset;
|
|
15
|
+
let result = '';
|
|
16
|
+
if (number < base) {
|
|
17
|
+
return characters.charAt(number);
|
|
18
|
+
}
|
|
19
|
+
while (number > 0) {
|
|
20
|
+
const index = number % base;
|
|
21
|
+
result = characters.charAt(index) + result;
|
|
22
|
+
number = Math.floor(number / base);
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=encodeNumber.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encodeNumber.js","sourceRoot":"","sources":["../src/encodeNumber.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,SAAS,EAAC,MAAM,cAAc,CAAC;AAErD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAc,EAAE,OAAyB,EAAU,EAAE;IAEhF,MAAM,KAAK,GAAG,SAAS,CAAC,OAAO,CAAC,CAAA;IAChC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAA;IACtD,MAAM,UAAU,GAAG,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAA;IAElD,IAAI,MAAM,GAAG,EAAE,CAAA;IAEf,IAAI,MAAM,GAAG,IAAI,EAAE,CAAC;QAClB,OAAO,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAClC,CAAC;IAED,OAAO,MAAM,GAAG,CAAC,EAAE,CAAC;QAClB,MAAM,KAAK,GAAG,MAAM,GAAG,IAAI,CAAC;QAC5B,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAA;QAC1C,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IACrC,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC,CAAA"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ensureCharset, presets } from "./Charset.js";
|
|
2
|
+
export { encodeNumber } from './encodeNumber.js';
|
|
3
|
+
export { encodeBigInt } from './encodeBigInt.js';
|
|
4
|
+
export { decodeNumber } from './decodeNumber.js';
|
|
5
|
+
export { decodeBigInt } from './decodeBigInt.js';
|
|
6
|
+
export type { Charset } from "./Charset.js";
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AACpD,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,YAAY,EAAC,OAAO,EAAC,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { ensureCharset, presets } from "./Charset.js";
|
|
2
|
+
export { encodeNumber } from './encodeNumber.js';
|
|
3
|
+
export { encodeBigInt } from './encodeBigInt.js';
|
|
4
|
+
export { decodeNumber } from './decodeNumber.js';
|
|
5
|
+
export { decodeBigInt } from './decodeBigInt.js';
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,aAAa,EAAE,OAAO,EAAC,MAAM,cAAc,CAAC;AACpD,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAC9C,OAAO,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pfeiferio/custom-base",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Custom base encoding and decoding for numbers and BigInts using arbitrary charsets",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Pascal Pfeifer <pascal@pfeifer.zone>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"base",
|
|
23
|
+
"base-encoding",
|
|
24
|
+
"custom-base",
|
|
25
|
+
"charset",
|
|
26
|
+
"encoding",
|
|
27
|
+
"decoding",
|
|
28
|
+
"bigint",
|
|
29
|
+
"number",
|
|
30
|
+
"id",
|
|
31
|
+
"token",
|
|
32
|
+
"utility",
|
|
33
|
+
"no-dependencies",
|
|
34
|
+
"esm"
|
|
35
|
+
],
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/pfeiferio/custom-base.git"
|
|
39
|
+
},
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/pfeiferio/custom-base/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/pfeiferio/custom-base#readme",
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.0.0"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsc",
|
|
49
|
+
"test": "npm run build && node --test",
|
|
50
|
+
"test:watch": "node --test --watch",
|
|
51
|
+
"test:coverage": "npm run build && node --test --experimental-test-coverage",
|
|
52
|
+
"prepublishOnly": "npm run build && npm test",
|
|
53
|
+
"clean": "rm -rf dist"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@types/node": "^20.17.9",
|
|
57
|
+
"typescript": "^5.9.3"
|
|
58
|
+
}
|
|
59
|
+
}
|