data-unit
Version:
Simple TypeScript/ES2017 classes to represent unit values (like time/data size)
155 lines • 5.93 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Amount = /** @class */ (function () {
function Amount(value, unit) {
this.value = value;
this.unit = unit;
}
Object.defineProperty(Amount.prototype, "previousUnit", {
get: function () {
return (this.unit - 1);
},
enumerable: true,
configurable: true
});
Object.defineProperty(Amount.prototype, "nextUnit", {
get: function () {
return (this.unit + 1);
},
enumerable: true,
configurable: true
});
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) {
var value = this.as(unit);
if (value === null) {
return null;
}
return this.create(value, unit);
};
Amount.prototype.as = function (unit, decimals) {
if (unit === void 0) { unit = this.unit; }
if (decimals === void 0) { decimals = null; }
// If invalid unit
if (unit < 0 || unit >= this.exchangeRates.length) {
return 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.toHuman = function () {
var cursor = this, backtrack = this;
if (cursor.value < 1) {
// We'll go lower
while (cursor != null) {
cursor = cursor.convert(cursor.previousUnit);
// Test if the lower unit is still bigger than 1
// If not, cancel the search and we will use the value saved on backtrack as the closest we got
if (cursor == null || cursor.value < 1) {
break;
}
backtrack = cursor;
}
}
else {
// We'll go up
while (cursor != null) {
cursor = cursor.convert(cursor.nextUnit);
// Test if the lower unit is still bigger than 1
// If not, cancel the search and we will use the value saved on backtrack as the closest we got
if (cursor == null || cursor.value < 1) {
break;
}
backtrack = cursor;
}
}
return backtrack;
};
Amount.prototype.toHumanString = function () {
return this.toHuman().toString();
};
Amount.prototype.toString = function () {
return this.value + " " + this.unitToString();
};
return Amount;
}());
exports.Amount = Amount;
//# sourceMappingURL=Amount.js.map