UNPKG

data-unit

Version:

Simple TypeScript/ES2017 classes to represent unit values (like time/data size)

102 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Amount = /** @class */ (function () { function Amount(value, unit) { this.value = value; this.unit = unit; } Amount.prototype.create = function (value, unit) { return new this.constructor(value, unit); }; Amount.prototype.unaryOp = function (op) { return this.create(op(this.value), this.unit); }; Amount.prototype.binaryOp = function (value, op) { if (typeof value === 'number') { return this.create(op(this.value, value), this.unit); } else { var amount = this.constructor.parse(value); return this.create(op(this.value, amount.as(this.unit)), this.unit); } }; Amount.prototype.ternaryOp = function (value1, value2, op) { var value1Number = typeof value1 === 'number' ? value1 : this.constructor.parse(value1).as(this.unit); var value2Number = typeof value2 === 'number' ? value2 : this.constructor.parse(value2).as(this.unit); return this.create(op(this.value, value1Number, value2Number), this.unit); }; Amount.prototype.convert = function (unit) { return this.create(this.as(unit), unit); }; Amount.prototype.as = function (unit, decimals) { if (unit === void 0) { unit = this.unit; } if (decimals === void 0) { decimals = null; } var value = this.value; if (unit < this.unit) { for (var i = this.unit; i > unit; i--) { value *= this.exchangeRates[i].multiplier; } } else if (unit > this.unit) { for (var i = this.unit + 1; i <= unit; i++) { value /= this.exchangeRates[i].multiplier; } } if (typeof decimals === 'number') { if (decimals > 0) { var exp = Math.pow(10, decimals); value = Math.round(value * exp) / exp; } else if (decimals === 0) { value = Math.round(value); } } return value; }; Amount.prototype.div = function (value) { return this.binaryOp(value, function (a, b) { return a / b; }); }; Amount.prototype.mul = function (value) { return this.binaryOp(value, function (a, b) { return a * b; }); }; Amount.prototype.plus = function (value) { return this.binaryOp(value, function (a, b) { return a + b; }); }; Amount.prototype.minus = function (value) { return this.binaryOp(value, function (a, b) { return a - b; }); }; Amount.prototype.floor = function () { return this.unaryOp(function (a) { return Math.floor(a); }); }; Amount.prototype.ceil = function () { return this.unaryOp(function (a) { return Math.ceil(a); }); }; Amount.prototype.round = function (decimals) { if (decimals === void 0) { decimals = 0; } if (decimals > 0) { var exp_1 = Math.pow(10, decimals); return this.unaryOp(function (a) { return Math.round(a * exp_1) / exp_1; }); } else { return this.unaryOp(function (a) { return Math.round(a); }); } }; Amount.prototype.clamp = function (min, max) { return this.ternaryOp(min, max, function (v, min, max) { return Math.max(min, Math.min(v, max)); }); }; Amount.prototype.atMost = function (max) { return this.binaryOp(max, function (v, max) { return Math.min(v, max); }); }; Amount.prototype.atLeast = function (min) { return this.binaryOp(min, function (v, min) { return Math.max(v, min); }); }; Amount.prototype.unitToString = function () { return '' + this.unit; }; Amount.prototype.toString = function () { return this.value + " " + this.unitToString(); }; return Amount; }()); exports.Amount = Amount; //# sourceMappingURL=Core.js.map