UNPKG

@drewsonne/maya-dates

Version:

Typescript package to manipulate dates in the Maya Calendar

135 lines 5.45 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 = void 0; exports.getTzolkin = getTzolkin; var tzolkinDay_1 = require("./component/tzolkinDay"); var wildcard_1 = require("../wildcard"); var numberCoefficient_1 = __importDefault(require("./component/numberCoefficient")); 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]; } 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.adjMod = function (x, n) { return ((x - 1) % n + n) % n + 1; }; Tzolkin.fromDayNumber = function (dayNumber) { var number = Tzolkin.adjMod(dayNumber + Tzolkin.EPOCH_NUMBER, 13); var nameIndex = Tzolkin.adjMod(dayNumber + Tzolkin.EPOCH_NAME_INDEX, 20); return getTzolkin(new numberCoefficient_1.default(number), (0, tzolkinDay_1.getTzolkinDay)(nameIndex)); }; Tzolkin.prototype.next = function () { return this.shift(1); }; Tzolkin.prototype.validate = function () { if (this.coeff instanceof numberCoefficient_1.default) { if (this.coeff.value > 13 || this.coeff.value < 1) { throw new Error('Tzolk\'in coefficient must be between 1 and 13 inclusive.'); } } 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 (this.coeff instanceof numberCoefficient_1.default && this.day instanceof tzolkinDay_1.TzolkinDay) { var incremental = newIncremental % 260; if (incremental === 0) { return this; } var newCoeff = Tzolkin.adjMod(this.coeff.value + incremental, 13); var newDayPosition = Tzolkin.adjMod(this.day.position + incremental, 20); return getTzolkin(new numberCoefficient_1.default(newCoeff), (0, tzolkinDay_1.getTzolkinDay)(newDayPosition)); } 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); }; Tzolkin.EPOCH_NUMBER = 4; Tzolkin.EPOCH_NAME_INDEX = 20; return Tzolkin; }(comment_wrapper_1.CommentWrapper)); exports.Tzolkin = Tzolkin; //# sourceMappingURL=tzolkin.js.map