@kensoni/big-number
Version:
Handle the big number
379 lines (378 loc) • 13.3 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
import { add, aEqualB, aGreaterThanB, aGreaterThanOrEqualB, aLessThanB, aLessThanOrEqualB } from "./utils";
var SORT = {
bubble: function (bigs) {
var length = bigs.length;
for (var i = 0; i < length - 1; i++) {
var swapped = Boolean(true);
for (var j = 0; j < length - i - 1; j++) {
if (bigs[j].gt(bigs[j + 1])) {
var temp = bigs[j];
bigs[j] = bigs[j + 1];
bigs[j + 1] = temp;
swapped = false;
}
}
if (swapped === true)
break;
}
return bigs;
},
selection: function (bigs) {
var length = bigs.length;
for (var step = 0; step < length - 1; step++) {
var min_idx = step;
for (var i = step + 1; i < length; i++) {
if (bigs[i].lt(bigs[min_idx]))
min_idx = i;
}
var temp = bigs[step];
bigs[step] = bigs[min_idx];
bigs[min_idx] = temp;
}
return bigs;
},
insertion: function (bigs) {
var length = bigs.length;
for (var step = 1; step < length; step++) {
var key = bigs[step];
var j = step - 1;
while (j >= 0 && key.lt(bigs[j])) {
bigs[j + 1] = bigs[j];
--j;
}
bigs[j + 1] = key;
}
return bigs;
},
// quick(){},
// radix(){},
// merge(bigs: Array<BigNumber | BigNumberData>){
// },
// heap(){
// },
// shaker(){
// },
// shell(){
// },
// counting(){
// },
// binaryinsetion(){
// },
// interchange(){
// },
};
var BigNumber = /** @class */ (function () {
function BigNumber(value, options) {
var _a;
this._value = '';
this._integer = '';
this._decimal = '';
this._options = {};
this._nevigate = false;
this._options = options !== null && options !== void 0 ? options : {};
if (value !== undefined) {
var val = this.revert(value.toString());
if (typeof val === 'number') {
if (!isNaN(val)) {
this._value = val;
}
}
else {
this._value = val;
}
}
_a = this._value.toString().split('.'), this._integer = _a[0], this._decimal = _a[1];
if (this._decimal === undefined)
this._decimal = '';
this._nevigate = this._integer.startsWith('-');
this._integer = this._integer.replace(/^-/, '').replace(/^0+/, '0');
}
Object.defineProperty(BigNumber.prototype, "value", {
get: function () {
return this._value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "int", {
get: function () {
return this._integer;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "dec", {
get: function () {
return this._decimal;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "negative", {
get: function () {
return this._nevigate;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "isDecimal", {
get: function () {
return !!this._decimal.length;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "empty", {
get: function () {
return this._value === '';
},
enumerable: false,
configurable: true
});
Object.defineProperty(BigNumber.prototype, "formatValue", {
get: function () {
return this.format();
},
enumerable: false,
configurable: true
});
BigNumber.prototype.sameSign = function (big) {
return (this.negative && big.negative) || (!this.negative && big.negative);
};
BigNumber.prototype.eq = function (big) {
return aEqualB(this.toString(), big.toString());
};
BigNumber.prototype.lt = function (big) {
return aLessThanB(this.toString(), big.toString());
};
BigNumber.prototype.lte = function (big) {
return aLessThanOrEqualB(this.toString(), big.toString());
};
BigNumber.prototype.gt = function (big) {
return aGreaterThanB(this.toString(), big.toString());
};
BigNumber.prototype.gte = function (big) {
return aGreaterThanOrEqualB(this.toString(), big.toString());
};
BigNumber.prototype.abs = function () {
return BigNumber.from(this.value.toString().replace('-', ''), this._options);
};
BigNumber.prototype.add = function () {
var bigs = [];
for (var _i = 0; _i < arguments.length; _i++) {
bigs[_i] = arguments[_i];
}
var rs = this.toString();
bigs.forEach(function (big) { return rs = add(rs, big.toString()); });
return BigNumber.from(rs, this._options);
};
BigNumber.prototype.sub = function () {
var bigs = [];
for (var _i = 0; _i < arguments.length; _i++) {
bigs[_i] = arguments[_i];
}
var rs = this.toString();
bigs.forEach(function (big) {
if (big.negative) {
rs = add(rs, big.abs().toString());
}
else {
rs = add(rs, '-' + big.toString());
}
});
return BigNumber.from(rs, this._options);
};
BigNumber.prototype.mul = function () {
};
BigNumber.prototype.div = function (big) {
return this;
};
BigNumber.prototype.floor = function () {
return BigNumber.from((this.negative ? '-' : '') + this.int, this._options);
};
BigNumber.prototype.ceil = function () {
var val = BigNumber.from(this.int);
return BigNumber.from((this.negative ? '-' : '') +
(this.dec === '' ? val : val.add(BigNumber.from(1))), this._options);
};
BigNumber.prototype.round = function () {
var _a;
var up = parseInt((_a = this.dec[0]) !== null && _a !== void 0 ? _a : '0') >= 5;
return up ? this.ceil() : this.floor();
};
BigNumber.prototype.toString = function () {
return this._value.toString();
};
BigNumber.prototype.format = function (options) {
var _options = options !== null && options !== void 0 ? options : this._options;
var value = BigNumber.format(this._value, __assign(__assign({}, _options), { comma: false }));
if (!(_options === null || _options === void 0 ? void 0 : _options.comma))
return value;
return value.replace('.', '_').replace(/,/g, '.').replace('_', ',');
};
BigNumber.prototype.revert = function (value) {
return BigNumber.revert(value, this._options);
};
BigNumber.prototype.revertFormat = function (value) {
return BigNumber.revertFormat(value);
};
BigNumber.prototype.revertComma = function (value) {
return BigNumber.revertComma(value);
};
BigNumber.prototype.match = function (value) {
return BigNumber.match(value);
};
BigNumber.prototype.isMatch = function (value) {
return BigNumber.isMatch(value);
};
BigNumber.prototype.isNumber = function (value) {
return BigNumber.isNumber(value);
};
BigNumber.prototype.isInt = function (value) {
return BigNumber.isInt(value);
};
BigNumber.prototype.isFormat = function (value) {
return BigNumber.isFormat(value);
};
BigNumber.prototype.isComma = function (value) {
return BigNumber.isComma(value);
};
BigNumber.prototype.isOutOfMax = function (value) {
return BigNumber.isOutOfMax(value);
};
BigNumber.prototype.isOutOfMin = function (value) {
return BigNumber.isOutOfMin(value);
};
BigNumber.from = function (value, options) {
if (value instanceof BigNumber)
return new this(value.toString(), options);
return new this(value, options);
};
BigNumber.format = function (value, options) {
var _value = this.from(value, options).toString();
if (!_value)
return _value;
var arr = _value.split('.');
var existDec = arr.length > 1;
var _int = arr[0], _a = arr[1], _dec = _a === void 0 ? '' : _a;
if (_int.length < 4)
return _value;
var _b = options !== null && options !== void 0 ? options : {}, comma = _b.comma, _c = _b.decimal, decimal = _c === void 0 ? true : _c, trim = _b.trim;
var separator = comma ? ',' : '.';
var formatted;
if (_int.length < 16) {
formatted = new Intl.NumberFormat(comma ? 'de-DE' : 'en-EN').format(+_int);
}
else {
formatted = Array.from(_int)
.reverse()
.map(function (char, index) { return index % 3 === 2 ? ",".concat(char) : char; })
.reverse()
.join('')
.replace(/^,/, '');
if (comma)
formatted = formatted.replace(/,/g, '.');
}
var trimDecimal = (decimal === false || decimal <= 0);
var addDecimal = existDec && !trimDecimal && (!trim || _dec.length);
var decimalVal = _dec.substring(0, typeof decimal === 'number' ? decimal : undefined);
formatted = [formatted].concat(addDecimal ? [decimalVal] : []).join(separator);
return formatted;
};
BigNumber.revert = function (value, options) {
if (!this.isMatch(value))
return '';
if ((options === null || options === void 0 ? void 0 : options.comma) && this.isComma(value))
return this.revertComma(value);
if (this.isFormat(value))
return this.revertFormat(value);
if (this.isComma(value))
return this.revertComma(value);
return value;
};
BigNumber.revertFormat = function (value) {
value = value.trim();
if (!this.isFormat(value))
return NaN;
return value.replace(/,/g, '');
};
BigNumber.revertComma = function (value) {
value = value.trim();
if (!this.isComma(value))
return NaN;
return value.replace(/\./g, '').replace(',', '.');
};
BigNumber.match = function (value) {
var match = value.toString().match(/^-?(\d*\.?)\d*/);
return match ? match[0] : '';
};
BigNumber.isMatch = function (value) {
return this.isNumber(value)
|| this.isFormat(value.toString())
|| this.isComma(value.toString());
};
BigNumber.isNumber = function (value) {
return !!this.match(value);
};
BigNumber.isInt = function (value) {
return !!value.toString().match(/^-?\d+$/);
};
BigNumber.isFormat = function (value) {
return !!value.match(/^-?\d{1,3}(,\d{3})*(\.\d*)?$/);
};
BigNumber.isComma = function (value) {
return !!value.match(/^-?\d{1,3}(\.\d{3})*(,\d*)?$/);
};
BigNumber.isOutOfMax = function (value) {
return parseFloat(value.toString()) > BigNumber.MAX_VALUE;
};
BigNumber.isOutOfMin = function (value) {
return parseFloat(value.toString()) < BigNumber.MAX_VALUE;
};
BigNumber.sort = function (bigs, algorithm) {
if (algorithm === void 0) { algorithm = 'bubble'; }
if (bigs.length < 2)
return bigs;
return SORT[algorithm](bigs);
};
BigNumber.min = function (big) {
var bigs = [];
for (var _i = 1; _i < arguments.length; _i++) {
bigs[_i - 1] = arguments[_i];
}
return this.sort(__spreadArray([big], bigs, true))[0];
};
BigNumber.max = function (big) {
var bigs = [];
for (var _i = 1; _i < arguments.length; _i++) {
bigs[_i - 1] = arguments[_i];
}
return this.sort(__spreadArray([big], bigs, true)).pop();
};
BigNumber.MAX_INTEGER = Number.MAX_SAFE_INTEGER;
BigNumber.MIN_INTEGER = Number.MIN_SAFE_INTEGER;
BigNumber.MAX_VALUE = Number.MAX_VALUE;
BigNumber.MIN_VALUE = Number.MIN_VALUE;
return BigNumber;
}());
export default BigNumber;