@sora-substrate/math
Advanced tools
Comparing version 1.32.3 to 1.32.4
@@ -10,3 +10,3 @@ import BigNumber from 'bignumber.js'; | ||
declare type OperatorParam = string | number | BigNumber; | ||
declare type NumberType = Codec | FPNumber | string | number | BigNumber; | ||
declare type NumberType = Codec | FPNumber | string | number | BigNumber | bigint; | ||
export declare class FPNumber { | ||
@@ -143,6 +143,6 @@ precision: number; | ||
* Get FPNumber from codec value | ||
* @param {(string | number)} value Codec value `(value * 10^precision)` | ||
* @param {(string | number | bigint)} value Codec value `(value * 10^precision)` | ||
* @param {number} precision Precision | ||
*/ | ||
static fromCodecValue(value: number | string, precision?: number): FPNumber; | ||
static fromCodecValue(value: number | string | bigint, precision?: number): FPNumber; | ||
readonly value: BigNumber; | ||
@@ -153,3 +153,3 @@ private formatInitialDataString; | ||
/** | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber` and `Codec` data types. | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber`, `BigInt` and `Codec` data types. | ||
* It's better not to use `number` parameter as data if you want the strict rules for rounding | ||
@@ -165,2 +165,6 @@ * @param data | ||
/** | ||
* Formatted codec BigInt representation | ||
*/ | ||
get codecBigInt(): BigInt; | ||
/** | ||
* Format number to Codec string | ||
@@ -170,2 +174,6 @@ */ | ||
/** | ||
* Format number to Codec string wrapped with `BigInt` | ||
*/ | ||
toCodecBigInt(): BigInt; | ||
/** | ||
* Returns a string representation of the value using the custom formatting. | ||
@@ -198,2 +206,7 @@ * @param dp max decimal places | ||
/** | ||
* Format FPNumber to bigint | ||
* @param {number} [dp=6] Decimal places | ||
*/ | ||
toBigInt(dp?: number): bigint; | ||
/** | ||
* Returns a FPNumber whose value is the value of this FPNumber to a maximum of decimalPlaces decimal places. | ||
@@ -200,0 +213,0 @@ * @param {number} [dp=precision] Decimal places |
@@ -183,3 +183,3 @@ "use strict"; | ||
* Get FPNumber from codec value | ||
* @param {(string | number)} value Codec value `(value * 10^precision)` | ||
* @param {(string | number | bigint)} value Codec value `(value * 10^precision)` | ||
* @param {number} precision Precision | ||
@@ -189,3 +189,14 @@ */ | ||
let precision = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : FPNumber.DEFAULT_PRECISION; | ||
const filtered = typeof value === 'string' ? value.replace(/[,. ]/g, '') : value; | ||
let filtered; | ||
switch (typeof value) { | ||
case 'string': | ||
filtered = value.replace(/[,. ]/g, ''); | ||
break; | ||
case 'bigint': | ||
filtered = value.toString(); | ||
break; | ||
default: | ||
filtered = value; | ||
break; | ||
} | ||
const bn = new _bignumber.default(filtered); | ||
@@ -234,8 +245,10 @@ return new FPNumber(bn.div(10 ** precision), precision); | ||
formatInitialData(data, precision) { | ||
if (typeof data === 'number') { | ||
return data; | ||
switch (typeof data) { | ||
case 'number': | ||
return data; | ||
case 'string': | ||
return this.formatInitialDataString(data); | ||
case 'bigint': | ||
return this.formatInitialDataString(data.toString()); | ||
} | ||
if (typeof data === 'string') { | ||
return this.formatInitialDataString(data); | ||
} | ||
if ('toString' in data) { | ||
@@ -248,3 +261,3 @@ return this.formatInitialDataCodec(data, precision); | ||
/** | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber` and `Codec` data types. | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber`, `BigInt` and `Codec` data types. | ||
* It's better not to use `number` parameter as data if you want the strict rules for rounding | ||
@@ -283,2 +296,9 @@ * @param data | ||
/** | ||
* Formatted codec BigInt representation | ||
*/ | ||
get codecBigInt() { | ||
return BigInt(this.codec); | ||
} | ||
/** | ||
* Format number to Codec string | ||
@@ -291,2 +311,9 @@ */ | ||
/** | ||
* Format number to Codec string wrapped with `BigInt` | ||
*/ | ||
toCodecBigInt() { | ||
return this.codecBigInt; | ||
} | ||
/** | ||
* Returns a string representation of the value using the custom formatting. | ||
@@ -362,2 +389,12 @@ * @param dp max decimal places | ||
/** | ||
* Format FPNumber to bigint | ||
* @param {number} [dp=6] Decimal places | ||
*/ | ||
toBigInt() { | ||
let dp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : FPNumber.DEFAULT_DECIMAL_PLACES; | ||
const result = this.value.dp(dp, FPNumber.DEFAULT_ROUND_MODE); | ||
return BigInt(result.toString()); | ||
} | ||
/** | ||
* Returns a FPNumber whose value is the value of this FPNumber to a maximum of decimalPlaces decimal places. | ||
@@ -364,0 +401,0 @@ * @param {number} [dp=precision] Decimal places |
{ | ||
"name": "@sora-substrate/math", | ||
"version": "1.32.3", | ||
"version": "1.32.4", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "main": "./build/index.js", |
{ | ||
"name": "@sora-substrate/math", | ||
"version": "1.32.3", | ||
"version": "1.32.4", | ||
"license": "Apache-2.0", | ||
@@ -18,3 +18,3 @@ "main": "./build/index.js", | ||
}, | ||
"gitHead": "71ad77e2ed914cad4e11864cf1a406c4b0b66fb3" | ||
"gitHead": "a0310b975a0f9bf3912c083c03da148e3d4080b1" | ||
} |
@@ -22,3 +22,3 @@ import BigNumber from 'bignumber.js'; | ||
type OperatorParamFull = FPNumber | string | number | BigNumber; | ||
type NumberType = Codec | FPNumber | string | number | BigNumber; | ||
type NumberType = Codec | FPNumber | string | number | BigNumber | bigint; | ||
@@ -204,7 +204,22 @@ const isFinityString = (str: string) => !['-Infinity', 'Infinity', 'NaN'].includes(str); | ||
* Get FPNumber from codec value | ||
* @param {(string | number)} value Codec value `(value * 10^precision)` | ||
* @param {(string | number | bigint)} value Codec value `(value * 10^precision)` | ||
* @param {number} precision Precision | ||
*/ | ||
public static fromCodecValue(value: number | string, precision: number = FPNumber.DEFAULT_PRECISION): FPNumber { | ||
const filtered = typeof value === 'string' ? value.replace(/[,. ]/g, '') : value; | ||
public static fromCodecValue( | ||
value: number | string | bigint, | ||
precision: number = FPNumber.DEFAULT_PRECISION | ||
): FPNumber { | ||
let filtered: number | string; | ||
switch (typeof value) { | ||
case 'string': | ||
filtered = value.replace(/[,. ]/g, ''); | ||
break; | ||
case 'bigint': | ||
filtered = value.toString(); | ||
break; | ||
default: | ||
filtered = value; | ||
break; | ||
} | ||
const bn = new BigNumber(filtered); | ||
@@ -266,9 +281,11 @@ return new FPNumber(bn.div(10 ** precision), precision); | ||
private formatInitialData(data: string | number | Codec, precision: number): string | number { | ||
if (typeof data === 'number') { | ||
return data; | ||
private formatInitialData(data: string | number | Codec | bigint, precision: number): string | number { | ||
switch (typeof data) { | ||
case 'number': | ||
return data; | ||
case 'string': | ||
return this.formatInitialDataString(data); | ||
case 'bigint': | ||
return this.formatInitialDataString(data.toString()); | ||
} | ||
if (typeof data === 'string') { | ||
return this.formatInitialDataString(data); | ||
} | ||
if ('toString' in data) { | ||
@@ -281,3 +298,3 @@ return this.formatInitialDataCodec(data, precision); | ||
/** | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber` and `Codec` data types. | ||
* Supports `data` as `string`, `number`, `BigNumber`, `FPNumber`, `BigInt` and `Codec` data types. | ||
* It's better not to use `number` parameter as data if you want the strict rules for rounding | ||
@@ -308,2 +325,9 @@ * @param data | ||
/** | ||
* Formatted codec BigInt representation | ||
*/ | ||
get codecBigInt(): BigInt { | ||
return BigInt(this.codec); | ||
} | ||
/** | ||
* Format number to Codec string | ||
@@ -316,2 +340,9 @@ */ | ||
/** | ||
* Format number to Codec string wrapped with `BigInt` | ||
*/ | ||
public toCodecBigInt(): BigInt { | ||
return this.codecBigInt; | ||
} | ||
/** | ||
* Returns a string representation of the value using the custom formatting. | ||
@@ -385,2 +416,11 @@ * @param dp max decimal places | ||
/** | ||
* Format FPNumber to bigint | ||
* @param {number} [dp=6] Decimal places | ||
*/ | ||
public toBigInt(dp: number = FPNumber.DEFAULT_DECIMAL_PLACES): bigint { | ||
const result = this.value.dp(dp, FPNumber.DEFAULT_ROUND_MODE); | ||
return BigInt(result.toString()); | ||
} | ||
/** | ||
* Returns a FPNumber whose value is the value of this FPNumber to a maximum of decimalPlaces decimal places. | ||
@@ -387,0 +427,0 @@ * @param {number} [dp=precision] Decimal places |
67244
1619