bigint-base 0.1.4 → 0.2.1
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/lib/convert-base-to-base.js +4 -8
- package/lib/convert-base-to-base.js.map +1 -1
- package/lib/convert-base-to-decimal.js +12 -7
- package/lib/convert-base-to-decimal.js.map +1 -1
- package/lib/convert-decimal-to-base.js +13 -6
- package/lib/convert-decimal-to-base.js.map +1 -1
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -19
- package/lib/index.js.map +1 -1
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +7 -0
- package/lib/utils.js.map +1 -0
- package/package.json +32 -30
- package/src/convert-base-to-base.ts +2 -2
- package/src/convert-base-to-decimal.ts +20 -2
- package/src/convert-decimal-to-base.ts +18 -1
- package/src/index.ts +3 -3
- package/src/utils.ts +7 -0
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const convert_decimal_to_base_1 = require("./convert-decimal-to-base");
|
|
5
|
-
const convert_base_to_decimal_1 = require("./convert-base-to-decimal");
|
|
6
|
-
function convertBaseToBase(...args) {
|
|
1
|
+
import { convertDecimalToBase } from './convert-decimal-to-base.js';
|
|
2
|
+
import { convertBaseToDecimal } from './convert-base-to-decimal.js';
|
|
3
|
+
export function convertBaseToBase(...args) {
|
|
7
4
|
if (args.length === 2) {
|
|
8
5
|
const [srcAlphabet, destAlphabet] = args;
|
|
9
6
|
return (val) => convertBaseToBase(srcAlphabet, destAlphabet, val);
|
|
10
7
|
}
|
|
11
8
|
const [srcAlphabet, destAlphabet, val] = args;
|
|
12
|
-
return
|
|
9
|
+
return convertDecimalToBase(destAlphabet, convertBaseToDecimal(srcAlphabet, val));
|
|
13
10
|
}
|
|
14
|
-
exports.convertBaseToBase = convertBaseToBase;
|
|
15
11
|
//# sourceMappingURL=convert-base-to-base.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-base-to-base.js","sourceRoot":"","sources":["../src/convert-base-to-base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert-base-to-base.js","sourceRoot":"","sources":["../src/convert-base-to-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAA;AAWnE,MAAM,UAAU,iBAAiB,CAAC,GAAG,IAEQ;IAE3C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,CAAC,WAAW,EAAE,YAAY,CAAC,GAAG,IAAI,CAAA;QACxC,OAAO,CAAC,GAAW,EAAE,EAAE,CAAC,iBAAiB,CAAC,WAAW,EAAE,YAAY,EAAE,GAAG,CAAC,CAAA;IAC3E,CAAC;IAED,MAAM,CAAC,WAAW,EAAE,YAAY,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;IAC7C,OAAO,oBAAoB,CAAC,YAAY,EAAE,oBAAoB,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAA;AACnF,CAAC"}
|
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function convertBaseToDecimal(...args) {
|
|
1
|
+
import { assert } from '@blackglory/prelude';
|
|
2
|
+
import { validateAlphabet } from './utils.js';
|
|
3
|
+
export function convertBaseToDecimal(...args) {
|
|
5
4
|
if (args.length === 1) {
|
|
6
5
|
const [alphabet] = args;
|
|
7
|
-
|
|
6
|
+
validateAlphabet(alphabet);
|
|
7
|
+
return (val) => _convertBaseToDecimal(alphabet, val);
|
|
8
8
|
}
|
|
9
9
|
const [alphabet, val] = args;
|
|
10
|
+
validateAlphabet(alphabet);
|
|
11
|
+
return _convertBaseToDecimal(alphabet, val);
|
|
12
|
+
}
|
|
13
|
+
function _convertBaseToDecimal(alphabet, val) {
|
|
10
14
|
const base = BigInt(alphabet.length);
|
|
11
15
|
let accumulator = 0n;
|
|
12
16
|
for (let i = val.length; i--;) {
|
|
13
17
|
const char = val[i];
|
|
14
18
|
const indexOfAlphabet = alphabet.indexOf(char);
|
|
15
|
-
|
|
19
|
+
assert(indexOfAlphabet >= 0, `The characer ${JSON.stringify(char)} is not in the alphabet`);
|
|
20
|
+
const currentValue = BigInt(indexOfAlphabet)
|
|
21
|
+
* (base ** BigInt(val.length - i - 1));
|
|
16
22
|
accumulator += currentValue;
|
|
17
23
|
}
|
|
18
24
|
return accumulator;
|
|
19
25
|
}
|
|
20
|
-
exports.convertBaseToDecimal = convertBaseToDecimal;
|
|
21
26
|
//# sourceMappingURL=convert-base-to-decimal.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-base-to-decimal.js","sourceRoot":"","sources":["../src/convert-base-to-decimal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert-base-to-decimal.js","sourceRoot":"","sources":["../src/convert-base-to-decimal.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAI7C,MAAM,UAAU,oBAAoB,CAAC,GAAG,IAEpB;IAElB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAA;QACvB,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAE1B,OAAO,CAAC,GAAW,EAAE,EAAE,CAAC,qBAAqB,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;IAC9D,CAAC;IAED,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;IAC5B,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAE1B,OAAO,qBAAqB,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;AAC7C,CAAC;AAED,SAAS,qBAAqB,CAAC,QAAgB,EAAE,GAAW;IAC1D,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IAEpC,IAAI,WAAW,GAAG,EAAE,CAAA;IACpB,KAAK,IAAI,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,CAAC;QAC9B,MAAM,IAAI,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;QACnB,MAAM,eAAe,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC9C,MAAM,CACJ,eAAe,IAAI,CAAC,EACpB,gBAAgB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,yBAAyB,CAC9D,CAAA;QAED,MAAM,YAAY,GAAG,MAAM,CAAC,eAAe,CAAC;cACvB,CAAC,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;QAEzD,WAAW,IAAI,YAAY,CAAA;IAC7B,CAAC;IACD,OAAO,WAAW,CAAA;AACpB,CAAC"}
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
function convertDecimalToBase(...args) {
|
|
1
|
+
import { assert } from '@blackglory/prelude';
|
|
2
|
+
import { validateAlphabet } from './utils.js';
|
|
3
|
+
export function convertDecimalToBase(...args) {
|
|
5
4
|
if (args.length === 1) {
|
|
6
5
|
const [alphabet] = args;
|
|
7
|
-
|
|
6
|
+
validateAlphabet(alphabet);
|
|
7
|
+
return (val) => _convertDecimalToBase(alphabet, val);
|
|
8
8
|
}
|
|
9
9
|
const [alphabet, val] = args;
|
|
10
|
+
validateAlphabet(alphabet);
|
|
11
|
+
return _convertDecimalToBase(alphabet, val);
|
|
12
|
+
}
|
|
13
|
+
function _convertDecimalToBase(alphabet, val) {
|
|
14
|
+
validateVal(val);
|
|
10
15
|
const base = BigInt(alphabet.length);
|
|
11
16
|
const result = [];
|
|
12
17
|
let temp = val;
|
|
@@ -18,5 +23,7 @@ function convertDecimalToBase(...args) {
|
|
|
18
23
|
.reverse()
|
|
19
24
|
.join('');
|
|
20
25
|
}
|
|
21
|
-
|
|
26
|
+
function validateVal(val) {
|
|
27
|
+
assert(val >= 0, 'The val must be greater than or equal to 0');
|
|
28
|
+
}
|
|
22
29
|
//# sourceMappingURL=convert-decimal-to-base.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"convert-decimal-to-base.js","sourceRoot":"","sources":["../src/convert-decimal-to-base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"convert-decimal-to-base.js","sourceRoot":"","sources":["../src/convert-decimal-to-base.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAI7C,MAAM,UAAU,oBAAoB,CAAC,GAAG,IAEpB;IAElB,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAA;QACvB,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAE1B,OAAO,CAAC,GAAW,EAAE,EAAE,CAAC,qBAAqB,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;IAC9D,CAAC;IAED,MAAM,CAAC,QAAQ,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;IAC5B,gBAAgB,CAAC,QAAQ,CAAC,CAAA;IAE1B,OAAO,qBAAqB,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAA;AAC7C,CAAC;AAED,SAAS,qBAAqB,CAAC,QAAgB,EAAE,GAAW;IAC1D,WAAW,CAAC,GAAG,CAAC,CAAA;IAEhB,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAA;IACpC,MAAM,MAAM,GAAa,EAAE,CAAA;IAE3B,IAAI,IAAI,GAAG,GAAG,CAAA;IACd,GAAG,CAAC;QACF,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC,CAAA;QACrC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAA;IAClC,CAAC,QAAQ,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,CAAC,KAAK,EAAE,EAAC;IAErC,OAAO,MAAM;SACV,OAAO,EAAE;SACT,IAAI,CAAC,EAAE,CAAC,CAAA;AACb,CAAC;AAED,SAAS,WAAW,CAAC,GAAW;IAC9B,MAAM,CAAC,GAAG,IAAI,CAAC,EAAE,4CAA4C,CAAC,CAAA;AAChE,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './convert-base-to-decimal';
|
|
2
|
-
export * from './convert-decimal-to-base';
|
|
3
|
-
export * from './convert-base-to-base';
|
|
1
|
+
export * from './convert-base-to-decimal.js';
|
|
2
|
+
export * from './convert-decimal-to-base.js';
|
|
3
|
+
export * from './convert-base-to-base.js';
|
package/lib/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./convert-base-to-decimal"), exports);
|
|
18
|
-
__exportStar(require("./convert-decimal-to-base"), exports);
|
|
19
|
-
__exportStar(require("./convert-base-to-base"), exports);
|
|
1
|
+
export * from './convert-base-to-decimal.js';
|
|
2
|
+
export * from './convert-decimal-to-base.js';
|
|
3
|
+
export * from './convert-base-to-base.js';
|
|
20
4
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,8BAA8B,CAAA;AAC5C,cAAc,8BAA8B,CAAA;AAC5C,cAAc,2BAA2B,CAAA"}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function validateAlphabet(alphabet: string): void;
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { assert } from '@blackglory/prelude';
|
|
2
|
+
import { toSet } from 'iterable-operator';
|
|
3
|
+
export function validateAlphabet(alphabet) {
|
|
4
|
+
assert(alphabet.length >= 2, 'The alphabet must have at least two characters');
|
|
5
|
+
assert(toSet(alphabet).size === alphabet.length, 'The alphabet must contain only unique characters');
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAEzC,MAAM,UAAU,gBAAgB,CAAC,QAAgB;IAC/C,MAAM,CAAC,QAAQ,CAAC,MAAM,IAAI,CAAC,EAAE,gDAAgD,CAAC,CAAA;IAC9E,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,IAAI,KAAK,QAAQ,CAAC,MAAM,EAAE,kDAAkD,CAAC,CAAA;AACtG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bigint-base",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Yet another base conversion library, but using BigInt.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bigint",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"lib",
|
|
11
11
|
"src"
|
|
12
12
|
],
|
|
13
|
+
"type": "module",
|
|
13
14
|
"main": "lib/index.js",
|
|
14
15
|
"types": "lib/index.d.ts",
|
|
15
16
|
"repository": "git@github.com:BlackGlory/bigint-base.git",
|
|
@@ -17,49 +18,50 @@
|
|
|
17
18
|
"license": "MIT",
|
|
18
19
|
"sideEffects": false,
|
|
19
20
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
21
|
+
"node": ">=22"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
24
|
+
"prepare": "ts-patch install -s",
|
|
23
25
|
"lint": "eslint --ext .js,.jsx,.ts,.tsx --quiet src __tests__",
|
|
24
|
-
"test": "
|
|
25
|
-
"
|
|
26
|
-
"test:coverage": "jest --coverage --config jest.config.js",
|
|
27
|
-
"prepublishOnly": "run-s clean build",
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"prepublishOnly": "run-s prepare clean build",
|
|
28
28
|
"clean": "rimraf lib",
|
|
29
|
-
"build": "
|
|
30
|
-
"
|
|
31
|
-
"build:patch": "tscpaths -p tsconfig.build.json -s ./src -o ./lib",
|
|
32
|
-
"bench": "ts-node --require tsconfig-paths/register benches/index.ts",
|
|
29
|
+
"build": "tsc --project tsconfig.build.json",
|
|
30
|
+
"bench": "tsx benches/index.ts",
|
|
33
31
|
"release": "standard-version"
|
|
34
32
|
},
|
|
35
33
|
"husky": {
|
|
36
34
|
"hooks": {
|
|
37
|
-
"pre-commit": "run-s lint build test",
|
|
35
|
+
"pre-commit": "run-s prepare lint build test",
|
|
38
36
|
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
|
|
39
37
|
}
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"@commitlint/
|
|
44
|
-
"@
|
|
45
|
-
"@types/
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"eslint": "^8.28.0",
|
|
50
|
-
"extra-benchmark": "^0.2.2",
|
|
51
|
-
"extra-generator": "^0.2.21",
|
|
40
|
+
"@commitlint/cli": "^20.4.1",
|
|
41
|
+
"@commitlint/config-conventional": "^20.4.1",
|
|
42
|
+
"@eslint/js": "^10.0.1",
|
|
43
|
+
"@types/node": "22",
|
|
44
|
+
"eslint": "^10.0.0",
|
|
45
|
+
"extra-benchmark": "^0.2.5",
|
|
46
|
+
"extra-generator": "^0.5.9",
|
|
52
47
|
"husky": "^4.3.8",
|
|
53
|
-
"jest": "^29.3.1",
|
|
54
48
|
"npm-run-all": "^4.1.5",
|
|
55
|
-
"
|
|
49
|
+
"return-style": "^3.0.1",
|
|
50
|
+
"rimraf": "^6.1.2",
|
|
56
51
|
"standard-version": "^9.5.0",
|
|
57
|
-
"ts-
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"typescript": "^
|
|
52
|
+
"ts-patch": "^3.3.0",
|
|
53
|
+
"tsconfig-paths": "^4.2.0",
|
|
54
|
+
"tslib": "^2.8.1",
|
|
55
|
+
"tsx": "^4.21.0",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"typescript-eslint": "^8.55.0",
|
|
58
|
+
"typescript-transform-paths": "^3.5.6",
|
|
59
|
+
"vite": "^7.3.1",
|
|
60
|
+
"vite-tsconfig-paths": "^6.1.1",
|
|
61
|
+
"vitest": "^4.0.18"
|
|
63
62
|
},
|
|
64
|
-
"dependencies": {
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@blackglory/prelude": "^0.4.0",
|
|
65
|
+
"iterable-operator": "^5.1.0"
|
|
66
|
+
}
|
|
65
67
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { convertDecimalToBase } from './convert-decimal-to-base'
|
|
2
|
-
import { convertBaseToDecimal } from './convert-base-to-decimal'
|
|
1
|
+
import { convertDecimalToBase } from './convert-decimal-to-base.js'
|
|
2
|
+
import { convertBaseToDecimal } from './convert-base-to-decimal.js'
|
|
3
3
|
|
|
4
4
|
export function convertBaseToBase(
|
|
5
5
|
srcAlphabet: string
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { assert } from '@blackglory/prelude'
|
|
2
|
+
import { validateAlphabet } from './utils.js'
|
|
3
|
+
|
|
1
4
|
export function convertBaseToDecimal(alphabet: string, val: string): bigint
|
|
2
5
|
export function convertBaseToDecimal(alphabet: string): (val: string) => bigint
|
|
3
6
|
export function convertBaseToDecimal(...args:
|
|
@@ -6,17 +9,32 @@ export function convertBaseToDecimal(...args:
|
|
|
6
9
|
) {
|
|
7
10
|
if (args.length === 1) {
|
|
8
11
|
const [alphabet] = args
|
|
9
|
-
|
|
12
|
+
validateAlphabet(alphabet)
|
|
13
|
+
|
|
14
|
+
return (val: string) => _convertBaseToDecimal(alphabet, val)
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
const [alphabet, val] = args
|
|
18
|
+
validateAlphabet(alphabet)
|
|
19
|
+
|
|
20
|
+
return _convertBaseToDecimal(alphabet, val)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function _convertBaseToDecimal(alphabet: string, val: string): bigint {
|
|
13
24
|
const base = BigInt(alphabet.length)
|
|
14
25
|
|
|
15
26
|
let accumulator = 0n
|
|
16
27
|
for (let i = val.length; i--;) {
|
|
17
28
|
const char = val[i]
|
|
18
29
|
const indexOfAlphabet = alphabet.indexOf(char)
|
|
19
|
-
|
|
30
|
+
assert(
|
|
31
|
+
indexOfAlphabet >= 0
|
|
32
|
+
, `The characer ${JSON.stringify(char)} is not in the alphabet`
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
const currentValue = BigInt(indexOfAlphabet)
|
|
36
|
+
* (base ** BigInt(val.length - i - 1))
|
|
37
|
+
|
|
20
38
|
accumulator += currentValue
|
|
21
39
|
}
|
|
22
40
|
return accumulator
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { assert } from '@blackglory/prelude'
|
|
2
|
+
import { validateAlphabet } from './utils.js'
|
|
3
|
+
|
|
1
4
|
export function convertDecimalToBase(alphabet: string, val: bigint): string
|
|
2
5
|
export function convertDecimalToBase(alphabet: string): (val: bigint) => string
|
|
3
6
|
export function convertDecimalToBase(...args:
|
|
@@ -6,10 +9,20 @@ export function convertDecimalToBase(...args:
|
|
|
6
9
|
) {
|
|
7
10
|
if (args.length === 1) {
|
|
8
11
|
const [alphabet] = args
|
|
9
|
-
|
|
12
|
+
validateAlphabet(alphabet)
|
|
13
|
+
|
|
14
|
+
return (val: bigint) => _convertDecimalToBase(alphabet, val)
|
|
10
15
|
}
|
|
11
16
|
|
|
12
17
|
const [alphabet, val] = args
|
|
18
|
+
validateAlphabet(alphabet)
|
|
19
|
+
|
|
20
|
+
return _convertDecimalToBase(alphabet, val)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function _convertDecimalToBase(alphabet: string, val: bigint): string {
|
|
24
|
+
validateVal(val)
|
|
25
|
+
|
|
13
26
|
const base = BigInt(alphabet.length)
|
|
14
27
|
const result: string[] = []
|
|
15
28
|
|
|
@@ -23,3 +36,7 @@ export function convertDecimalToBase(...args:
|
|
|
23
36
|
.reverse()
|
|
24
37
|
.join('')
|
|
25
38
|
}
|
|
39
|
+
|
|
40
|
+
function validateVal(val: bigint): void {
|
|
41
|
+
assert(val >= 0, 'The val must be greater than or equal to 0')
|
|
42
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './convert-base-to-decimal'
|
|
2
|
-
export * from './convert-decimal-to-base'
|
|
3
|
-
export * from './convert-base-to-base'
|
|
1
|
+
export * from './convert-base-to-decimal.js'
|
|
2
|
+
export * from './convert-decimal-to-base.js'
|
|
3
|
+
export * from './convert-base-to-base.js'
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { assert } from '@blackglory/prelude'
|
|
2
|
+
import { toSet } from 'iterable-operator'
|
|
3
|
+
|
|
4
|
+
export function validateAlphabet(alphabet: string): void {
|
|
5
|
+
assert(alphabet.length >= 2, 'The alphabet must have at least two characters')
|
|
6
|
+
assert(toSet(alphabet).size === alphabet.length, 'The alphabet must contain only unique characters')
|
|
7
|
+
}
|