UNPKG

data-unit

Version:

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

147 lines 6.69 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var Amount_1 = require("./Amount"); var DurationParseError = /** @class */ (function (_super) { __extends(DurationParseError, _super); function DurationParseError() { return _super !== null && _super.apply(this, arguments) || this; } return DurationParseError; }(Error)); exports.DurationParseError = DurationParseError; var DurationUnit; (function (DurationUnit) { DurationUnit[DurationUnit["MILLISECONDS"] = 0] = "MILLISECONDS"; DurationUnit[DurationUnit["SECONDS"] = 1] = "SECONDS"; DurationUnit[DurationUnit["MINUTES"] = 2] = "MINUTES"; DurationUnit[DurationUnit["HOURS"] = 3] = "HOURS"; DurationUnit[DurationUnit["DAYS"] = 4] = "DAYS"; DurationUnit[DurationUnit["WEEKS"] = 5] = "WEEKS"; })(DurationUnit = exports.DurationUnit || (exports.DurationUnit = {})); var Duration = /** @class */ (function (_super) { __extends(Duration, _super); function Duration() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.exchangeRates = [ { unit: DurationUnit.MILLISECONDS, multiplier: 1 }, { unit: DurationUnit.SECONDS, multiplier: 1000 }, { unit: DurationUnit.MINUTES, multiplier: 60 }, { unit: DurationUnit.HOURS, multiplier: 60 }, { unit: DurationUnit.DAYS, multiplier: 24 }, { unit: DurationUnit.WEEKS, multiplier: 7 }, ]; return _this; } Duration.isValid = function (value) { var validString = (typeof value === 'string' && (Duration.unitPattern.test(value) || Duration.aggregatePattern.test(value))); return value && (typeof value === 'number' || validString); }; Duration.parse = function (value) { if (value === void 0 || value === null) { throw new DurationParseError("Value is null."); } if (value instanceof Duration) { return value; } else if (typeof value === 'number') { return new Duration(value, DurationUnit.SECONDS); } else if (typeof value === 'string') { var match = value.match(Duration.unitPattern); if (match) { var value_1 = +match[1]; var unit = Duration.unitKeywords[match[2].toLowerCase()]; return new Duration(value_1, unit); } else { match = value.match(Duration.aggregatePattern); if (!match) { throw new DurationParseError("Duration value string has invalid format."); } var segments_1 = value.split(':'); var base_1 = 1; // If the last element (seconds) contains a period, then anything after the period should be treated as milliseconds if (segments_1[segments_1.length - 1].indexOf('.') >= 0) { // Remove the last element (that stores seconds, segments.length - 1), and append two items segments_1.splice.apply(segments_1, [segments_1.length - 1, 1].concat(segments_1[segments_1.length - 1].split('.'))); base_1 = 0; } var exchangeRates_1 = (new Duration(0, DurationUnit.SECONDS)).exchangeRates; var units = segments_1.map(function (value, index) { var unitIndex = base_1 + (segments_1.length - index - 1); var unit = exchangeRates_1[unitIndex].unit; return new Duration(+value, unit); }); return units.reduce(function (a, b) { return a.plus(b); }, new Duration(0, DurationUnit.SECONDS)); } } else { throw new DurationParseError("Value should be a Duration, number or string."); } }; Duration.tryParse = function (value) { try { return Duration.parse(value); } catch (error) { if (error instanceof DurationParseError) { return null; } throw error; } }; // Override Duration.prototype.unitToString = function () { var _this = this; var keys = Object.keys(Duration.unitKeywords); return keys.find(function (key) { return Duration.unitKeywords[key] === _this.unit; }); }; Duration.prototype.toHumanShortString = function () { var minutes = Math.floor(this.as(DurationUnit.MINUTES)); var seconds = Math.floor(this.as(DurationUnit.SECONDS) % 60); return minutes + ":" + seconds; }; // Override Duration.prototype.toHumanString = function (showMilliseconds) { if (showMilliseconds === void 0) { showMilliseconds = true; } var hours = Math.floor(this.as(DurationUnit.HOURS)); var minutes = Math.floor(this.as(DurationUnit.MINUTES)); var seconds = Math.floor(this.as(DurationUnit.SECONDS) % 60); var milliseconds = this.as(DurationUnit.MILLISECONDS) % 1000; var hoursStr = String(hours).padStart(2, '0'); var minutesStr = String(minutes).padStart(2, '0'); var secondsStr = String(seconds).padStart(2, '0'); var millisecondsStr = String(milliseconds).padStart(3, '0'); var string = hoursStr + ":" + minutesStr + ":" + secondsStr; if (showMilliseconds) { string += "." + millisecondsStr; } return string; }; Duration.unitPattern = /(\d+)\s*(w|d|h|m|s|ms)/i; Duration.aggregatePattern = /(\d+)(:\d+){0,4}(\.\d+)?/; Duration.unitKeywords = { 'ms': DurationUnit.MILLISECONDS, 's': DurationUnit.SECONDS, 'm': DurationUnit.MINUTES, 'h': DurationUnit.HOURS, 'd': DurationUnit.DAYS, 'w': DurationUnit.WEEKS }; return Duration; }(Amount_1.Amount)); exports.Duration = Duration; //# sourceMappingURL=Duration.js.map