@agrozyme/numeric 1.0.5 → 1.0.10
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/jasmine.json +2 -6
- package/out/index.d.ts +2 -2
- package/out/index.js +20 -19
- package/out/index.mjs +4 -3
- package/package.json +15 -10
- package/root/index.ts +7 -3
package/jasmine.json
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"spec_dir": "test",
|
|
3
|
-
"spec_files": [
|
|
4
|
-
|
|
5
|
-
],
|
|
6
|
-
"requires": [
|
|
7
|
-
"ts-node/register"
|
|
8
|
-
],
|
|
3
|
+
"spec_files": ["**/*.test.ts"],
|
|
4
|
+
"requires": ["node_modules/ts-node/register/index.js"],
|
|
9
5
|
"stopSpecOnExpectationFailure": false,
|
|
10
6
|
"random": true
|
|
11
7
|
}
|
package/out/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
|
|
|
3
3
|
import bigInt from 'big-integer';
|
|
4
4
|
import BN from 'bn.js';
|
|
5
5
|
import { Decimal } from 'decimal.js';
|
|
6
|
-
import {
|
|
6
|
+
import { InspectOptionsStylized } from 'util';
|
|
7
7
|
export declare type NumericValue = Numeric | Decimal.Value | bigInt.BigNumber | BigNumberish;
|
|
8
8
|
export interface IFormatNumberOptions {
|
|
9
9
|
negativeType?: 'right' | 'left' | 'brackets' | 'none';
|
|
@@ -136,7 +136,7 @@ export declare class Numeric extends Decimal {
|
|
|
136
136
|
toSignificantDigits(decimalPlaces?: number, rounding?: Decimal.Rounding): Numeric;
|
|
137
137
|
trunc(): Numeric;
|
|
138
138
|
truncated(): Numeric;
|
|
139
|
-
|
|
139
|
+
inspect(depth: number, options: InspectOptionsStylized): string;
|
|
140
140
|
}
|
|
141
141
|
export declare const toNumeric: (value: NumericValue) => Numeric;
|
|
142
142
|
export declare const toNumerics: (values: NumericValue[]) => Numeric[];
|
package/out/index.js
CHANGED
|
@@ -4,12 +4,12 @@ exports.MinInt256 = exports.MaxInt256 = exports.MaxUint256 = exports.WeiPerEther
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const bignumber_1 = require("@ethersproject/bignumber");
|
|
6
6
|
const bytes_1 = require("@ethersproject/bytes");
|
|
7
|
-
const constants = tslib_1.__importStar(require("@ethersproject/constants"));
|
|
8
|
-
const big_integer_1 = tslib_1.__importDefault(require("big-integer"));
|
|
9
|
-
const bn_js_1 = tslib_1.__importDefault(require("bn.js"));
|
|
7
|
+
const constants = (0, tslib_1.__importStar)(require("@ethersproject/constants"));
|
|
8
|
+
const big_integer_1 = (0, tslib_1.__importDefault)(require("big-integer"));
|
|
9
|
+
const bn_js_1 = (0, tslib_1.__importDefault)(require("bn.js"));
|
|
10
|
+
const browser_or_node_1 = require("browser-or-node");
|
|
10
11
|
const decimal_js_1 = require("decimal.js");
|
|
11
|
-
const format_number_1 = tslib_1.__importDefault(require("format-number"));
|
|
12
|
-
const util_1 = require("util");
|
|
12
|
+
const format_number_1 = (0, tslib_1.__importDefault)(require("format-number"));
|
|
13
13
|
//noinspection FunctionNamingConventionJS
|
|
14
14
|
class Numeric extends decimal_js_1.Decimal {
|
|
15
15
|
constructor(value) {
|
|
@@ -29,8 +29,8 @@ class Numeric extends decimal_js_1.Decimal {
|
|
|
29
29
|
if (big_integer_1.default.isInstance(value) || bignumber_1.BigNumber.isBigNumber(value) || bn_js_1.default.isBN(value)) {
|
|
30
30
|
super(value.toString());
|
|
31
31
|
}
|
|
32
|
-
else if (bytes_1.isBytesLike(value)) {
|
|
33
|
-
super(bytes_1.hexlify(value));
|
|
32
|
+
else if ((0, bytes_1.isBytesLike)(value)) {
|
|
33
|
+
super((0, bytes_1.hexlify)(value));
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
36
36
|
super(value);
|
|
@@ -91,7 +91,7 @@ class Numeric extends decimal_js_1.Decimal {
|
|
|
91
91
|
return new Numeric(value).floor();
|
|
92
92
|
}
|
|
93
93
|
static hypot(...values) {
|
|
94
|
-
return new Numeric(decimal_js_1.Decimal.hypot(...exports.toNumerics(values)));
|
|
94
|
+
return new Numeric(decimal_js_1.Decimal.hypot(...(0, exports.toNumerics)(values)));
|
|
95
95
|
}
|
|
96
96
|
static ln(value) {
|
|
97
97
|
return new Numeric(value).ln();
|
|
@@ -106,10 +106,10 @@ class Numeric extends decimal_js_1.Decimal {
|
|
|
106
106
|
return new Numeric(value).log(2);
|
|
107
107
|
}
|
|
108
108
|
static max(...values) {
|
|
109
|
-
return new Numeric(decimal_js_1.Decimal.max(...exports.toNumerics(values)));
|
|
109
|
+
return new Numeric(decimal_js_1.Decimal.max(...(0, exports.toNumerics)(values)));
|
|
110
110
|
}
|
|
111
111
|
static min(...values) {
|
|
112
|
-
return new Numeric(decimal_js_1.Decimal.min(...exports.toNumerics(values)));
|
|
112
|
+
return new Numeric(decimal_js_1.Decimal.min(...(0, exports.toNumerics)(values)));
|
|
113
113
|
}
|
|
114
114
|
static mod(x, y) {
|
|
115
115
|
return new Numeric(x).mod(y);
|
|
@@ -362,7 +362,7 @@ class Numeric extends decimal_js_1.Decimal {
|
|
|
362
362
|
: super.toDecimalPlaces(decimalPlaces));
|
|
363
363
|
}
|
|
364
364
|
toFraction(maxDenominator) {
|
|
365
|
-
return exports.toNumerics(super.toFraction(undefined === maxDenominator ? undefined : new Numeric(maxDenominator)));
|
|
365
|
+
return (0, exports.toNumerics)(super.toFraction(undefined === maxDenominator ? undefined : new Numeric(maxDenominator)));
|
|
366
366
|
}
|
|
367
367
|
toNearest(value, rounding) {
|
|
368
368
|
return new Numeric(super.toNearest(new Numeric(value), rounding));
|
|
@@ -386,8 +386,9 @@ class Numeric extends decimal_js_1.Decimal {
|
|
|
386
386
|
truncated() {
|
|
387
387
|
return new Numeric(super.truncated());
|
|
388
388
|
}
|
|
389
|
-
|
|
390
|
-
|
|
389
|
+
inspect(depth, options) {
|
|
390
|
+
const value = this.toFixed();
|
|
391
|
+
return browser_or_node_1.isNode ? options.stylize(value, 'bigint') : value;
|
|
391
392
|
}
|
|
392
393
|
}
|
|
393
394
|
exports.Numeric = Numeric;
|
|
@@ -400,23 +401,23 @@ const toNumerics = (values) => {
|
|
|
400
401
|
};
|
|
401
402
|
exports.toNumerics = toNumerics;
|
|
402
403
|
const toIntegerString = (value) => {
|
|
403
|
-
return exports.toNumeric(value).toFixed(0);
|
|
404
|
+
return (0, exports.toNumeric)(value).toFixed(0);
|
|
404
405
|
};
|
|
405
406
|
exports.toIntegerString = toIntegerString;
|
|
406
407
|
const toBigInt = (value) => {
|
|
407
|
-
return BigInt(exports.toIntegerString(value));
|
|
408
|
+
return BigInt((0, exports.toIntegerString)(value));
|
|
408
409
|
};
|
|
409
410
|
exports.toBigInt = toBigInt;
|
|
410
411
|
const toBN = (value) => {
|
|
411
|
-
return new bn_js_1.default(exports.toIntegerString(value));
|
|
412
|
+
return new bn_js_1.default((0, exports.toIntegerString)(value));
|
|
412
413
|
};
|
|
413
414
|
exports.toBN = toBN;
|
|
414
415
|
const toBigInteger = (value) => {
|
|
415
|
-
return big_integer_1.default(exports.toIntegerString(value));
|
|
416
|
+
return (0, big_integer_1.default)((0, exports.toIntegerString)(value));
|
|
416
417
|
};
|
|
417
418
|
exports.toBigInteger = toBigInteger;
|
|
418
419
|
const toBigNumber = (value) => {
|
|
419
|
-
return bignumber_1.BigNumber.from(exports.toIntegerString(value));
|
|
420
|
+
return bignumber_1.BigNumber.from((0, exports.toIntegerString)(value));
|
|
420
421
|
};
|
|
421
422
|
exports.toBigNumber = toBigNumber;
|
|
422
423
|
const createArrayCompareFunction = (ascending = true) => {
|
|
@@ -427,7 +428,7 @@ const createArrayCompareFunction = (ascending = true) => {
|
|
|
427
428
|
};
|
|
428
429
|
exports.createArrayCompareFunction = createArrayCompareFunction;
|
|
429
430
|
const formatValue = (value, options) => {
|
|
430
|
-
const format = format_number_1.default(options);
|
|
431
|
+
const format = (0, format_number_1.default)(options);
|
|
431
432
|
const text = new Numeric(value).toFixed();
|
|
432
433
|
return format(text);
|
|
433
434
|
};
|
package/out/index.mjs
CHANGED
|
@@ -3,9 +3,9 @@ import { hexlify, isBytesLike } from '@ethersproject/bytes';
|
|
|
3
3
|
import * as constants from '@ethersproject/constants';
|
|
4
4
|
import bigInt from 'big-integer';
|
|
5
5
|
import BN from 'bn.js';
|
|
6
|
+
import { isNode } from 'browser-or-node';
|
|
6
7
|
import { Decimal } from 'decimal.js';
|
|
7
8
|
import formatter from 'format-number';
|
|
8
|
-
import { inspect } from 'util';
|
|
9
9
|
//noinspection FunctionNamingConventionJS
|
|
10
10
|
export class Numeric extends Decimal {
|
|
11
11
|
constructor(value) {
|
|
@@ -382,8 +382,9 @@ export class Numeric extends Decimal {
|
|
|
382
382
|
truncated() {
|
|
383
383
|
return new Numeric(super.truncated());
|
|
384
384
|
}
|
|
385
|
-
|
|
386
|
-
|
|
385
|
+
inspect(depth, options) {
|
|
386
|
+
const value = this.toFixed();
|
|
387
|
+
return isNode ? options.stylize(value, 'bigint') : value;
|
|
387
388
|
}
|
|
388
389
|
}
|
|
389
390
|
export const toNumeric = (value) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agrozyme/numeric",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "out/index.js",
|
|
6
6
|
"module": "out/index.mjs",
|
|
@@ -11,12 +11,6 @@
|
|
|
11
11
|
"require": "./out/index.js"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
"scripts": {
|
|
15
|
-
"clean": "shx rm -f *.tsbuildinfo && shx rm -rf out",
|
|
16
|
-
"build": "npm run clean && tsc-multi --force",
|
|
17
|
-
"prepublishOnly": "npm run build",
|
|
18
|
-
"test": "jasmine --config=jasmine.json"
|
|
19
|
-
},
|
|
20
14
|
"repository": {
|
|
21
15
|
"type": "git",
|
|
22
16
|
"url": "git+ssh://git@gitlab.com/agrozyme-package/JavaScript/numeric.git"
|
|
@@ -31,17 +25,28 @@
|
|
|
31
25
|
"devDependencies": {
|
|
32
26
|
"@types/bn.js": "^5",
|
|
33
27
|
"@types/jasmine": "^3",
|
|
34
|
-
"
|
|
28
|
+
"@types/node": "^17",
|
|
29
|
+
"jasmine": "^4",
|
|
35
30
|
"shx": "^0",
|
|
36
31
|
"ts-node": "^10",
|
|
37
32
|
"tsc-multi": "^0",
|
|
38
33
|
"typescript": "^4"
|
|
39
34
|
},
|
|
40
35
|
"dependencies": {
|
|
36
|
+
"@ethersproject/bignumber": "^5",
|
|
37
|
+
"@ethersproject/bytes": "^5",
|
|
41
38
|
"@ethersproject/constants": "^5",
|
|
42
39
|
"big-integer": "^1",
|
|
40
|
+
"bn.js": "^5",
|
|
41
|
+
"browser-or-node": "^2",
|
|
43
42
|
"decimal.js": "^10",
|
|
44
43
|
"format-number": "^3",
|
|
45
44
|
"tslib": "^2"
|
|
46
|
-
}
|
|
47
|
-
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"clean": "shx rm -f *.tsbuildinfo && shx rm -rf out",
|
|
48
|
+
"build": "npm run clean && tsc-multi --force",
|
|
49
|
+
"test": "jasmine --config=jasmine.json"
|
|
50
|
+
},
|
|
51
|
+
"readme": "# Numeric\n\nA wrapper of Decimal.js\n\n## Usage\n\nSee the source code for more details.\n\nAll functions are same as Decimal.js and can convert from:\n\n- String\n- Number\n- BigInt\n- Array of Number\n- [bn.js](https://www.npmjs.com/package/bn.js)\n- [BigInteger.js](https://www.npmjs.com/package/big-integer)\n- [Big Numbers](https://www.npmjs.com/package/@ethersproject/bignumber)\n- [Decimal.js](https://www.npmjs.com/package/decimal.js)\n\nExamples:\n\n```ts\nimport { Numeric } from '@agrozyme/numeric';\nconst test1 = new Numeric('1');\nconst test2 = new Numeric(2);\nconst test3 = new Numeric(3n);\nconst test4 = new Numeric([4]);\nconst test5 = new Numeric(new BN(5));\nconst test6 = new Numeric(bigInt('6'));\nconst test7 = new Numeric(BigNumber.from(7));\nconst test8 = new Numeric(new Decimal(8));\n```\n\n## Constants\n\nSame as [Etehreum Constants](https://www.npmjs.com/package/@ethersproject/constants) bignumber constants but export to Numeric type\n\n- NegativeOne\n- Zero\n- One\n- Two\n- WeiPerEther\n- MaxUint256\n- MaxInt256\n- MinInt256\n\n## Helper Functions\n\n- createArrayCompareFunction: create a simple array compare function\n\n```ts\nconst ascending = createArrayCompareFunction(true);\nconst descending = createArrayCompareFunction(false);\n```\n\n- formatValue: use [format-number](https://www.npmjs.com/package/format-number) to format a Numeric-Like value\n\n```ts\nconst text = formatValue(1000, { prefix: '$' });\n```\n\n## Convert Functions\n\nAll convert functions are accept one Numeric-Like argument, except `toNumerics`\n\n- toNumerics: convert a Numeric-Like array to a Numeric array\n\n```ts\nimport { toNumerics } from '@agrozyme/numeric';\nconst test = toNumerics(['1', 2, 3n]);\n```\n\n- toNumeric: convert a Numeric-Like value to a Numeric value\n- toIntegerString: convert a Numeric-Like value to a interger string\n- toBigInt: convert a Numeric-Like value to a `native` bigint value\n- toBN: convert a Numeric-Like value to a BN value\n- toBigInteger: convert a Numeric-Like value to a BigInteger value\n- toBigNumber: convert a Numeric-Like value to a BigNumber value\n\n## Config Functions\n\n- getNumericConfig: get NumericConfig that same as [Decimal.Config](https://mikemcl.github.io/decimal.js/#constructor-properties)\n- setupNumericConfig: setup NumericConfig\n"
|
|
52
|
+
}
|
package/root/index.ts
CHANGED
|
@@ -3,9 +3,12 @@ import { hexlify, isBytesLike } from '@ethersproject/bytes';
|
|
|
3
3
|
import * as constants from '@ethersproject/constants';
|
|
4
4
|
import bigInt from 'big-integer';
|
|
5
5
|
import BN from 'bn.js';
|
|
6
|
+
import { isNode } from 'browser-or-node';
|
|
6
7
|
import { Decimal } from 'decimal.js';
|
|
7
8
|
import formatter from 'format-number';
|
|
8
|
-
import {
|
|
9
|
+
import { InspectOptionsStylized } from 'util';
|
|
10
|
+
|
|
11
|
+
// const inspect = Symbol.for('nodejs.util.inspect.custom');
|
|
9
12
|
|
|
10
13
|
export type NumericValue = Numeric | Decimal.Value | bigInt.BigNumber | BigNumberish;
|
|
11
14
|
|
|
@@ -521,8 +524,9 @@ export class Numeric extends Decimal {
|
|
|
521
524
|
return new Numeric(super.truncated());
|
|
522
525
|
}
|
|
523
526
|
|
|
524
|
-
|
|
525
|
-
|
|
527
|
+
inspect(depth: number, options: InspectOptionsStylized) {
|
|
528
|
+
const value = this.toFixed();
|
|
529
|
+
return isNode ? options.stylize(value, 'bigint'): value;
|
|
526
530
|
}
|
|
527
531
|
}
|
|
528
532
|
|