UNPKG

vue-lazy-calc

Version:

A simple calculator with lazy evaluation featuer

126 lines 4.77 kB
import * as tslib_1 from "tslib"; var LazyCalc = /** @class */ (function () { function LazyCalc(init) { if (init === void 0) { init = 0; } this.initValue = 0; this.compose = function (fns) { return fns.reduceRight(function (prevFn, nextFn) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return nextFn(prevFn.apply(void 0, tslib_1.__spread(args))); }; }, function (i) { return i; }); }; this.initValue = init; this.operators = []; return this; } LazyCalc.prototype.createRound = function (methodName, precision) { var _this = this; if (precision === void 0) { precision = 0; } var func = Math[methodName]; return function (number) { if (_this.isInvalid(number) || _this.isInvalid(precision)) { return NaN; } precision = precision == null ? 0 : precision >= 0 ? Math.min(precision, 292) : Math.max(precision, -292); if (precision) { // Shift with exponential notation to avoid floating-point issues. // See [MDN](https://mdn.io/round#Examples) for more details. var pair = (number + "e").split("e"); var value = func(pair[0] + "e" + (+pair[1] + precision)); pair = (value + "e").split("e"); return +(pair[0] + "e" + (+pair[1] - precision)); } return func(number); }; }; LazyCalc.prototype.isInvalid = function (x) { return Number.isNaN(+x) || !Number.isFinite(+x); }; LazyCalc.prototype.clone = function (operators) { var tmp = new LazyCalc(this.initValue); tmp.operators = operators; return tmp; }; LazyCalc.prototype.add = function (y) { var _this = this; var operation = function (x) { if (_this.isInvalid(x) || _this.isInvalid(y)) { return NaN; } return +x + +y; }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.divide = function (y) { var _this = this; var operation = function (x) { if (_this.isInvalid(x) || _this.isInvalid(y)) { return NaN; } return +x / +y; }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.subtract = function (y) { var _this = this; var operation = function (x) { if (_this.isInvalid(x) || _this.isInvalid(y)) { return NaN; } return +x - +y; }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.multiply = function (y) { var _this = this; var operation = function (x) { if (_this.isInvalid(x) || _this.isInvalid(y)) { return NaN; } return +x * +y; }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.do = function (fn) { var operation = function (y) { return fn(y); }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.ceil = function (precision) { if (precision === void 0) { precision = 0; } var operation = this.createRound("ceil", precision); return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.floor = function (precision) { if (precision === void 0) { precision = 0; } var operation = this.createRound("floor", precision); return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.round = function (precision) { if (precision === void 0) { precision = 0; } var operation = this.createRound("round", precision); return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.default = function (fallback) { var _this = this; var operation = function (x) { return _this.isInvalid(x) ? fallback : x; }; return this.clone(tslib_1.__spread([operation], this.operators)); }; LazyCalc.prototype.value = function () { return this.compose(this.operators)(this.initValue); }; return LazyCalc; }()); export { LazyCalc }; //# sourceMappingURL=simple.js.map