@gsknnft/bigint-buffer 1.4.3 → 1.4.5
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/README.md +98 -89
- package/build/Release/bigint_buffer.iobj +0 -0
- package/build/Release/bigint_buffer.ipdb +0 -0
- package/build/Release/bigint_buffer.node +0 -0
- package/build/Release/bigint_buffer.pdb +0 -0
- package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
- package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
- package/build/binding.sln +3 -3
- package/build/conversion/index.js +18 -0
- package/build/conversion/index.js.map +1 -0
- package/build/conversion/src/ts/fixedPoint/index.d.ts +12 -0
- package/build/conversion/src/ts/fixedPoint/index.js +79 -0
- package/build/conversion/src/ts/fixedPoint/index.js.map +1 -0
- package/build/conversion/src/ts/index.d.ts +169 -0
- package/build/conversion/src/ts/index.js +453 -0
- package/build/conversion/src/ts/index.js.map +1 -0
- package/build/conversion/test/bigintToBase64.test.js +72 -0
- package/build/conversion/test/bigintToBase64.test.js.map +1 -0
- package/build/conversion/test/bigintToBuf.test.js +77 -0
- package/build/conversion/test/bigintToBuf.test.js.map +1 -0
- package/build/conversion/test/bigintToHex.test.js +86 -0
- package/build/conversion/test/bigintToHex.test.js.map +1 -0
- package/build/conversion/test/bigintToText.test.js +63 -0
- package/build/conversion/test/bigintToText.test.js.map +1 -0
- package/build/conversion/test/bufToBigint.test.js +55 -0
- package/build/conversion/test/bufToBigint.test.js.map +1 -0
- package/build/conversion/test/hexToBigint.test.js +56 -0
- package/build/conversion/test/hexToBigint.test.js.map +1 -0
- package/build/conversion/test/hexToBuf.test.js +73 -0
- package/build/conversion/test/hexToBuf.test.js.map +1 -0
- package/build/conversion/test/parseHex.test.js +69 -0
- package/build/conversion/test/parseHex.test.js.map +1 -0
- package/build/conversion/test/textToBuf.test.js +59 -0
- package/build/conversion/test/textToBuf.test.js.map +1 -0
- package/build/index.js +242 -0
- package/build/index.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +257 -200
- package/dist/index.umd.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/conversion/src/ts/fixedPoint/index.d.ts +12 -0
- package/dist/types/conversion/src/ts/index.d.ts +1 -0
- package/package.json +132 -132
- package/src/conversion/docs/README.md +9 -1
- package/src/conversion/src/docs/index.md +6 -1
- package/src/conversion/src/ts/fixedPoint/index.ts +87 -0
- package/src/conversion/src/ts/index.ts +15 -0
- /package/dist/types/dist/dist/{types/index.d.ts → index.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,132 +1,132 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@gsknnft/bigint-buffer",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "bigint to buffer conversion with native support and built-in conversion helpers",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.js",
|
|
7
|
-
"browser": "dist/browser.js",
|
|
8
|
-
"exports": {
|
|
9
|
-
".": {
|
|
10
|
-
"types": "./dist/index.d.ts",
|
|
11
|
-
"import": "./dist/index.js",
|
|
12
|
-
"require": "./dist/index.cjs"
|
|
13
|
-
},
|
|
14
|
-
"./conversion": {
|
|
15
|
-
"types": "./dist/conversion/index.d.ts",
|
|
16
|
-
"import": "./dist/conversion/index.js",
|
|
17
|
-
"require": "./dist/conversion/index.cjs"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
"files": [
|
|
21
|
-
"dist/",
|
|
22
|
-
"helper/",
|
|
23
|
-
"src/",
|
|
24
|
-
"binding.gyp",
|
|
25
|
-
"build/",
|
|
26
|
-
"README.md",
|
|
27
|
-
"LICENSE"
|
|
28
|
-
],
|
|
29
|
-
"workspaces": [],
|
|
30
|
-
"scripts": {
|
|
31
|
-
"test": "vitest run",
|
|
32
|
-
"test:watch": "vitest watch",
|
|
33
|
-
"coverage": "vitest cover ./test/index.js",
|
|
34
|
-
"lint": "eslint . --ext .ts,.js",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"test:
|
|
39
|
-
"
|
|
40
|
-
"declare": "tsc -p . --declaration --emitDeclarationOnly --outDir dist/types",
|
|
41
|
-
"benchmark": "node -r ts-node/register src/index.bench.ts",
|
|
42
|
-
"typedoc": "typedoc --out docs $(pwd)/src $(pwd)/helper --target es6 --mode file --tsconfig ./tsconfig.json --excludePrivate --excludeProtected --excludeNotExported --exclude '**/*+(spec|bench).ts'",
|
|
43
|
-
"rebuild
|
|
44
|
-
"build": "vite build && npm run declare && tsc -p . && cpy \"index.d.ts\" ../dist --cwd dist/types && cpy \"conversion/index.d.ts\" ../dist/conversion --cwd dist/types",
|
|
45
|
-
"compile": "vite build && npm run declare && tsc -p . && cpy \"index.d.ts\" ../dist/types --cwd dist/types && cpy \"conversion/index.d.ts\" ../dist/conversion --cwd dist/types",
|
|
46
|
-
"check": "eslint src/**/*.ts src/conversion/**/*.ts src/index.spec.ts src/conversion/test/**/*.ts",
|
|
47
|
-
"test:vitest": "vitest",
|
|
48
|
-
"test:all": "npm run test && npm run benchmark",
|
|
49
|
-
"clean": "rm -rf build/* dist/* node_modules package-lock.json pnpm-lock.yaml",
|
|
50
|
-
"fix": "eslint . --ext .ts,.js --fix && prettier --write .",
|
|
51
|
-
"pretest": "npm run compile",
|
|
52
|
-
"posttest": "npm run check"
|
|
53
|
-
},
|
|
54
|
-
"repository": {
|
|
55
|
-
"type": "git",
|
|
56
|
-
"url": "git+https://github.com/gsknnft/bigint-buffer.git"
|
|
57
|
-
},
|
|
58
|
-
"bugs": {
|
|
59
|
-
"url": "https://github.com/gsknnft/bigint-buffer/issues"
|
|
60
|
-
},
|
|
61
|
-
"homepage": "https://github.com/gsknnft/bigintbuffer",
|
|
62
|
-
"keywords": [
|
|
63
|
-
"bigint",
|
|
64
|
-
"bignum",
|
|
65
|
-
"tc39-bigint",
|
|
66
|
-
"napi"
|
|
67
|
-
],
|
|
68
|
-
"license": "Apache-2.0",
|
|
69
|
-
"pnpm": {
|
|
70
|
-
"overrides": {
|
|
71
|
-
"tmp": "0.2.5"
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
"
|
|
75
|
-
"bindings": "^1.5.0"
|
|
76
|
-
},
|
|
77
|
-
"devDependencies": {
|
|
78
|
-
"@rollup/plugin-commonjs": "^29.0.0",
|
|
79
|
-
"@rollup/plugin-esm-shim": "0.1.8",
|
|
80
|
-
"@rollup/plugin-json": "^6.1.0",
|
|
81
|
-
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
82
|
-
"@rollup/plugin-replace": "6.0.3",
|
|
83
|
-
"@rollup/plugin-typescript": "^12.1.4",
|
|
84
|
-
"rollup-plugin-polyfill-node": "^0.13.0",
|
|
85
|
-
"@types/benchmark": "^2.1.5",
|
|
86
|
-
"@types/bn.js": "^5.2.0",
|
|
87
|
-
"@types/chai": "^5.2.3",
|
|
88
|
-
"@types/mocha": "^10.0.10",
|
|
89
|
-
"@types/node": "^24.10.1",
|
|
90
|
-
"@vitest/coverage-v8": "4.0.14",
|
|
91
|
-
"assertion-error": "^2.0.1",
|
|
92
|
-
"benchmark": "^2.1.4",
|
|
93
|
-
"bindings": "1.5.0",
|
|
94
|
-
"bn.js": "^5.2.2",
|
|
95
|
-
"cpy-cli": "^6.0.0",
|
|
96
|
-
"cross-env": "^10.1.0",
|
|
97
|
-
"eslint": "^9.39.1",
|
|
98
|
-
"globals": "^16.5.0",
|
|
99
|
-
"jiti": "^2.6.1",
|
|
100
|
-
"karma": "^6.4.4",
|
|
101
|
-
"karma-chrome-launcher": "^3.2.0",
|
|
102
|
-
"karma-env-preprocessor": "^0.1.1",
|
|
103
|
-
"karma-mocha": "^2.0.1",
|
|
104
|
-
"karma-mocha-reporter": "^2.2.5",
|
|
105
|
-
"karma-webpack": "^5.0.1",
|
|
106
|
-
"microtime": "^3.0.0",
|
|
107
|
-
"mkdirp": "^3.0.1",
|
|
108
|
-
"prettier": "^3.2.2",
|
|
109
|
-
"rollup": "^4.53.3",
|
|
110
|
-
"ts-loader": "^9.5.4",
|
|
111
|
-
"ts-node": "^10.9.2",
|
|
112
|
-
"typedoc": "^0.28.14",
|
|
113
|
-
"typescript": "5.9.3",
|
|
114
|
-
"typescript-eslint": "^8.48.0",
|
|
115
|
-
"undici-types": "^7.16.0",
|
|
116
|
-
"vite": "7.2.4",
|
|
117
|
-
"vitest": "^4.0.14",
|
|
118
|
-
"webpack": "5.103.0",
|
|
119
|
-
"webpack-cli": "6.0.1"
|
|
120
|
-
},
|
|
121
|
-
"engines": {
|
|
122
|
-
"node": ">=
|
|
123
|
-
"npm": ">=9.0.0"
|
|
124
|
-
},
|
|
125
|
-
"publishConfig": {
|
|
126
|
-
"access": "public"
|
|
127
|
-
},
|
|
128
|
-
"contributors": [
|
|
129
|
-
"Gordon Skinner <gsknnft@gmail.com> (https://github.com/gsknnft)",
|
|
130
|
-
"Michael Wei <mwei@vmware.com> (https://github.com/no2chem)"
|
|
131
|
-
]
|
|
132
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@gsknnft/bigint-buffer",
|
|
3
|
+
"version": "1.4.5",
|
|
4
|
+
"description": "bigint to buffer conversion with native support and built-in conversion helpers",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"browser": "dist/browser.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"./conversion": {
|
|
15
|
+
"types": "./dist/conversion/index.d.ts",
|
|
16
|
+
"import": "./dist/conversion/index.js",
|
|
17
|
+
"require": "./dist/conversion/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/",
|
|
22
|
+
"helper/",
|
|
23
|
+
"src/",
|
|
24
|
+
"binding.gyp",
|
|
25
|
+
"build/",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"workspaces": [],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:watch": "vitest watch",
|
|
33
|
+
"coverage": "vitest cover ./test/index.js",
|
|
34
|
+
"lint": "eslint . --ext .ts,.js",
|
|
35
|
+
"prepublishOnly": "rm -rf build/Makefile; rm build/gyp-*; rm build/*.mk; rm build/*.Makefile; rm *.tgz",
|
|
36
|
+
"clean:js": "npm clean && rm -rf build/* dist/*",
|
|
37
|
+
"test:browser": "vite test --browser",
|
|
38
|
+
"test:node": "vitest run",
|
|
39
|
+
"postinstall": "node-gyp rebuild",
|
|
40
|
+
"declare": "tsc -p . --declaration --emitDeclarationOnly --outDir dist/types",
|
|
41
|
+
"benchmark": "node -r ts-node/register src/index.bench.ts",
|
|
42
|
+
"typedoc": "typedoc --out docs $(pwd)/src $(pwd)/helper --target es6 --mode file --tsconfig ./tsconfig.json --excludePrivate --excludeProtected --excludeNotExported --exclude '**/*+(spec|bench).ts'",
|
|
43
|
+
"rebuild": "node-gyp rebuild && npm run build",
|
|
44
|
+
"build": "vite build && npm run declare && tsc -p . && cpy \"index.d.ts\" ../dist --cwd dist/types && cpy \"conversion/index.d.ts\" ../dist/conversion --cwd dist/types",
|
|
45
|
+
"compile": "vite build && npm run declare && tsc -p . && cpy \"index.d.ts\" ../dist/types --cwd dist/types && cpy \"conversion/index.d.ts\" ../dist/conversion --cwd dist/types",
|
|
46
|
+
"check": "eslint src/**/*.ts src/conversion/**/*.ts src/index.spec.ts src/conversion/test/**/*.ts",
|
|
47
|
+
"test:vitest": "vitest",
|
|
48
|
+
"test:all": "npm run test && npm run benchmark",
|
|
49
|
+
"clean": "rm -rf build/* dist/* node_modules package-lock.json pnpm-lock.yaml",
|
|
50
|
+
"fix": "eslint . --ext .ts,.js --fix && prettier --write .",
|
|
51
|
+
"pretest": "npm run compile",
|
|
52
|
+
"posttest": "npm run check"
|
|
53
|
+
},
|
|
54
|
+
"repository": {
|
|
55
|
+
"type": "git",
|
|
56
|
+
"url": "git+https://github.com/gsknnft/bigint-buffer.git"
|
|
57
|
+
},
|
|
58
|
+
"bugs": {
|
|
59
|
+
"url": "https://github.com/gsknnft/bigint-buffer/issues"
|
|
60
|
+
},
|
|
61
|
+
"homepage": "https://github.com/gsknnft/bigintbuffer",
|
|
62
|
+
"keywords": [
|
|
63
|
+
"bigint",
|
|
64
|
+
"bignum",
|
|
65
|
+
"tc39-bigint",
|
|
66
|
+
"napi"
|
|
67
|
+
],
|
|
68
|
+
"license": "Apache-2.0",
|
|
69
|
+
"pnpm": {
|
|
70
|
+
"overrides": {
|
|
71
|
+
"tmp": "0.2.5"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"bindings": "^1.5.0"
|
|
76
|
+
},
|
|
77
|
+
"devDependencies": {
|
|
78
|
+
"@rollup/plugin-commonjs": "^29.0.0",
|
|
79
|
+
"@rollup/plugin-esm-shim": "0.1.8",
|
|
80
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
81
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
82
|
+
"@rollup/plugin-replace": "6.0.3",
|
|
83
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
84
|
+
"rollup-plugin-polyfill-node": "^0.13.0",
|
|
85
|
+
"@types/benchmark": "^2.1.5",
|
|
86
|
+
"@types/bn.js": "^5.2.0",
|
|
87
|
+
"@types/chai": "^5.2.3",
|
|
88
|
+
"@types/mocha": "^10.0.10",
|
|
89
|
+
"@types/node": "^24.10.1",
|
|
90
|
+
"@vitest/coverage-v8": "4.0.14",
|
|
91
|
+
"assertion-error": "^2.0.1",
|
|
92
|
+
"benchmark": "^2.1.4",
|
|
93
|
+
"bindings": "1.5.0",
|
|
94
|
+
"bn.js": "^5.2.2",
|
|
95
|
+
"cpy-cli": "^6.0.0",
|
|
96
|
+
"cross-env": "^10.1.0",
|
|
97
|
+
"eslint": "^9.39.1",
|
|
98
|
+
"globals": "^16.5.0",
|
|
99
|
+
"jiti": "^2.6.1",
|
|
100
|
+
"karma": "^6.4.4",
|
|
101
|
+
"karma-chrome-launcher": "^3.2.0",
|
|
102
|
+
"karma-env-preprocessor": "^0.1.1",
|
|
103
|
+
"karma-mocha": "^2.0.1",
|
|
104
|
+
"karma-mocha-reporter": "^2.2.5",
|
|
105
|
+
"karma-webpack": "^5.0.1",
|
|
106
|
+
"microtime": "^3.0.0",
|
|
107
|
+
"mkdirp": "^3.0.1",
|
|
108
|
+
"prettier": "^3.2.2",
|
|
109
|
+
"rollup": "^4.53.3",
|
|
110
|
+
"ts-loader": "^9.5.4",
|
|
111
|
+
"ts-node": "^10.9.2",
|
|
112
|
+
"typedoc": "^0.28.14",
|
|
113
|
+
"typescript": "5.9.3",
|
|
114
|
+
"typescript-eslint": "^8.48.0",
|
|
115
|
+
"undici-types": "^7.16.0",
|
|
116
|
+
"vite": "7.2.4",
|
|
117
|
+
"vitest": "^4.0.14",
|
|
118
|
+
"webpack": "5.103.0",
|
|
119
|
+
"webpack-cli": "6.0.1"
|
|
120
|
+
},
|
|
121
|
+
"engines": {
|
|
122
|
+
"node": ">=20.0.0",
|
|
123
|
+
"npm": ">=9.0.0"
|
|
124
|
+
},
|
|
125
|
+
"publishConfig": {
|
|
126
|
+
"access": "public"
|
|
127
|
+
},
|
|
128
|
+
"contributors": [
|
|
129
|
+
"Gordon Skinner <gsknnft@gmail.com> (https://github.com/gsknnft)",
|
|
130
|
+
"Michael Wei <mwei@vmware.com> (https://github.com/no2chem)"
|
|
131
|
+
]
|
|
132
|
+
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
***
|
|
4
4
|
|
|
5
|
-
# bigint-conversion
|
|
5
|
+
# bigint-conversion (v1.4.5)
|
|
6
6
|
|
|
7
7
|
## Type Aliases
|
|
8
8
|
|
|
@@ -32,3 +32,11 @@
|
|
|
32
32
|
- [toBufferBE](functions/toBufferBE.md)
|
|
33
33
|
- [toBufferLE](functions/toBufferLE.md)
|
|
34
34
|
- [validateBigIntBuffer](functions/validateBigIntBuffer.md)
|
|
35
|
+
- **New in 1.4.5:**
|
|
36
|
+
- [toFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
37
|
+
- [fromFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
38
|
+
- [addFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
39
|
+
- [subtractFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
40
|
+
- [averageFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
41
|
+
- [compareFixedPoint](../src/ts/fixedPoint/index.ts)
|
|
42
|
+
- [FixedPoint type](../src/ts/fixedPoint/index.ts)
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
[](https://standardjs.com)
|
|
4
4
|
{{GITHUB_ACTIONS_BADGES}}
|
|
5
5
|
|
|
6
|
-
# {{PKG_NAME}}
|
|
6
|
+
# {{PKG_NAME}} v1.4.5
|
|
7
7
|
|
|
8
8
|
Convert to/from non-negative integers represented with [ES-2020 native JS implementation of BigInt](https://tc39.es/ecma262/#sec-bigint-objects) from/to:
|
|
9
9
|
|
|
@@ -14,6 +14,11 @@ Convert to/from non-negative integers represented with [ES-2020 native JS implem
|
|
|
14
14
|
|
|
15
15
|
It provides a common interface for the conversions that works for both **node.js** and **native javascript**.
|
|
16
16
|
|
|
17
|
+
**New in 1.4.5:**
|
|
18
|
+
- FixedPoint utilities for high-precision math and conversions
|
|
19
|
+
- Native bindings included in `build/Release/` out of the box
|
|
20
|
+
- Improved JS fallback for environments without native support
|
|
21
|
+
|
|
17
22
|
> Note that there is not a directly visible `TypedArray()` constructor, but a set of typed array ones: `Int8Array()`, `Uint8Array()`, `Uint8ClampedArray()`, `Int16Array()`, `Uint16Array()`, `Int32Array()`, `Uint32Array()`, `Float32Array()`, `Float64Array()`, `BigInt64Array()`, `BigUint64Array()`.
|
|
18
23
|
|
|
19
24
|
## Usage
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { bigintToHex, hexToBigint } from "#pkg";
|
|
2
|
+
|
|
3
|
+
export type FixedPoint = string;
|
|
4
|
+
|
|
5
|
+
export const FIXED_POINT_DECIMALS = 9;
|
|
6
|
+
export const FIXED_POINT_PATTERN = /^-?0x[0-9a-f]+$/i;
|
|
7
|
+
export const ZERO_FIXED_POINT: FixedPoint = '0x0';
|
|
8
|
+
|
|
9
|
+
const normalizeHex = (value: string): string =>
|
|
10
|
+
value.startsWith('0x') || value.startsWith('0X') ? value : `0x${value}`;
|
|
11
|
+
|
|
12
|
+
const toHexString = (value: bigint): FixedPoint => {
|
|
13
|
+
if (value === 0n) {
|
|
14
|
+
return ZERO_FIXED_POINT;
|
|
15
|
+
}
|
|
16
|
+
const isNegative = value < 0n;
|
|
17
|
+
const absValue = isNegative ? -value : value;
|
|
18
|
+
const hexValue = bigintToHex(absValue);
|
|
19
|
+
return `${isNegative ? '-' : ''}0x${hexValue}`;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export const toBigIntValue = (value?: FixedPoint): bigint => {
|
|
23
|
+
if (!value) {
|
|
24
|
+
return 0n;
|
|
25
|
+
}
|
|
26
|
+
const trimmed = value.trim();
|
|
27
|
+
if (trimmed.length === 0) {
|
|
28
|
+
return 0n;
|
|
29
|
+
}
|
|
30
|
+
const isNegative = trimmed.startsWith('-');
|
|
31
|
+
const body = isNegative ? trimmed.slice(1) : trimmed;
|
|
32
|
+
const normalized = normalizeHex(body);
|
|
33
|
+
const bigValue = hexToBigint(normalized);
|
|
34
|
+
return isNegative ? -bigValue : bigValue;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function toFixedPoint(
|
|
38
|
+
value: number,
|
|
39
|
+
decimals: number = FIXED_POINT_DECIMALS,
|
|
40
|
+
): FixedPoint {
|
|
41
|
+
if (!Number.isFinite(value)) {
|
|
42
|
+
return ZERO_FIXED_POINT;
|
|
43
|
+
}
|
|
44
|
+
const scale = Math.pow(10, decimals);
|
|
45
|
+
const scaled = BigInt(Math.round(value * scale));
|
|
46
|
+
return toHexString(scaled);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function fromFixedPoint(
|
|
50
|
+
value?: FixedPoint,
|
|
51
|
+
decimals: number = FIXED_POINT_DECIMALS,
|
|
52
|
+
): number {
|
|
53
|
+
const bigValue = toBigIntValue(value);
|
|
54
|
+
if (bigValue === 0n) {
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
const scale = Math.pow(10, decimals);
|
|
58
|
+
return Number(bigValue) / scale;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function addFixedPoint(a: FixedPoint, b: FixedPoint): FixedPoint {
|
|
62
|
+
return toHexString(toBigIntValue(a) + toBigIntValue(b));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function subtractFixedPoint(a: FixedPoint, b: FixedPoint): FixedPoint {
|
|
66
|
+
return toHexString(toBigIntValue(a) - toBigIntValue(b));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function averageFixedPoint(values: FixedPoint[]): FixedPoint {
|
|
70
|
+
if (values.length === 0) {
|
|
71
|
+
return ZERO_FIXED_POINT;
|
|
72
|
+
}
|
|
73
|
+
const sum = values.reduce((acc, value) => acc + toBigIntValue(value), 0n);
|
|
74
|
+
return toHexString(sum / BigInt(values.length));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function compareFixedPoint(a: FixedPoint, b: FixedPoint): number {
|
|
78
|
+
const diff = toBigIntValue(a) - toBigIntValue(b);
|
|
79
|
+
if (diff === 0n) {
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
return diff > 0n ? 1 : -1;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function fixedPointToBigInt(value?: FixedPoint): bigint {
|
|
86
|
+
return toBigIntValue(value);
|
|
87
|
+
}
|
|
@@ -2,6 +2,21 @@ import { Buffer } from 'buffer';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import fs from 'fs';
|
|
4
4
|
|
|
5
|
+
export {
|
|
6
|
+
toFixedPoint,
|
|
7
|
+
fromFixedPoint,
|
|
8
|
+
addFixedPoint,
|
|
9
|
+
subtractFixedPoint,
|
|
10
|
+
averageFixedPoint,
|
|
11
|
+
compareFixedPoint,
|
|
12
|
+
type FixedPoint,
|
|
13
|
+
FIXED_POINT_DECIMALS,
|
|
14
|
+
ZERO_FIXED_POINT,
|
|
15
|
+
fixedPointToBigInt,
|
|
16
|
+
toBigIntValue,
|
|
17
|
+
} from './fixedPoint'
|
|
18
|
+
|
|
19
|
+
|
|
5
20
|
interface ConverterInterface {
|
|
6
21
|
toBigInt: (buf: Buffer, bigEndian?: boolean) => bigint;
|
|
7
22
|
fromBigInt: (num: bigint, buf: Buffer, bigEndian?: boolean) => Buffer;
|
|
File without changes
|