@drewsonne/maya-dates
Version:
Typescript package to manipulate dates in the Maya Calendar
145 lines • 6.52 kB
JavaScript
;
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.origin = exports.CalendarRound = exports.getCalendarRound = void 0;
var tzolkin_1 = require("./tzolkin");
var haab_1 = require("./haab");
var distance_number_1 = __importDefault(require("../lc/distance-number"));
var haabMonth_1 = require("./component/haabMonth");
var tzolkinDay_1 = require("./component/tzolkinDay");
var numberCoefficient_1 = __importDefault(require("./component/numberCoefficient"));
var wildcard_1 = require("../wildcard");
var wildcardCoefficient_1 = __importDefault(require("./component/wildcardCoefficient"));
var comment_wrapper_1 = require("../comment-wrapper");
var singleton = {};
function getCalendarRound(tzolkin, haab) {
var crId = "".concat(tzolkin, " ").concat(haab);
if (singleton[crId] === undefined) {
singleton[crId] = new CalendarRound(tzolkin, haab);
}
return singleton[crId];
}
exports.getCalendarRound = getCalendarRound;
var CalendarRound = (function (_super) {
__extends(CalendarRound, _super);
function CalendarRound(tzolkin, haab) {
var _this = _super.call(this) || this;
_this.tzolkin = tzolkin;
_this.haab = haab;
_this.validate();
return _this;
}
CalendarRound.prototype.validate = function () {
var validHaabCoeffs = [];
if ([
tzolkinDay_1.TzolkinDays.KABAN, tzolkinDay_1.TzolkinDays.IK, tzolkinDay_1.TzolkinDays.MANIK, tzolkinDay_1.TzolkinDays.EB,
].map(function (d) { return "".concat(d); }).includes("".concat(this.tzolkin.day))) {
validHaabCoeffs = [0, 5, 10, 15];
}
else if ([
tzolkinDay_1.TzolkinDays.ETZ_NAB, tzolkinDay_1.TzolkinDays.AK_BAL, tzolkinDay_1.TzolkinDays.LAMAT, tzolkinDay_1.TzolkinDays.BEN,
].map(function (d) { return "".concat(d); }).includes("".concat(this.tzolkin.day))) {
validHaabCoeffs = [1, 6, 11, 16];
}
else if ([
tzolkinDay_1.TzolkinDays.KAWAK, tzolkinDay_1.TzolkinDays.K_AN, tzolkinDay_1.TzolkinDays.MULUK, tzolkinDay_1.TzolkinDays.IX,
].map(function (d) { return "".concat(d); }).includes("".concat(this.tzolkin.day))) {
validHaabCoeffs = [2, 7, 12, 17];
}
else if ([
tzolkinDay_1.TzolkinDays.AJAW, tzolkinDay_1.TzolkinDays.CHIKCHAN, tzolkinDay_1.TzolkinDays.OK, tzolkinDay_1.TzolkinDays.MEN,
].map(function (d) { return "".concat(d); }).includes("".concat(this.tzolkin.day))) {
validHaabCoeffs = [3, 8, 13, 18];
}
else if ([
tzolkinDay_1.TzolkinDays.IMIX, tzolkinDay_1.TzolkinDays.KIMI, tzolkinDay_1.TzolkinDays.CHUWEN, tzolkinDay_1.TzolkinDays.KIB,
].map(function (d) { return "".concat(d); }).includes("".concat(this.tzolkin.day))) {
validHaabCoeffs = [4, 9, 14, 19];
}
else {
validHaabCoeffs = Array.from(Array(19).keys());
}
if (this.haab.coeff !== undefined) {
if (!this.haab.coeff.isIn(validHaabCoeffs)) {
throw new Error("".concat(this, " should have Haab coeff in ").concat(validHaabCoeffs, " for day ").concat(this.tzolkin.name));
}
}
};
CalendarRound.prototype.next = function () {
return this.shift(1);
};
CalendarRound.prototype.minus = function (targetCr) {
var foundOrigin = false;
var foundTarget = false;
var current = this;
var count = 0;
var cycleCount = 0;
var result = null;
while (!foundTarget) {
if (current === targetCr) {
result = new distance_number_1.default(foundOrigin
? -(18979 - cycleCount - count)
: count).normalise();
foundTarget = true;
}
else if (current === exports.origin) {
foundOrigin = true;
cycleCount = count;
count = 0;
}
else {
count += 1;
}
current = current.next();
}
if (result instanceof distance_number_1.default) {
return result;
}
else {
throw new Error("Could not assign a DistanceNumber");
}
};
CalendarRound.prototype.match = function (newCr) {
return this.haab.match(newCr.haab) &&
this.tzolkin.match(newCr.tzolkin);
};
CalendarRound.prototype.shift = function (increment) {
return getCalendarRound(this.tzolkin.shift(increment), this.haab.shift(increment));
};
CalendarRound.prototype.isPartial = function () {
return (0, wildcard_1.isWildcard)(this.tzolkin.day)
|| (this.tzolkin.coeff instanceof wildcardCoefficient_1.default)
|| (0, wildcard_1.isWildcard)(this.haab.month)
|| (this.haab.coeff instanceof wildcardCoefficient_1.default);
};
CalendarRound.prototype.toString = function () {
return "".concat(this.tzolkin, " ").concat(this.haab);
};
CalendarRound.prototype.equal = function (other) {
if (other instanceof CalendarRound) {
return this === other;
}
return false;
};
return CalendarRound;
}(comment_wrapper_1.CommentWrapper));
exports.CalendarRound = CalendarRound;
exports.origin = getCalendarRound((0, tzolkin_1.getTzolkin)(new numberCoefficient_1.default(4), (0, tzolkinDay_1.getTzolkinDay)('Ajaw')), (0, haab_1.getHaab)(new numberCoefficient_1.default(8), (0, haabMonth_1.getHaabMonth)('Kumk\'u')));
//# sourceMappingURL=calendar-round.js.map