@drewsonne/maya-dates
Version:
Typescript package to manipulate dates in the Maya Calendar
315 lines • 12.1 kB
JavaScript
"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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var wildcard_1 = require("../wildcard");
var longcount_addition_1 = __importDefault(require("../operations/longcount-addition"));
var longcount_subtraction_1 = __importDefault(require("../operations/longcount-subtraction"));
var comment_wrapper_1 = require("../comment-wrapper");
var DistanceNumber = (function (_super) {
__extends(DistanceNumber, _super);
function DistanceNumber() {
var cycles = [];
for (var _i = 0; _i < arguments.length; _i++) {
cycles[_i] = arguments[_i];
}
var _this = _super.call(this) || this;
_this.parts = cycles;
_this.datePattern = /([\d*]+\.?)+/;
_this.sign = (_this.parts[_this.parts.length - 1] < 0) ? -1 : 1;
if (_this.isNegative) {
var lastComponent = _this.parts[_this.parts.length - 1];
if (typeof lastComponent === 'number') {
_this.parts[_this.parts.length - 1] = -1 * lastComponent;
}
else {
throw new Error("Last component is not a number");
}
}
return _this;
}
Object.defineProperty(DistanceNumber.prototype, "isPositive", {
get: function () {
return this.sign === 1;
},
set: function (newPositive) {
this.sign = newPositive ? 1 : -1;
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "isNegative", {
get: function () {
return this.sign === -1;
},
set: function (newNegative) {
this.isPositive = !newNegative;
},
enumerable: false,
configurable: true
});
DistanceNumber.prototype.equal = function (other) {
if (other instanceof DistanceNumber) {
var thisMinParts = this.sigParts;
var otherMinParts_1 = other.sigParts;
var signEqual = this.sign === other.sign;
var lengthEqual = thisMinParts.length === otherMinParts_1.length;
var partsEqual = thisMinParts.map(function (e, i) {
return (0, wildcard_1.isWildcard)(e) ? (0, wildcard_1.isWildcard)(otherMinParts_1[i]) : e === otherMinParts_1[i];
});
var everyPartEqual = partsEqual.every(function (p) { return p; });
return signEqual && lengthEqual && everyPartEqual;
}
return false;
};
DistanceNumber.prototype.exactlyEqual = function (other) {
var signsEqual = (this.sign === other.sign);
var lengthEqual = (this.parts.length === other.parts.length);
var partsEqual = this.parts.every(function (e, i) {
return (0, wildcard_1.isWildcard)(e) ? (0, wildcard_1.isWildcard)(other.parts[i]) : e === other.parts[i];
});
return signsEqual && lengthEqual && partsEqual;
};
Object.defineProperty(DistanceNumber.prototype, "sigParts", {
get: function () {
var sigParts = [];
for (var i = 0; i < this.parts.length; i++) {
if (this.parts[i] === 0) {
i = this.parts.length;
}
else {
sigParts.push(this.parts[i]);
}
}
return sigParts;
},
enumerable: false,
configurable: true
});
DistanceNumber.prototype.clone = function () {
return new (DistanceNumber.bind.apply(DistanceNumber, __spreadArray([void 0], this.parts, false)))();
};
DistanceNumber.prototype.getDateSections = function (index) {
var part = this.parts[index];
if (part === undefined) {
return 0;
}
return part;
};
DistanceNumber.prototype.setDateSections = function (index, newValue) {
this.parts[index] = newValue;
return this;
};
DistanceNumber.prototype.map = function (fn) {
return this.parts.map(fn);
};
DistanceNumber.prototype.lt = function (newLongCount) {
return this.getPosition() < newLongCount.getPosition();
};
DistanceNumber.prototype.gt = function (newLongCount) {
return this.getPosition() > newLongCount.getPosition();
};
Object.defineProperty(DistanceNumber.prototype, "kIn", {
get: function () {
return this.getDateSections(0);
},
set: function (newKIn) {
this.setDateSections(0, newKIn);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "winal", {
get: function () {
return this.getDateSections(1);
},
set: function (newWinal) {
this.setDateSections(1, newWinal);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "tun", {
get: function () {
return this.getDateSections(2);
},
set: function (newTun) {
this.setDateSections(2, newTun);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "kAtun", {
get: function () {
return this.getDateSections(3);
},
set: function (newKAtun) {
this.setDateSections(3, newKAtun);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "bakTun", {
get: function () {
return this.getDateSections(4);
},
set: function (newBakTun) {
this.setDateSections(4, newBakTun);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "piktun", {
get: function () {
return this.getDateSections(5);
},
set: function (newBakTun) {
this.setDateSections(5, newBakTun);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "kalabtun", {
get: function () {
return this.getDateSections(6);
},
set: function (newBakTun) {
this.setDateSections(6, newBakTun);
},
enumerable: false,
configurable: true
});
Object.defineProperty(DistanceNumber.prototype, "kinchiltun", {
get: function () {
return this.getDateSections(7);
},
set: function (newBakTun) {
this.setDateSections(7, newBakTun);
},
enumerable: false,
configurable: true
});
DistanceNumber.prototype.isValid = function () {
return this.datePattern.test(this.toString());
};
DistanceNumber.prototype.isPartial = function () {
return this.parts.some(function (part) { return (0, wildcard_1.isWildcard)(part); });
};
DistanceNumber.prototype.getPosition = function () {
if (this.isPartial()) {
throw new Error('Can not get position of fullDate dates');
}
var total = 0;
if (!(0, wildcard_1.isWildcard)(this.kIn)) {
total += this.kIn;
}
if (!(0, wildcard_1.isWildcard)(this.winal)) {
total += this.winal * 20;
}
if (!(0, wildcard_1.isWildcard)(this.tun)) {
total += this.tun * 360;
}
if (!(0, wildcard_1.isWildcard)(this.kAtun)) {
total += this.kAtun * 7200;
}
if (!(0, wildcard_1.isWildcard)(this.bakTun)) {
total += this.bakTun * 144000;
}
if (!(0, wildcard_1.isWildcard)(this.piktun)) {
total += this.piktun * 2880000;
}
if (!(0, wildcard_1.isWildcard)(this.kalabtun)) {
total += this.kalabtun * 57600000;
}
if (!(0, wildcard_1.isWildcard)(this.kinchiltun)) {
total += this.kinchiltun * 1152000000;
}
return total * this.sign;
};
DistanceNumber.prototype.plus = function (newLc) {
return new longcount_addition_1.default(DistanceNumber, this, newLc);
};
DistanceNumber.prototype.minus = function (newLc) {
return new longcount_subtraction_1.default(DistanceNumber, this, newLc);
};
DistanceNumber.prototype.normalise = function () {
var totalKIn = this.getPosition();
var norm = new DistanceNumber();
norm.kIn = totalKIn % 20;
norm.winal = (totalKIn - norm.getPosition()) / 20 % 18;
norm.tun = (totalKIn - norm.getPosition()) / 360 % 20;
norm.kAtun = (totalKIn - norm.getPosition()) / 7200 % 20;
norm.bakTun = (totalKIn - norm.getPosition()) / 144000 % 20;
norm.piktun = (totalKIn - norm.getPosition()) / 2880000 % 20;
norm.kalabtun = (totalKIn - norm.getPosition()) / 57600000 % 20;
norm.kinchiltun = (totalKIn - norm.getPosition()) / 1152000000 % 20;
var foundNegative = norm.parts.reduce(function (found, part) { return found || (part < 0); }, false);
this.sign = foundNegative ? -1 : 1;
this.parts = norm.parts.map(function (part) {
if (typeof part === 'number') {
return Math.abs(part);
}
else {
throw new Error("part is not number");
}
});
return this;
};
DistanceNumber.prototype.toString = function () {
var significantDigits = [];
for (var i = this.parts.length - 1; i >= 0; i -= 1) {
var part = this.parts[i];
if (part !== 0) {
significantDigits = this.parts.slice(0, i + 1).reverse();
break;
}
}
for (var i = 0; i < significantDigits.length; i += 1) {
if (significantDigits[i] === undefined) {
significantDigits[i] = 0;
}
}
var dateLength = significantDigits.length;
if (dateLength < 5) {
significantDigits = significantDigits.reverse();
for (var i = 0; i < 5 - dateLength; i += 1) {
significantDigits.push(0);
}
significantDigits = significantDigits.reverse();
}
var stringSignificantDigits = new Array(significantDigits.length);
for (var i = 0; i < significantDigits.length; i += 1) {
var part = significantDigits[i].toString();
stringSignificantDigits[i] = (part.length < 2) ? " ".concat(part) : "".concat(part);
}
return "".concat(this.sign === -1 ? '-' : '').concat(stringSignificantDigits.join('.'));
};
return DistanceNumber;
}(comment_wrapper_1.CommentWrapper));
exports.default = DistanceNumber;
//# sourceMappingURL=distance-number.js.map