BigIntDecimal.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  8. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  9. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  10. var _numberUtil = require("./numberUtil");
  11. var BigIntDecimal = /*#__PURE__*/function () {
  12. /** BigInt will convert `0009` to `9`. We need record the len of decimal */
  13. function BigIntDecimal(value) {
  14. (0, _classCallCheck2.default)(this, BigIntDecimal);
  15. (0, _defineProperty2.default)(this, "origin", '');
  16. (0, _defineProperty2.default)(this, "negative", void 0);
  17. (0, _defineProperty2.default)(this, "integer", void 0);
  18. (0, _defineProperty2.default)(this, "decimal", void 0);
  19. (0, _defineProperty2.default)(this, "decimalLen", void 0);
  20. (0, _defineProperty2.default)(this, "empty", void 0);
  21. (0, _defineProperty2.default)(this, "nan", void 0);
  22. if ((0, _numberUtil.isEmpty)(value)) {
  23. this.empty = true;
  24. return;
  25. }
  26. this.origin = String(value);
  27. // Act like Number convert
  28. if (value === '-' || Number.isNaN(value)) {
  29. this.nan = true;
  30. return;
  31. }
  32. var mergedValue = value;
  33. // We need convert back to Number since it require `toFixed` to handle this
  34. if ((0, _numberUtil.isE)(mergedValue)) {
  35. mergedValue = Number(mergedValue);
  36. }
  37. mergedValue = typeof mergedValue === 'string' ? mergedValue : (0, _numberUtil.num2str)(mergedValue);
  38. if ((0, _numberUtil.validateNumber)(mergedValue)) {
  39. var trimRet = (0, _numberUtil.trimNumber)(mergedValue);
  40. this.negative = trimRet.negative;
  41. var numbers = trimRet.trimStr.split('.');
  42. this.integer = BigInt(numbers[0]);
  43. var decimalStr = numbers[1] || '0';
  44. this.decimal = BigInt(decimalStr);
  45. this.decimalLen = decimalStr.length;
  46. } else {
  47. this.nan = true;
  48. }
  49. }
  50. (0, _createClass2.default)(BigIntDecimal, [{
  51. key: "getMark",
  52. value: function getMark() {
  53. return this.negative ? '-' : '';
  54. }
  55. }, {
  56. key: "getIntegerStr",
  57. value: function getIntegerStr() {
  58. return this.integer.toString();
  59. }
  60. /**
  61. * @private get decimal string
  62. */
  63. }, {
  64. key: "getDecimalStr",
  65. value: function getDecimalStr() {
  66. return this.decimal.toString().padStart(this.decimalLen, '0');
  67. }
  68. /**
  69. * @private Align BigIntDecimal with same decimal length. e.g. 12.3 + 5 = 1230000
  70. * This is used for add function only.
  71. */
  72. }, {
  73. key: "alignDecimal",
  74. value: function alignDecimal(decimalLength) {
  75. var str = "".concat(this.getMark()).concat(this.getIntegerStr()).concat(this.getDecimalStr().padEnd(decimalLength, '0'));
  76. return BigInt(str);
  77. }
  78. }, {
  79. key: "negate",
  80. value: function negate() {
  81. var clone = new BigIntDecimal(this.toString());
  82. clone.negative = !clone.negative;
  83. return clone;
  84. }
  85. }, {
  86. key: "cal",
  87. value: function cal(offset, calculator, calDecimalLen) {
  88. var maxDecimalLength = Math.max(this.getDecimalStr().length, offset.getDecimalStr().length);
  89. var myAlignedDecimal = this.alignDecimal(maxDecimalLength);
  90. var offsetAlignedDecimal = offset.alignDecimal(maxDecimalLength);
  91. var valueStr = calculator(myAlignedDecimal, offsetAlignedDecimal).toString();
  92. var nextDecimalLength = calDecimalLen(maxDecimalLength);
  93. // We need fill string length back to `maxDecimalLength` to avoid parser failed
  94. var _trimNumber = (0, _numberUtil.trimNumber)(valueStr),
  95. negativeStr = _trimNumber.negativeStr,
  96. trimStr = _trimNumber.trimStr;
  97. var hydrateValueStr = "".concat(negativeStr).concat(trimStr.padStart(nextDecimalLength + 1, '0'));
  98. return new BigIntDecimal("".concat(hydrateValueStr.slice(0, -nextDecimalLength), ".").concat(hydrateValueStr.slice(-nextDecimalLength)));
  99. }
  100. }, {
  101. key: "add",
  102. value: function add(value) {
  103. if (this.isInvalidate()) {
  104. return new BigIntDecimal(value);
  105. }
  106. var offset = new BigIntDecimal(value);
  107. if (offset.isInvalidate()) {
  108. return this;
  109. }
  110. return this.cal(offset, function (num1, num2) {
  111. return num1 + num2;
  112. }, function (len) {
  113. return len;
  114. });
  115. }
  116. }, {
  117. key: "multi",
  118. value: function multi(value) {
  119. var target = new BigIntDecimal(value);
  120. if (this.isInvalidate() || target.isInvalidate()) {
  121. return new BigIntDecimal(NaN);
  122. }
  123. return this.cal(target, function (num1, num2) {
  124. return num1 * num2;
  125. }, function (len) {
  126. return len * 2;
  127. });
  128. }
  129. }, {
  130. key: "isEmpty",
  131. value: function isEmpty() {
  132. return this.empty;
  133. }
  134. }, {
  135. key: "isNaN",
  136. value: function isNaN() {
  137. return this.nan;
  138. }
  139. }, {
  140. key: "isInvalidate",
  141. value: function isInvalidate() {
  142. return this.isEmpty() || this.isNaN();
  143. }
  144. }, {
  145. key: "equals",
  146. value: function equals(target) {
  147. return this.toString() === (target === null || target === void 0 ? void 0 : target.toString());
  148. }
  149. }, {
  150. key: "lessEquals",
  151. value: function lessEquals(target) {
  152. return this.add(target.negate().toString()).toNumber() <= 0;
  153. }
  154. }, {
  155. key: "toNumber",
  156. value: function toNumber() {
  157. if (this.isNaN()) {
  158. return NaN;
  159. }
  160. return Number(this.toString());
  161. }
  162. }, {
  163. key: "toString",
  164. value: function toString() {
  165. var safe = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
  166. if (!safe) {
  167. return this.origin;
  168. }
  169. if (this.isInvalidate()) {
  170. return '';
  171. }
  172. return (0, _numberUtil.trimNumber)("".concat(this.getMark()).concat(this.getIntegerStr(), ".").concat(this.getDecimalStr())).fullStr;
  173. }
  174. }]);
  175. return BigIntDecimal;
  176. }();
  177. exports.default = BigIntDecimal;