@glif/filecoin-number
Advanced tools
Comparing version 2.0.56 to 2.0.57
@@ -55,4 +55,4 @@ /// <reference types="node" /> | ||
* Expresses this FilecoinNumber as a balance string | ||
* @param options.truncate Whether to truncate the address, defaults to `true` | ||
* @param options.decimals How many decimals to display, defaults to `3` | ||
* @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null` | ||
* @param options.decimals How many decimals to display, `null` disables rounding, defaults to `3` | ||
* @param options.addUnit Whether to display the unit, defaults to `true` | ||
@@ -62,3 +62,3 @@ */ | ||
truncate?: boolean; | ||
decimals?: number; | ||
decimals?: number | null; | ||
addUnit?: boolean; | ||
@@ -65,0 +65,0 @@ }): string; |
@@ -158,4 +158,4 @@ "use strict"; | ||
* Expresses this FilecoinNumber as a balance string | ||
* @param options.truncate Whether to truncate the address, defaults to `true` | ||
* @param options.decimals How many decimals to display, defaults to `3` | ||
* @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null` | ||
* @param options.decimals How many decimals to display, `null` disables rounding, defaults to `3` | ||
* @param options.addUnit Whether to display the unit, defaults to `true` | ||
@@ -170,26 +170,44 @@ */ | ||
var truncate = (_options$truncate = options === null || options === void 0 ? void 0 : options.truncate) !== null && _options$truncate !== void 0 ? _options$truncate : true; | ||
var round = (options === null || options === void 0 ? void 0 : options.decimals) !== null; | ||
var decimals = (_options$decimals = options === null || options === void 0 ? void 0 : options.decimals) !== null && _options$decimals !== void 0 ? _options$decimals : 3; | ||
var addUnit = (_options$addUnit = options === null || options === void 0 ? void 0 : options.addUnit) !== null && _options$addUnit !== void 0 ? _options$addUnit : true; | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
var addUnit = (_options$addUnit = options === null || options === void 0 ? void 0 : options.addUnit) !== null && _options$addUnit !== void 0 ? _options$addUnit : true; // Create format configuration | ||
var format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSeparator: ',', | ||
groupSize: 3, | ||
suffix: addUnit ? " ".concat(this.unit) : '' | ||
}; // Base value is zero | ||
}; // When not rounding, it doesn't make sense to truncate either. | ||
// Return the original value when it's zero or when not rounding. | ||
if (this.isZero()) return this.toFormat(format); | ||
var isNegative = this.isNegative(); | ||
var dpValue = truncate ? this.dp(decimals, _bignumber.BigNumber.ROUND_DOWN) : this.clone(); | ||
var dpUpValue = truncate ? this.dp(decimals, _bignumber.BigNumber.ROUND_UP) : this.clone(); // Zero after stripping decimals | ||
if (this.isZero() || !round) return this.toFormat(format); // Round down by default to avoid showing higher balance | ||
if (dpValue.isZero()) return decimals === 0 ? dpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '< ' : '> ' | ||
})) : dpUpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '> ' : '< ' | ||
})); // Stripped value is between -1000 and 1000 | ||
var rounded = this.dp(decimals, _bignumber.BigNumber.ROUND_DOWN); // Value is zero after rounding | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
if (rounded.isZero()) { | ||
var isNegative = this.isNegative(); | ||
if (decimals === 0) { | ||
// We rounded to 0 decimals, so we show | ||
// "< 0" for negative and "> 0" for positive values | ||
var prefix = isNegative ? '< ' : '> '; | ||
return rounded.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: prefix | ||
})); | ||
} else { | ||
// We rounded to 1+ decimals, so we show | ||
// "> -0.01" for negative and "< 0.01" for positive values | ||
var _prefix = isNegative ? '> ' : '< '; | ||
var roundedUp = this.dp(decimals, _bignumber.BigNumber.ROUND_UP); | ||
return roundedUp.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: _prefix | ||
})); | ||
} | ||
} // Return rounded value between -1000 and 1000 or when not truncating | ||
var isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000); | ||
if (isLt1K || !truncate) return rounded.toFormat(format); // Truncate values below -1000 or above 1000 | ||
var power = 0; | ||
@@ -200,7 +218,13 @@ var units = ['K', 'M', 'B', 'T']; | ||
var unit = _units[_i]; | ||
var unitVal = dpValue.dividedBy(Math.pow(1000, ++power)); | ||
var unitDpVal = unitVal.dp(3, _bignumber.BigNumber.ROUND_DOWN); | ||
if (unitDpVal.isGreaterThan(-1000) && unitDpVal.isLessThan(1000) || unit === 'T') return unitDpVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix: "".concat(unit).concat(format.suffix) | ||
})); | ||
var unitRaw = rounded.dividedBy(Math.pow(1000, ++power)); | ||
var unitVal = unitRaw.dp(1, _bignumber.BigNumber.ROUND_DOWN); | ||
var _isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000); | ||
if (_isLt1K || unit === 'T') { | ||
var suffix = "".concat(unit).concat(format.suffix); | ||
return unitVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix: suffix | ||
})); | ||
} | ||
} // Should never hit here | ||
@@ -207,0 +231,0 @@ |
@@ -142,4 +142,4 @@ import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
* Expresses this FilecoinNumber as a balance string | ||
* @param options.truncate Whether to truncate the address, defaults to `true` | ||
* @param options.decimals How many decimals to display, defaults to `3` | ||
* @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null` | ||
* @param options.decimals How many decimals to display, `null` disables rounding, defaults to `3` | ||
* @param options.addUnit Whether to display the unit, defaults to `true` | ||
@@ -154,26 +154,44 @@ */ | ||
var truncate = (_options$truncate = options === null || options === void 0 ? void 0 : options.truncate) !== null && _options$truncate !== void 0 ? _options$truncate : true; | ||
var round = (options === null || options === void 0 ? void 0 : options.decimals) !== null; | ||
var decimals = (_options$decimals = options === null || options === void 0 ? void 0 : options.decimals) !== null && _options$decimals !== void 0 ? _options$decimals : 3; | ||
var addUnit = (_options$addUnit = options === null || options === void 0 ? void 0 : options.addUnit) !== null && _options$addUnit !== void 0 ? _options$addUnit : true; | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
var addUnit = (_options$addUnit = options === null || options === void 0 ? void 0 : options.addUnit) !== null && _options$addUnit !== void 0 ? _options$addUnit : true; // Create format configuration | ||
var format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSeparator: ',', | ||
groupSize: 3, | ||
suffix: addUnit ? " ".concat(this.unit) : '' | ||
}; // Base value is zero | ||
}; // When not rounding, it doesn't make sense to truncate either. | ||
// Return the original value when it's zero or when not rounding. | ||
if (this.isZero()) return this.toFormat(format); | ||
var isNegative = this.isNegative(); | ||
var dpValue = truncate ? this.dp(decimals, BigNumber.ROUND_DOWN) : this.clone(); | ||
var dpUpValue = truncate ? this.dp(decimals, BigNumber.ROUND_UP) : this.clone(); // Zero after stripping decimals | ||
if (this.isZero() || !round) return this.toFormat(format); // Round down by default to avoid showing higher balance | ||
if (dpValue.isZero()) return decimals === 0 ? dpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '< ' : '> ' | ||
})) : dpUpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '> ' : '< ' | ||
})); // Stripped value is between -1000 and 1000 | ||
var rounded = this.dp(decimals, BigNumber.ROUND_DOWN); // Value is zero after rounding | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
if (rounded.isZero()) { | ||
var isNegative = this.isNegative(); | ||
if (decimals === 0) { | ||
// We rounded to 0 decimals, so we show | ||
// "< 0" for negative and "> 0" for positive values | ||
var prefix = isNegative ? '< ' : '> '; | ||
return rounded.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: prefix | ||
})); | ||
} else { | ||
// We rounded to 1+ decimals, so we show | ||
// "> -0.01" for negative and "< 0.01" for positive values | ||
var _prefix = isNegative ? '> ' : '< '; | ||
var roundedUp = this.dp(decimals, BigNumber.ROUND_UP); | ||
return roundedUp.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: _prefix | ||
})); | ||
} | ||
} // Return rounded value between -1000 and 1000 or when not truncating | ||
var isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000); | ||
if (isLt1K || !truncate) return rounded.toFormat(format); // Truncate values below -1000 or above 1000 | ||
var power = 0; | ||
@@ -184,7 +202,13 @@ var units = ['K', 'M', 'B', 'T']; | ||
var unit = _units[_i]; | ||
var unitVal = dpValue.dividedBy(Math.pow(1000, ++power)); | ||
var unitDpVal = unitVal.dp(3, BigNumber.ROUND_DOWN); | ||
if (unitDpVal.isGreaterThan(-1000) && unitDpVal.isLessThan(1000) || unit === 'T') return unitDpVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix: "".concat(unit).concat(format.suffix) | ||
})); | ||
var unitRaw = rounded.dividedBy(Math.pow(1000, ++power)); | ||
var unitVal = unitRaw.dp(1, BigNumber.ROUND_DOWN); | ||
var _isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000); | ||
if (_isLt1K || unit === 'T') { | ||
var suffix = "".concat(unit).concat(format.suffix); | ||
return unitVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix: suffix | ||
})); | ||
} | ||
} // Should never hit here | ||
@@ -191,0 +215,0 @@ |
@@ -133,4 +133,4 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
* Expresses this FilecoinNumber as a balance string | ||
* @param options.truncate Whether to truncate the address, defaults to `true` | ||
* @param options.decimals How many decimals to display, defaults to `3` | ||
* @param options.truncate Whether to truncate the address with K, M, B and T units, defaults to `true`. Disabled when `options.decimals` is `null` | ||
* @param options.decimals How many decimals to display, `null` disables rounding, defaults to `3` | ||
* @param options.addUnit Whether to display the unit, defaults to `true` | ||
@@ -142,26 +142,43 @@ */ | ||
const truncate = (options === null || options === void 0 ? void 0 : options.truncate) ?? true; | ||
const round = (options === null || options === void 0 ? void 0 : options.decimals) !== null; | ||
const decimals = (options === null || options === void 0 ? void 0 : options.decimals) ?? 3; | ||
const addUnit = (options === null || options === void 0 ? void 0 : options.addUnit) ?? true; | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
const addUnit = (options === null || options === void 0 ? void 0 : options.addUnit) ?? true; // Create format configuration | ||
const format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSeparator: ',', | ||
groupSize: 3, | ||
suffix: addUnit ? ` ${this.unit}` : '' | ||
}; // Base value is zero | ||
}; // When not rounding, it doesn't make sense to truncate either. | ||
// Return the original value when it's zero or when not rounding. | ||
if (this.isZero()) return this.toFormat(format); | ||
const isNegative = this.isNegative(); | ||
const dpValue = truncate ? this.dp(decimals, BigNumber.ROUND_DOWN) : this.clone(); | ||
const dpUpValue = truncate ? this.dp(decimals, BigNumber.ROUND_UP) : this.clone(); // Zero after stripping decimals | ||
if (this.isZero() || !round) return this.toFormat(format); // Round down by default to avoid showing higher balance | ||
if (dpValue.isZero()) return decimals === 0 ? dpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '< ' : '> ' | ||
})) : dpUpValue.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix: isNegative ? '> ' : '< ' | ||
})); // Stripped value is between -1000 and 1000 | ||
const rounded = this.dp(decimals, BigNumber.ROUND_DOWN); // Value is zero after rounding | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
if (rounded.isZero()) { | ||
const isNegative = this.isNegative(); | ||
if (decimals === 0) { | ||
// We rounded to 0 decimals, so we show | ||
// "< 0" for negative and "> 0" for positive values | ||
const prefix = isNegative ? '< ' : '> '; | ||
return rounded.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix | ||
})); | ||
} else { | ||
// We rounded to 1+ decimals, so we show | ||
// "> -0.01" for negative and "< 0.01" for positive values | ||
const prefix = isNegative ? '> ' : '< '; | ||
const roundedUp = this.dp(decimals, BigNumber.ROUND_UP); | ||
return roundedUp.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
prefix | ||
})); | ||
} | ||
} // Return rounded value between -1000 and 1000 or when not truncating | ||
const isLt1K = rounded.isGreaterThan(-1000) && rounded.isLessThan(1000); | ||
if (isLt1K || !truncate) return rounded.toFormat(format); // Truncate values below -1000 or above 1000 | ||
let power = 0; | ||
@@ -171,7 +188,12 @@ const units = ['K', 'M', 'B', 'T']; | ||
for (const unit of units) { | ||
const unitVal = dpValue.dividedBy(Math.pow(1000, ++power)); | ||
const unitDpVal = unitVal.dp(3, BigNumber.ROUND_DOWN); | ||
if (unitDpVal.isGreaterThan(-1000) && unitDpVal.isLessThan(1000) || unit === 'T') return unitDpVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix: `${unit}${format.suffix}` | ||
})); | ||
const unitRaw = rounded.dividedBy(Math.pow(1000, ++power)); | ||
const unitVal = unitRaw.dp(1, BigNumber.ROUND_DOWN); | ||
const isLt1K = unitVal.isGreaterThan(-1000) && unitVal.isLessThan(1000); | ||
if (isLt1K || unit === 'T') { | ||
const suffix = `${unit}${format.suffix}`; | ||
return unitVal.toFormat(_objectSpread(_objectSpread({}, format), {}, { | ||
suffix | ||
})); | ||
} | ||
} // Should never hit here | ||
@@ -178,0 +200,0 @@ |
{ | ||
"name": "@glif/filecoin-number", | ||
"version": "2.0.56", | ||
"version": "2.0.57", | ||
"description": "a javascript package to handle Filecoin numbers like Fil and AttoFil", | ||
@@ -50,3 +50,3 @@ "main": "./dist/index.js", | ||
}, | ||
"gitHead": "b2c970bc3e2fd6434480fa59969ad158edca7a19" | ||
"gitHead": "e8fbedae9310127bdfb310824a16a38b9952c154" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
150284
1358