UNPKG

@drewsonne/maya-dates

Version:

Typescript package to manipulate dates in the Maya Calendar

139 lines 5.52 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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Haab = exports.getHaab = void 0; var haabMonth_1 = require("./component/haabMonth"); var wildcard_1 = require("../wildcard"); var numberCoefficient_1 = __importDefault(require("./component/numberCoefficient")); var coefficient_1 = require("./component/coefficient"); var comment_wrapper_1 = require("../comment-wrapper"); var singleton = {}; function getHaab(coeff, month) { var monthName = "".concat(coeff, " ").concat(month); if (singleton[monthName] === undefined) { singleton[monthName] = new Haab(coeff, month); } return singleton[monthName]; } exports.getHaab = getHaab; var Haab = (function (_super) { __extends(Haab, _super); function Haab(coeff, month) { var _this = _super.call(this) || this; _this.month = month; _this.coeff = coeff; _this.nextHolder = null; _this.validate(); return _this; } Haab.prototype.validate = function () { if (this.coeff instanceof numberCoefficient_1.default) { if (this.coeff.value > 19 || this.coeff.value < 0) { throw new Error('Haab\' coefficient must inclusively between 0 and 19.'); } if (this.month === (0, haabMonth_1.getHaabMonth)(haabMonth_1.HaabMonths.WAYEB)) { if (this.coeff.value > 4) { throw new Error('Haab\' coefficient for Wayeb must inclusively between 0 and 4.'); } } } if (this.month === undefined) { throw new Error('Haab\' month must be provided'); } if (!((0, wildcard_1.isWildcard)(this.month))) { this.month.validate(); } return true; }; Haab.prototype.next = function () { return this.shift(1); }; Haab.prototype.match = function (otherHaab) { return (this.coeff.match(otherHaab.coeff)) && (((0, wildcard_1.isWildcard)(this.month) || (0, wildcard_1.isWildcard)(otherHaab.month)) ? true : (this.name === otherHaab.name)); }; Object.defineProperty(Haab.prototype, "name", { get: function () { return "".concat(this.month); }, enumerable: false, configurable: true }); Haab.prototype.shift = function (numDays) { if (!(0, wildcard_1.isWildcard)(this.month)) { if (this.coeff instanceof numberCoefficient_1.default) { var incremental = numDays % 365; if (incremental === 0) { return this; } return this.nextCalculator().shift(incremental - 1); } else { throw new Error("Coefficient is not an integer"); } } else { throw new Error("Can not shift Haab date with wildcard"); } }; Haab.prototype.nextCalculator = function () { if (this.nextHolder === null) { var wayeb = (0, haabMonth_1.getHaabMonth)(19); var isWayeb = this.month === wayeb; var monthLength = (isWayeb) ? 5 : 20; if (this.coeff instanceof numberCoefficient_1.default) { if (1 + this.coeff.value >= monthLength) { if (this.month instanceof haabMonth_1.HaabMonth) { var newMonth = this.month.shift(1); if (newMonth instanceof haabMonth_1.HaabMonth) { this.nextHolder = getHaab((0, coefficient_1.coefficientParser)(0), newMonth); } else { throw new Error("Unexpected HaabMonth type"); } } else { throw new Error("Month must not be wildcard to shift"); } } else { this.nextHolder = getHaab(this.coeff.increment(), this.month); } } else { throw new Error("Month Coefficient must not be a wildcard to shift"); } } return this.nextHolder; }; Haab.prototype.toString = function () { return "".concat(this.coeff, " ").concat(this.name); }; Haab.prototype.equal = function (other) { if (other instanceof Haab) { return other === this; } return false; }; return Haab; }(comment_wrapper_1.CommentWrapper)); exports.Haab = Haab; //# sourceMappingURL=haab.js.map