@glif/filecoin-number
Advanced tools
Comparing version 2.0.5 to 2.0.34
@@ -29,2 +29,6 @@ import { BigNumber } from 'bignumber.js'; | ||
/** | ||
* Expresses this FilecoinNumber as a balance string | ||
*/ | ||
formatBalance(decimals?: number, addUnit?: boolean): string; | ||
/** | ||
* Returns a copy of this FilecoinNumber | ||
@@ -31,0 +35,0 @@ */ |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
@@ -25,2 +27,6 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } | ||
@@ -105,2 +111,48 @@ | ||
/** | ||
* Expresses this FilecoinNumber as a balance string | ||
*/ | ||
}, { | ||
key: "formatBalance", | ||
value: function formatBalance() { | ||
var decimals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3; | ||
var addUnit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
var format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSize: 3, | ||
suffix: addUnit ? ' FIL' : '' | ||
}; // Base value is zero | ||
if (this.isZero()) return this.toFormat(format); | ||
var isNegative = this.isNegative(); | ||
var dpValue = this.dp(decimals, _bignumber.BigNumber.ROUND_DOWN); | ||
var dpUpValue = this.dp(decimals, _bignumber.BigNumber.ROUND_UP); // Zero after stripping decimals | ||
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 | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
var power = 0; | ||
var units = ['K', 'M', 'B', 'T']; | ||
for (var _i = 0, _units = units; _i < _units.length; _i++) { | ||
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) | ||
})); | ||
} // Should never hit here | ||
throw new Error('Failed to format balance'); | ||
} | ||
/** | ||
* Returns a copy of this FilecoinNumber | ||
@@ -107,0 +159,0 @@ */ |
@@ -0,1 +1,2 @@ | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; | ||
@@ -8,2 +9,6 @@ import _createClass from "@babel/runtime/helpers/createClass"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | ||
@@ -89,2 +94,48 @@ | ||
/** | ||
* Expresses this FilecoinNumber as a balance string | ||
*/ | ||
}, { | ||
key: "formatBalance", | ||
value: function formatBalance() { | ||
var decimals = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 3; | ||
var addUnit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
var format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSize: 3, | ||
suffix: addUnit ? ' FIL' : '' | ||
}; // Base value is zero | ||
if (this.isZero()) return this.toFormat(format); | ||
var isNegative = this.isNegative(); | ||
var dpValue = this.dp(decimals, BigNumber.ROUND_DOWN); | ||
var dpUpValue = this.dp(decimals, BigNumber.ROUND_UP); // Zero after stripping decimals | ||
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 | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
var power = 0; | ||
var units = ['K', 'M', 'B', 'T']; | ||
for (var _i = 0, _units = units; _i < _units.length; _i++) { | ||
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) | ||
})); | ||
} // Should never hit here | ||
throw new Error('Failed to format balance'); | ||
} | ||
/** | ||
* Returns a copy of this FilecoinNumber | ||
@@ -91,0 +142,0 @@ */ |
@@ -0,1 +1,7 @@ | ||
import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
import { BigNumber } from 'bignumber.js'; // not sure how we want to configure rounding for this | ||
@@ -71,2 +77,44 @@ | ||
/** | ||
* Expresses this FilecoinNumber as a balance string | ||
*/ | ||
formatBalance(decimals = 3, addUnit = true) { | ||
if (decimals < 0) throw new Error('Decimals must be >= 0'); | ||
if (this.isNaN()) throw new Error('Value cannot be NaN'); | ||
const format = { | ||
decimalSeparator: '.', | ||
groupSeparator: ' ', | ||
groupSize: 3, | ||
suffix: addUnit ? ' FIL' : '' | ||
}; // Base value is zero | ||
if (this.isZero()) return this.toFormat(format); | ||
const isNegative = this.isNegative(); | ||
const dpValue = this.dp(decimals, BigNumber.ROUND_DOWN); | ||
const dpUpValue = this.dp(decimals, BigNumber.ROUND_UP); // Zero after stripping decimals | ||
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 | ||
if (dpValue.isGreaterThan(-1000) && dpValue.isLessThan(1000)) return dpValue.toFormat(format); // from thousands to trillions | ||
let power = 0; | ||
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}` | ||
})); | ||
} // Should never hit here | ||
throw new Error('Failed to format balance'); | ||
} | ||
/** | ||
* Returns a copy of this FilecoinNumber | ||
@@ -73,0 +121,0 @@ */ |
{ | ||
"name": "@glif/filecoin-number", | ||
"version": "2.0.5", | ||
"version": "2.0.34", | ||
"description": "a javascript package to handle Filecoin numbers like Fil and AttoFil", | ||
@@ -48,3 +48,3 @@ "main": "./dist/index.js", | ||
}, | ||
"gitHead": "0beb86e6c2b03dc7ca26d3806d76877825cae530" | ||
"gitHead": "05e8ecf138683daa6a49932ef1f168dbf7d6e0ed" | ||
} |
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
118317
1035