UNPKG

@drewsonne/maya-dates

Version:

Typescript package to manipulate dates in the Maya Calendar

120 lines 4.63 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.Tzolkin = exports.getTzolkin = void 0; var tzolkinDay_1 = require("./component/tzolkinDay"); var wildcard_1 = require("../wildcard"); var numberCoefficient_1 = __importDefault(require("./component/numberCoefficient")); var wildcardCoefficient_1 = __importDefault(require("./component/wildcardCoefficient")); var comment_wrapper_1 = require("../comment-wrapper"); var singleton = {}; function getTzolkin(coeff, day) { var monthName = "".concat(coeff, " ").concat(day); if (singleton[monthName] === undefined) { singleton[monthName] = new Tzolkin(coeff, day); } return singleton[monthName]; } exports.getTzolkin = getTzolkin; var Tzolkin = (function (_super) { __extends(Tzolkin, _super); function Tzolkin(newCoeff, newDay) { var _this = _super.call(this) || this; _this.day = newDay; _this.coeff = newCoeff; _this.nextHolder = null; _this.validate(); return _this; } Tzolkin.prototype.next = function () { return this.shift(1); }; Tzolkin.prototype.validate = function () { this.coeff.validate(); if (this.day === undefined) { throw new Error('Tzolk\'in day must be provided'); } if (!(0, wildcard_1.isWildcard)(this.day)) { this.day.validate(); } return true; }; Tzolkin.prototype.shift = function (newIncremental) { if (!((0, wildcard_1.isWildcard)(this.day)) && !(this.coeff instanceof wildcardCoefficient_1.default)) { var incremental = newIncremental % 260; if (incremental === 0) { return this; } return this.nextCalculator().shift(incremental - 1); } else { throw new Error("Tzolkin must not have wildcards to shift"); } }; Tzolkin.prototype.nextCalculator = function () { if (this.nextHolder === null) { if (this.coeff instanceof numberCoefficient_1.default) { var newCoeff = (this.coeff.value + 1) % 13; newCoeff = (newCoeff % 13) === 0 ? 13 : newCoeff; if (this.day instanceof tzolkinDay_1.TzolkinDay) { this.nextHolder = getTzolkin(new numberCoefficient_1.default(newCoeff), this.day.shift(1)); } else { throw new Error("this.day must be a TzolkinDay to shift"); } } else { throw new Error('this.coeff is not a NumberCoefficient'); } } return this.nextHolder; }; Tzolkin.prototype.equal = function (other) { if (other instanceof Tzolkin) { return this === other; } return false; }; Tzolkin.prototype.match = function (newTzolkin) { if (this === newTzolkin) { return true; } return (this.coeff.match(newTzolkin.coeff)) && (((0, wildcard_1.isWildcard)(this.day) || (0, wildcard_1.isWildcard)(newTzolkin.day)) ? true : (this.name === newTzolkin.name)); }; Object.defineProperty(Tzolkin.prototype, "name", { get: function () { if ((0, wildcard_1.isWildcard)(this.day)) { return this.day; } return "".concat(this.day); }, enumerable: false, configurable: true }); Tzolkin.prototype.toString = function () { return "".concat(this.coeff, " ").concat(this.name); }; return Tzolkin; }(comment_wrapper_1.CommentWrapper)); exports.Tzolkin = Tzolkin; //# sourceMappingURL=tzolkin.js.map