123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- "use strict";
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
- var _numberUtil = require("./numberUtil");
- var BigIntDecimal = /*#__PURE__*/function () {
- /** BigInt will convert `0009` to `9`. We need record the len of decimal */
- function BigIntDecimal(value) {
- (0, _classCallCheck2.default)(this, BigIntDecimal);
- (0, _defineProperty2.default)(this, "origin", '');
- (0, _defineProperty2.default)(this, "negative", void 0);
- (0, _defineProperty2.default)(this, "integer", void 0);
- (0, _defineProperty2.default)(this, "decimal", void 0);
- (0, _defineProperty2.default)(this, "decimalLen", void 0);
- (0, _defineProperty2.default)(this, "empty", void 0);
- (0, _defineProperty2.default)(this, "nan", void 0);
- if ((0, _numberUtil.isEmpty)(value)) {
- this.empty = true;
- return;
- }
- this.origin = String(value);
- // Act like Number convert
- if (value === '-' || Number.isNaN(value)) {
- this.nan = true;
- return;
- }
- var mergedValue = value;
- // We need convert back to Number since it require `toFixed` to handle this
- if ((0, _numberUtil.isE)(mergedValue)) {
- mergedValue = Number(mergedValue);
- }
- mergedValue = typeof mergedValue === 'string' ? mergedValue : (0, _numberUtil.num2str)(mergedValue);
- if ((0, _numberUtil.validateNumber)(mergedValue)) {
- var trimRet = (0, _numberUtil.trimNumber)(mergedValue);
- this.negative = trimRet.negative;
- var numbers = trimRet.trimStr.split('.');
- this.integer = BigInt(numbers[0]);
- var decimalStr = numbers[1] || '0';
- this.decimal = BigInt(decimalStr);
- this.decimalLen = decimalStr.length;
- } else {
- this.nan = true;
- }
- }
- (0, _createClass2.default)(BigIntDecimal, [{
- key: "getMark",
- value: function getMark() {
- return this.negative ? '-' : '';
- }
- }, {
- key: "getIntegerStr",
- value: function getIntegerStr() {
- return this.integer.toString();
- }
- /**
- * @private get decimal string
- */
- }, {
- key: "getDecimalStr",
- value: function getDecimalStr() {
- return this.decimal.toString().padStart(this.decimalLen, '0');
- }
- /**
- * @private Align BigIntDecimal with same decimal length. e.g. 12.3 + 5 = 1230000
- * This is used for add function only.
- */
- }, {
- key: "alignDecimal",
- value: function alignDecimal(decimalLength) {
- var str = "".concat(this.getMark()).concat(this.getIntegerStr()).concat(this.getDecimalStr().padEnd(decimalLength, '0'));
- return BigInt(str);
- }
- }, {
- key: "negate",
- value: function negate() {
- var clone = new BigIntDecimal(this.toString());
- clone.negative = !clone.negative;
- return clone;
- }
- }, {
- key: "cal",
- value: function cal(offset, calculator, calDecimalLen) {
- var maxDecimalLength = Math.max(this.getDecimalStr().length, offset.getDecimalStr().length);
- var myAlignedDecimal = this.alignDecimal(maxDecimalLength);
- var offsetAlignedDecimal = offset.alignDecimal(maxDecimalLength);
- var valueStr = calculator(myAlignedDecimal, offsetAlignedDecimal).toString();
- var nextDecimalLength = calDecimalLen(maxDecimalLength);
- // We need fill string length back to `maxDecimalLength` to avoid parser failed
- var _trimNumber = (0, _numberUtil.trimNumber)(valueStr),
- negativeStr = _trimNumber.negativeStr,
- trimStr = _trimNumber.trimStr;
- var hydrateValueStr = "".concat(negativeStr).concat(trimStr.padStart(nextDecimalLength + 1, '0'));
- return new BigIntDecimal("".concat(hydrateValueStr.slice(0, -nextDecimalLength), ".").concat(hydrateValueStr.slice(-nextDecimalLength)));
- }
- }, {
- key: "add",
- value: function add(value) {
- if (this.isInvalidate()) {
- return new BigIntDecimal(value);
- }
- var offset = new BigIntDecimal(value);
- if (offset.isInvalidate()) {
- return this;
- }
- return this.cal(offset, function (num1, num2) {
- return num1 + num2;
- }, function (len) {
- return len;
- });
- }
- }, {
- key: "multi",
- value: function multi(value) {
- var target = new BigIntDecimal(value);
- if (this.isInvalidate() || target.isInvalidate()) {
- return new BigIntDecimal(NaN);
- }
- return this.cal(target, function (num1, num2) {
- return num1 * num2;
- }, function (len) {
- return len * 2;
- });
- }
- }, {
- key: "isEmpty",
- value: function isEmpty() {
- return this.empty;
- }
- }, {
- key: "isNaN",
- value: function isNaN() {
- return this.nan;
- }
- }, {
- key: "isInvalidate",
- value: function isInvalidate() {
- return this.isEmpty() || this.isNaN();
- }
- }, {
- key: "equals",
- value: function equals(target) {
- return this.toString() === (target === null || target === void 0 ? void 0 : target.toString());
- }
- }, {
- key: "lessEquals",
- value: function lessEquals(target) {
- return this.add(target.negate().toString()).toNumber() <= 0;
- }
- }, {
- key: "toNumber",
- value: function toNumber() {
- if (this.isNaN()) {
- return NaN;
- }
- return Number(this.toString());
- }
- }, {
- key: "toString",
- value: function toString() {
- var safe = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
- if (!safe) {
- return this.origin;
- }
- if (this.isInvalidate()) {
- return '';
- }
- return (0, _numberUtil.trimNumber)("".concat(this.getMark()).concat(this.getIntegerStr(), ".").concat(this.getDecimalStr())).fullStr;
- }
- }]);
- return BigIntDecimal;
- }();
- exports.default = BigIntDecimal;
|