vue-lazy-calc
Version:
A simple calculator with lazy evaluation featuer
129 lines • 4.89 kB
JavaScript
import * as tslib_1 from "tslib";
var LazyStream = /** @class */ (function () {
function LazyStream() {
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.operators = [];
return this;
}
LazyStream.prototype.clone = function (operators) {
var tmp = new LazyStream();
tmp.operators = operators;
return tmp;
};
LazyStream.prototype.isInvalid = function (x) {
return Number.isNaN(+x) || !Number.isFinite(+x);
};
LazyStream.prototype.createRound = function (methodName, precision) {
var _this = this;
if (precision === void 0) { precision = 0; }
var func = Math[methodName];
return function (number) {
// const _number = number.value()
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);
};
};
LazyStream.prototype.add = function (y) {
var _this = this;
var operation = function (x) {
var _y = y.value();
if (_this.isInvalid(x) || _this.isInvalid(_y)) {
return NaN;
}
return +x + +_y;
};
return this.clone(tslib_1.__spread([operation], this.operators));
};
LazyStream.prototype.subtract = function (y) {
var _this = this;
var _y = y.value();
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));
};
LazyStream.prototype.multiply = function (y) {
var _this = this;
var _y = y.value();
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));
};
LazyStream.prototype.divide = function (y) {
var _this = this;
var _y = y.value();
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));
};
LazyStream.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));
};
LazyStream.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));
};
LazyStream.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));
};
LazyStream.prototype.do = function (fn) {
var operation = function (y) {
return fn(y);
};
// this.operators.unshift(operation);
return this.clone(tslib_1.__spread([operation], this.operators));
};
LazyStream.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));
};
LazyStream.prototype.value = function () {
return this.compose(this.operators)(0);
};
return LazyStream;
}());
export default LazyStream;
//# sourceMappingURL=stream.js.map