From 9dc0b6b83db137daa0f178227112a220a89bfa35 Mon Sep 17 00:00:00 2001 From: Drew Date: Sun, 5 Jan 2020 12:39:29 +0000 Subject: [PATCH] Format code --- src/cr/calendar-round.js | 256 +++++----- src/cr/haab.js | 400 +++++++-------- src/cr/index.js | 12 +- src/cr/tzolkin.js | 376 +++++++-------- src/factory/base.js | 66 +-- src/factory/calendar-round.js | 48 +- src/factory/full-date.js | 28 +- src/factory/index.js | 6 +- src/factory/long-count.js | 50 +- src/full-date.js | 36 +- src/index.js | 10 +- src/lc/index.js | 4 +- src/lc/long-count.js | 562 +++++++++++----------- src/lc/night/lord-of-night.js | 42 +- src/operations/calendar-round-wildcard.js | 128 ++--- src/operations/fulldate-wildcard.js | 86 ++-- src/operations/index.js | 6 +- src/operations/longcount-wildcard.js | 60 +-- src/wildcard.js | 14 +- 19 files changed, 1095 insertions(+), 1095 deletions(-) diff --git a/src/cr/calendar-round.js b/src/cr/calendar-round.js index 10d0708..d797439 100644 --- a/src/cr/calendar-round.js +++ b/src/cr/calendar-round.js @@ -11,150 +11,150 @@ const wildcard = require('../wildcard'); * let cr = new CalendarRound(4, "Ajaw", 8, "Kumk'u"); */ class CalendarRound { + /** + * + * @param {number} tzolkin_coeff Coefficient for the 260-day cycle + * @param {string} tzolkin_day Name of the name in the 260-day cycle + * @param {number} haab_coeff Day in the Haab month + * @param {string} haab_month Name of the Haab month + */ + constructor(tzolkin_coeff, tzolkin_day, haab_coeff, haab_month) { /** - * - * @param {number} tzolkin_coeff Coefficient for the 260-day cycle - * @param {string} tzolkin_day Name of the name in the 260-day cycle - * @param {number} haab_coeff Day in the Haab month - * @param {string} haab_month Name of the Haab month + * 260-day cycle component of the Calendar Round + * @type {Tzolkin} */ - constructor(tzolkin_coeff, tzolkin_day, haab_coeff, haab_month) { - /** - * 260-day cycle component of the Calendar Round - * @type {Tzolkin} - */ - this.tzolkin = new tzolkin.Tzolkin(tzolkin_coeff, tzolkin_day); - /** - * Haab cycle component of the Calendar Round - * @type {Haab} - */ - this.haab = new haab.Haab(haab_coeff, haab_month); - - this.validate(); - } - + this.tzolkin = new tzolkin.Tzolkin(tzolkin_coeff, tzolkin_day); /** - * Validate that the Calendar Round has a correct 260-day and Haab - * configuration + * Haab cycle component of the Calendar Round + * @type {Haab} */ - validate() { - let valid_haab_coeffs = []; - if ([ - 'Kaban', 'Ik\'', 'Manik\'', 'Eb', - ].includes(this.tzolkin.name)) { - valid_haab_coeffs = [0, 5, 10, 15]; - } else if ([ - 'Etz\'nab', 'Ak\'bal', 'Lamat', 'Ben', - ].includes(this.tzolkin.name)) { - valid_haab_coeffs = [1, 6, 11, 16]; - } else if ([ - 'Kawak', 'K\'an', 'Muluk', 'Ix', - ].includes(this.tzolkin.name)) { - valid_haab_coeffs = [2, 7, 12, 17]; - } else if ([ - 'Ajaw', 'Chikchan', 'Ok', 'Men', - ].includes(this.tzolkin.name)) { - valid_haab_coeffs = [3, 8, 13, 18]; - } else if ([ - 'Imix', 'Kimi', 'Chuwen', 'Kib', - ].includes(this.tzolkin.name)) { - valid_haab_coeffs = [4, 9, 14, 19]; - } else if ([wildcard].includes(this.tzolkin.name)) { - valid_haab_coeffs = [...Array(19).keys()]; - } else { - throw `Could not allocate Tzolk'in (${this.tzolkin.name}) to permissible month coeffs.`; - } + this.haab = new haab.Haab(haab_coeff, haab_month); - valid_haab_coeffs.push(wildcard); + this.validate(); + } - if (!valid_haab_coeffs.includes(this.haab.coeff)) { - throw `${this} should have Haab coeff in ${valid_haab_coeffs} for day ${this.tzolkin.name}`; - } + /** + * Validate that the Calendar Round has a correct 260-day and Haab + * configuration + */ + validate() { + let valid_haab_coeffs = []; + if ([ + 'Kaban', 'Ik\'', 'Manik\'', 'Eb', + ].includes(this.tzolkin.name)) { + valid_haab_coeffs = [0, 5, 10, 15]; + } else if ([ + 'Etz\'nab', 'Ak\'bal', 'Lamat', 'Ben', + ].includes(this.tzolkin.name)) { + valid_haab_coeffs = [1, 6, 11, 16]; + } else if ([ + 'Kawak', 'K\'an', 'Muluk', 'Ix', + ].includes(this.tzolkin.name)) { + valid_haab_coeffs = [2, 7, 12, 17]; + } else if ([ + 'Ajaw', 'Chikchan', 'Ok', 'Men', + ].includes(this.tzolkin.name)) { + valid_haab_coeffs = [3, 8, 13, 18]; + } else if ([ + 'Imix', 'Kimi', 'Chuwen', 'Kib', + ].includes(this.tzolkin.name)) { + valid_haab_coeffs = [4, 9, 14, 19]; + } else if ([wildcard].includes(this.tzolkin.name)) { + valid_haab_coeffs = [...Array(19).keys()]; + } else { + throw `Could not allocate Tzolk'in (${this.tzolkin.name}) to permissible month coeffs.`; } - /** - * Increment both the Haab and 260-day cycle to the next day in the Calendar Round - * @returns {CalendarRound} - */ - next() { - let new_cr = this.clone(); - new_cr.tzolkin = this.tzolkin.next(); - new_cr.haab = this.haab.next(); - new_cr.validate(); - return new_cr; - } + valid_haab_coeffs.push(wildcard); - /** - * Check that this CalendarRound matches another CalendarRound. If one CR has - * wildcards and the other does not, this function will return false. - * @param {CalendarRound} new_cr - * @return {Boolean} - */ - equal(new_cr) { - return this.haab.equal(new_cr.haab) && - this.tzolkin.equal(new_cr.tzolkin); + if (!valid_haab_coeffs.includes(this.haab.coeff)) { + throw `${this} should have Haab coeff in ${valid_haab_coeffs} for day ${this.tzolkin.name}`; } + } - /** - * Check that this Calendar Round matches another CalendarRound. If one CR has - * wildcards and the other does not, this function will return true. - * @param {CalendarRound} new_cr - * @return {boolean} - */ - match(new_cr) { - let haab_matches = this.haab.match(new_cr.haab); - let tzolkin_matches = this.tzolkin.match(new_cr.tzolkin); - return haab_matches && tzolkin_matches; - } + /** + * Increment both the Haab and 260-day cycle to the next day in the Calendar Round + * @returns {CalendarRound} + */ + next() { + let new_cr = this.clone(); + new_cr.tzolkin = this.tzolkin.next(); + new_cr.haab = this.haab.next(); + new_cr.validate(); + return new_cr; + } - /** - * Shift a CalendarRound date forward through time. Does not modify this - * object and will return a new object. - * @param {number} increment - * @return {CalendarRound} - */ - shift(increment) { - let new_cr = this.clone(); - new_cr.haab = new_cr.haab.shift(increment); - new_cr.tzolkin = new_cr.tzolkin.shift(increment); - return new_cr; - } + /** + * Check that this CalendarRound matches another CalendarRound. If one CR has + * wildcards and the other does not, this function will return false. + * @param {CalendarRound} new_cr + * @return {Boolean} + */ + equal(new_cr) { + return this.haab.equal(new_cr.haab) && + this.tzolkin.equal(new_cr.tzolkin); + } - /** - * Return a brand new object with the same configuration as this object. - * @return {CalendarRound} - */ - clone() { - return new CalendarRound( - this.tzolkin.coeff, - this.tzolkin.day, - this.haab.coeff, - this.haab.month, - ); - } + /** + * Check that this Calendar Round matches another CalendarRound. If one CR has + * wildcards and the other does not, this function will return true. + * @param {CalendarRound} new_cr + * @return {boolean} + */ + match(new_cr) { + let haab_matches = this.haab.match(new_cr.haab); + let tzolkin_matches = this.tzolkin.match(new_cr.tzolkin); + return haab_matches && tzolkin_matches; + } - /** - * Return true, if this function has any wildcard portions. - * @return {boolean} - */ - is_partial() { - return (this.tzolkin.day === wildcard) || - (this.tzolkin.coeff === wildcard) || - (this.haab.month === wildcard) || - (this.haab.coeff === wildcard); - } + /** + * Shift a CalendarRound date forward through time. Does not modify this + * object and will return a new object. + * @param {number} increment + * @return {CalendarRound} + */ + shift(increment) { + let new_cr = this.clone(); + new_cr.haab = new_cr.haab.shift(increment); + new_cr.tzolkin = new_cr.tzolkin.shift(increment); + return new_cr; + } - /** - * Render the CalendarRound cycle date as a string - * @returns {string} - */ - toString(is_numeric) { - if (is_numeric) { - return `${this.tzolkin.toString(is_numeric)}:${this.haab.toString( - is_numeric)}`; - } - return `${this.tzolkin} ${this.haab}`; + /** + * Return a brand new object with the same configuration as this object. + * @return {CalendarRound} + */ + clone() { + return new CalendarRound( + this.tzolkin.coeff, + this.tzolkin.day, + this.haab.coeff, + this.haab.month, + ); + } + + /** + * Return true, if this function has any wildcard portions. + * @return {boolean} + */ + is_partial() { + return (this.tzolkin.day === wildcard) || + (this.tzolkin.coeff === wildcard) || + (this.haab.month === wildcard) || + (this.haab.coeff === wildcard); + } + + /** + * Render the CalendarRound cycle date as a string + * @returns {string} + */ + toString(is_numeric) { + if (is_numeric) { + return `${this.tzolkin.toString(is_numeric)}:${this.haab.toString( + is_numeric)}`; } + return `${this.tzolkin} ${this.haab}`; + } } module.exports = CalendarRound; diff --git a/src/cr/haab.js b/src/cr/haab.js index 337f297..50fc591 100644 --- a/src/cr/haab.js +++ b/src/cr/haab.js @@ -11,148 +11,148 @@ const wildcard = require('../wildcard'); * */ class Haab { - /** - * Constructor - * @param {number|Wildcard} coeff - The position in the Haab month for this date - * @param {string|HaabMonth|Wildcard} month - */ - constructor(coeff, month) { - if (coeff === '*') { - coeff = wildcard; - } else if (coeff !== wildcard) { - coeff = parseInt(coeff); - } - if (typeof month === 'string') { - if (month === '*') { - month = wildcard; - } else { - month = new HaabMonth(month); - } - } - /** - * @type {HaabMonth|Wildcard} - */ - this.month = month; - /** - * @type {number|Wildcard} - */ - this.coeff = coeff; - - this.validate(); + /** + * Constructor + * @param {number|Wildcard} coeff - The position in the Haab month for this date + * @param {string|HaabMonth|Wildcard} month + */ + constructor(coeff, month) { + if (coeff === '*') { + coeff = wildcard; + } else if (coeff !== wildcard) { + coeff = parseInt(coeff); } - - /** - * Ensure the Haab's coefficients are within range and the month is defined - * @return {boolean} - */ - validate() { - if (this.coeff > 19 || this.coeff < 0) { - throw 'Haab\' coefficient must inclusively between 0 and 19.'; - } - if (this.name === 'Wayeb' && this.coeff > 4) { - throw 'Haab\' coefficient for Wayeb must inclusively between 0 and 4.'; - } - if (this.month === undefined) { - throw 'Haab\' month must be provided'; - } - if (this.month !== wildcard) { - this.month.validate(); - } - - return true; + if (typeof month === 'string') { + if (month === '*') { + month = wildcard; + } else { + month = new HaabMonth(month); + } } - /** - * Return the next day in the Haab cycle - * @returns {Haab} + * @type {HaabMonth|Wildcard} */ - next() { - return this.shift(1); - } - + this.month = month; /** - * Ensure this Haab object has the same configuration as another Haab object. - * Does not take wildcards into account. - * @param {Haab} new_haab - * @return {boolean} + * @type {number|Wildcard} */ - equal(new_haab) { - return (this.coeff === new_haab.coeff) && - (this.name === new_haab.name); + this.coeff = coeff; + + this.validate(); + } + + /** + * Ensure the Haab's coefficients are within range and the month is defined + * @return {boolean} + */ + validate() { + if (this.coeff > 19 || this.coeff < 0) { + throw 'Haab\' coefficient must inclusively between 0 and 19.'; } - - /** - * Ensure this Haab object has a matching configuration as another Haab object. - * Takes wildcards into account. - * @param {Haab} new_haab - * @return {boolean} - */ - match(new_haab) { - return ( - (this.coeff === wildcard || new_haab.coeff === wildcard) ? - true : - (this.coeff === new_haab.coeff) - ) && ( - (this.month === wildcard || new_haab.month === wildcard) ? - true : - (this.name === new_haab.name) - ); + if (this.name === 'Wayeb' && this.coeff > 4) { + throw 'Haab\' coefficient for Wayeb must inclusively between 0 and 4.'; } - - /** - * Return a string representation of the Haab month name - * @returns {string} - */ - get name() { - if (this.month === wildcard) { - return this.month; - } - return this.month.name; + if (this.month === undefined) { + throw 'Haab\' month must be provided'; } - - /** - * - * @param {number} incremental - */ - shift(incremental) { - let new_date = this.clone(); - while (incremental > 0) { - let month_length = (new_date.name === this.month.months[19]) ? 5 : 20; - if (incremental + new_date.coeff >= month_length) { - let distance_to_month_end = month_length - new_date.coeff; - new_date.coeff = 0; - new_date.month = new_date.month.shift(1); - incremental -= distance_to_month_end; - } else { - new_date.coeff += incremental; - incremental = 0; - } - } - new_date.validate(); - return new_date; + if (this.month !== wildcard) { + this.month.validate(); } - /** - * Render the Haab date as a string - * @returns {string} - */ - toString(is_numeric) { - if (is_numeric) { - return `${this.coeff}:${this.month.month_position}`; - } - return `${this.coeff} ${this.name}`; + return true; + } + + /** + * Return the next day in the Haab cycle + * @returns {Haab} + */ + next() { + return this.shift(1); + } + + /** + * Ensure this Haab object has the same configuration as another Haab object. + * Does not take wildcards into account. + * @param {Haab} new_haab + * @return {boolean} + */ + equal(new_haab) { + return (this.coeff === new_haab.coeff) && + (this.name === new_haab.name); + } + + /** + * Ensure this Haab object has a matching configuration as another Haab object. + * Takes wildcards into account. + * @param {Haab} new_haab + * @return {boolean} + */ + match(new_haab) { + return ( + (this.coeff === wildcard || new_haab.coeff === wildcard) ? + true : + (this.coeff === new_haab.coeff) + ) && ( + (this.month === wildcard || new_haab.month === wildcard) ? + true : + (this.name === new_haab.name) + ); + } + + /** + * Return a string representation of the Haab month name + * @returns {string} + */ + get name() { + if (this.month === wildcard) { + return this.month; } - - /** - * Return a brand new object with the same configuration as this object. - * @return {Haab} - */ - clone() { - return new Haab( - this.coeff, - this.month, - ); + return this.month.name; + } + + /** + * + * @param {number} incremental + */ + shift(incremental) { + let new_date = this.clone(); + while (incremental > 0) { + let month_length = (new_date.name === this.month.months[19]) ? 5 : 20; + if (incremental + new_date.coeff >= month_length) { + let distance_to_month_end = month_length - new_date.coeff; + new_date.coeff = 0; + new_date.month = new_date.month.shift(1); + incremental -= distance_to_month_end; + } else { + new_date.coeff += incremental; + incremental = 0; + } + } + new_date.validate(); + return new_date; + } + + /** + * Render the Haab date as a string + * @returns {string} + */ + toString(is_numeric) { + if (is_numeric) { + return `${this.coeff}:${this.month.month_position}`; } + return `${this.coeff} ${this.name}`; + } + + /** + * Return a brand new object with the same configuration as this object. + * @return {Haab} + */ + clone() { + return new Haab( + this.coeff, + this.month, + ); + } } @@ -160,89 +160,89 @@ class Haab { * Describes only the month component of a Haab date */ class HaabMonth { - /** - * @param {string} name - Name of the Haab month - */ - constructor(name) { - - /** - * @type {Map} - */ - this.months = [ - undefined, - 'Pop', - 'Wo', - 'Sip', - 'Sotz\'', - 'Sek', - 'Xul', - 'Yaxk\'in', - 'Mol', - 'Ch\'en', - 'Yax', - 'Sak', - 'Keh', - 'Mak', - 'K\'ank\'in', - 'Muwan', - 'Pax', - 'K\'ayab', - 'Kumk\'u', - 'Wayeb', - ]; - - if (typeof name === 'number') { - name = this.months[name]; - } - - /** - * Name of the Haab month - * @type {string} - */ - this.name = name; - - /** - * @type {number} - */ - this.month_position = this.months.findIndex( - m => m === this.name); - } + /** + * @param {string} name - Name of the Haab month + */ + constructor(name) { /** - * Return the next month in the Haab cycle - * @returns {HaabMonth} + * @type {Map} */ - next() { - return this.shift(1); + this.months = [ + undefined, + 'Pop', + 'Wo', + 'Sip', + 'Sotz\'', + 'Sek', + 'Xul', + 'Yaxk\'in', + 'Mol', + 'Ch\'en', + 'Yax', + 'Sak', + 'Keh', + 'Mak', + 'K\'ank\'in', + 'Muwan', + 'Pax', + 'K\'ayab', + 'Kumk\'u', + 'Wayeb', + ]; + + if (typeof name === 'number') { + name = this.months[name]; } /** - * Ensure a Haab month name is defined, and that the month name is within the - * set of allowable values. + * Name of the Haab month + * @type {string} */ - validate() { - if (this.name === undefined) { - throw 'Haab\' month name must be provided'; - } - if (!this.months.includes(this.name)) { - throw `Haab' day (${this.name}) must be in ${this.months}`; - } - } + this.name = name; /** - * Shift a HaabMonth date forward through time. Does not modify this - * object and will return a new object. - * @param {number} increment - Number of months to move forward - * @return {HaabMonth} + * @type {number} */ - shift(increment) { - let new_incremental = (this.month_position + increment) % 19; - new_incremental = (new_incremental === 0) ? 19 : new_incremental; - return new HaabMonth(new_incremental); + this.month_position = this.months.findIndex( + m => m === this.name); + } + + /** + * Return the next month in the Haab cycle + * @returns {HaabMonth} + */ + next() { + return this.shift(1); + } + + /** + * Ensure a Haab month name is defined, and that the month name is within the + * set of allowable values. + */ + validate() { + if (this.name === undefined) { + throw 'Haab\' month name must be provided'; + } + if (!this.months.includes(this.name)) { + throw `Haab' day (${this.name}) must be in ${this.months}`; } + } + + /** + * Shift a HaabMonth date forward through time. Does not modify this + * object and will return a new object. + * @param {number} increment - Number of months to move forward + * @return {HaabMonth} + */ + shift(increment) { + let new_incremental = (this.month_position + increment) % 19; + new_incremental = (new_incremental === 0) ? 19 : new_incremental; + return new HaabMonth(new_incremental); + } } module.exports = { - 'Haab': Haab, - 'HaabMonth': HaabMonth, + 'Haab': Haab, + 'HaabMonth': HaabMonth, }; diff --git a/src/cr/index.js b/src/cr/index.js index db6e5ed..9b81c39 100644 --- a/src/cr/index.js +++ b/src/cr/index.js @@ -1,10 +1,10 @@ /** @ignore */ const CalendarRound = require('./calendar-round'); module.exports = { - 'CalendarRound': CalendarRound, - 'tzolkin': require('./tzolkin'), - 'haab': require('./haab'), - 'origin': new CalendarRound( - 4, 'Ajaw', - 8, 'Kumk\'u'), + 'CalendarRound': CalendarRound, + 'tzolkin': require('./tzolkin'), + 'haab': require('./haab'), + 'origin': new CalendarRound( + 4, 'Ajaw', + 8, 'Kumk\'u'), }; diff --git a/src/cr/tzolkin.js b/src/cr/tzolkin.js index ca2123a..71abb17 100644 --- a/src/cr/tzolkin.js +++ b/src/cr/tzolkin.js @@ -11,130 +11,130 @@ const wildcard = require('../wildcard'); * */ class Tzolkin { - /** - * Constructor - * @param {number} coeff - The position in the 260-day cycle - * @param {string|TzolkinDay} day - */ - constructor(coeff, day) { - if (coeff !== undefined) { - if (coeff === '*') { - coeff = wildcard; - } else if (coeff !== wildcard) { - coeff = parseInt(coeff); - } - } - if (day !== undefined) { - if (typeof day === 'string') { - if (day === '*') { - day = wildcard; - } else { - day = _get_day(day); - } - } - } - /** - * @type {TzolkinDay} - */ - this.day = day; - /** - * @type {number} - */ - this.coeff = coeff; - - this.validate(); - } - - /** - * Return the next day in the 260-day cycle - * @returns {Tzolkin} - */ - next() { - return this.shift(1); + /** + * Constructor + * @param {number} coeff - The position in the 260-day cycle + * @param {string|TzolkinDay} day + */ + constructor(coeff, day) { + if (coeff !== undefined) { + if (coeff === '*') { + coeff = wildcard; + } else if (coeff !== wildcard) { + coeff = parseInt(coeff); + } } - - /** - * Ensure the Tzolkin's coefficients are within range and the day is defined - * @return {boolean} - */ - validate() { - if (this.coeff > 13 || this.coeff < 1) { - throw 'Tzolk\'in coefficient must inclusively between 1 and 13.'; - } - if (this.day === undefined) { - throw 'Tzolk\'in day must be provided'; - } - if (this.day !== wildcard) { - this.day.validate(); + if (day !== undefined) { + if (typeof day === 'string') { + if (day === '*') { + day = wildcard; + } else { + day = _get_day(day); } - return true; + } } - /** - * - * @param {Number} incremental - * @return {Tzolkin} + * @type {TzolkinDay} */ - shift(incremental) { - let new_coeff = (this.coeff + incremental) % 13; - new_coeff = (new_coeff % 13) === 0 ? 13 : new_coeff; - let new_day = this.day.shift(incremental); - - let new_tzolkin = new Tzolkin(new_coeff, new_day); - new_tzolkin.validate(); - return new_tzolkin; - } - + this.day = day; /** - * Ensure this Tzolkin object has the same configuration as another Tzolkin - * object. Does not take wildcards into account. - * @param {Tzolkin} new_tzolkin - * @return {boolean} + * @type {number} */ - equal(new_tzolkin) { - return (this.coeff === new_tzolkin.coeff) && - (this.name === new_tzolkin.name); + this.coeff = coeff; + + this.validate(); + } + + /** + * Return the next day in the 260-day cycle + * @returns {Tzolkin} + */ + next() { + return this.shift(1); + } + + /** + * Ensure the Tzolkin's coefficients are within range and the day is defined + * @return {boolean} + */ + validate() { + if (this.coeff > 13 || this.coeff < 1) { + throw 'Tzolk\'in coefficient must inclusively between 1 and 13.'; } - - /** - * Ensure this Tzolkin object has a matching configuration as another Tzolkin - * object. Takes wildcards into account. - * @param {Tzolkin} new_tzolkin - * @return {boolean} - */ - match(new_tzolkin) { - return ( - (this.coeff === wildcard || new_tzolkin.coeff === wildcard) ? - true : - (this.coeff === new_tzolkin.coeff) - ) && ( - (this.day === wildcard || new_tzolkin.day === wildcard) ? - true : - (this.name === new_tzolkin.name) - ); + if (this.day === undefined) { + throw 'Tzolk\'in day must be provided'; } - - /** - * Return a string representation of the 260-day cycle name - * @returns {string} - */ - get name() { - if (this.day === wildcard) { - return this.day; - } - return this.day.name; + if (this.day !== wildcard) { + this.day.validate(); } - - /** - * Render the 260-day cycle date as a string - * @returns {string} - */ - toString(is_numeric) { - if (is_numeric) { - return `${this.coeff}:${this.day.day_position}`; - } - return `${this.coeff} ${this.name}`; + return true; + } + + /** + * + * @param {Number} incremental + * @return {Tzolkin} + */ + shift(incremental) { + let new_coeff = (this.coeff + incremental) % 13; + new_coeff = (new_coeff % 13) === 0 ? 13 : new_coeff; + let new_day = this.day.shift(incremental); + + let new_tzolkin = new Tzolkin(new_coeff, new_day); + new_tzolkin.validate(); + return new_tzolkin; + } + + /** + * Ensure this Tzolkin object has the same configuration as another Tzolkin + * object. Does not take wildcards into account. + * @param {Tzolkin} new_tzolkin + * @return {boolean} + */ + equal(new_tzolkin) { + return (this.coeff === new_tzolkin.coeff) && + (this.name === new_tzolkin.name); + } + + /** + * Ensure this Tzolkin object has a matching configuration as another Tzolkin + * object. Takes wildcards into account. + * @param {Tzolkin} new_tzolkin + * @return {boolean} + */ + match(new_tzolkin) { + return ( + (this.coeff === wildcard || new_tzolkin.coeff === wildcard) ? + true : + (this.coeff === new_tzolkin.coeff) + ) && ( + (this.day === wildcard || new_tzolkin.day === wildcard) ? + true : + (this.name === new_tzolkin.name) + ); + } + + /** + * Return a string representation of the 260-day cycle name + * @returns {string} + */ + get name() { + if (this.day === wildcard) { + return this.day; + } + return this.day.name; + } + + /** + * Render the 260-day cycle date as a string + * @returns {string} + */ + toString(is_numeric) { + if (is_numeric) { + return `${this.coeff}:${this.day.day_position}`; } + return `${this.coeff} ${this.name}`; + } } /** @ignore */ @@ -142,99 +142,99 @@ const _day_lookup = {}; /** @ignore */ function _get_day(day_name) { - if (_day_lookup[day_name] === undefined) { - _day_lookup[day_name] = new TzolkinDay(day_name); - } - return _day_lookup[day_name]; + if (_day_lookup[day_name] === undefined) { + _day_lookup[day_name] = new TzolkinDay(day_name); + } + return _day_lookup[day_name]; } /** * Describes only the day component of a 260-day cycle */ class TzolkinDay { + /** + * @param {string|number} name - Name or position of the 260-day cycle day + */ + constructor(name) { /** - * @param {string|number} name - Name or position of the 260-day cycle day + * Mapping of day names to indexes + * @type {Map} */ - constructor(name) { - /** - * Mapping of day names to indexes - * @type {Map} - */ - this.days = [ - undefined, - 'Imix', - 'Ik\'', - 'Ak\'bal', - 'K\'an', - 'Chikchan', - 'Kimi', - 'Manik\'', - 'Lamat', - 'Muluk', - 'Ok', - 'Chuwen', - 'Eb', - 'Ben', - 'Ix', - 'Men', - 'Kib', - 'Kaban', - 'Etz\'nab', - 'Kawak', - 'Ajaw', - ]; - - if (typeof name === 'number') { - name = this.days[name]; - } - - /** - * Name of the day in the 260-day cycle - * @type {string} - */ - this.name = name; - - /** - * Index of the day in the list of Tzolkin' days - * @type {number} - */ - this.day_position = this.days.findIndex( - d => d === this.name); + this.days = [ + undefined, + 'Imix', + 'Ik\'', + 'Ak\'bal', + 'K\'an', + 'Chikchan', + 'Kimi', + 'Manik\'', + 'Lamat', + 'Muluk', + 'Ok', + 'Chuwen', + 'Eb', + 'Ben', + 'Ix', + 'Men', + 'Kib', + 'Kaban', + 'Etz\'nab', + 'Kawak', + 'Ajaw', + ]; + + if (typeof name === 'number') { + name = this.days[name]; } /** - * Ensure the Tzolk'in day name is defined and is within the list of - * acceptable day names. + * Name of the day in the 260-day cycle + * @type {string} */ - validate() { - if (this.name === undefined) { - throw 'Tzolk\'in day name must be provided'; - } - if (!this.days.includes(this.name)) { - throw `Tzolk\'in day (${this.name}) must be in ${this.days}`; - } - } + this.name = name; /** - * Return the next day in the 260-day cycle - * @returns {TzolkinDay} + * Index of the day in the list of Tzolkin' days + * @type {number} */ - next() { - return this.shift(1); + this.day_position = this.days.findIndex( + d => d === this.name); + } + + /** + * Ensure the Tzolk'in day name is defined and is within the list of + * acceptable day names. + */ + validate() { + if (this.name === undefined) { + throw 'Tzolk\'in day name must be provided'; } - - /** - * - * @param {number} incremental - */ - shift(incremental) { - let new_incremental = (this.day_position + incremental) % 20; - new_incremental = (new_incremental === 0) ? 20 : new_incremental; - return new TzolkinDay(new_incremental); + if (!this.days.includes(this.name)) { + throw `Tzolk\'in day (${this.name}) must be in ${this.days}`; } + } + + /** + * Return the next day in the 260-day cycle + * @returns {TzolkinDay} + */ + next() { + return this.shift(1); + } + + /** + * + * @param {number} incremental + */ + shift(incremental) { + let new_incremental = (this.day_position + incremental) % 20; + new_incremental = (new_incremental === 0) ? 20 : new_incremental; + return new TzolkinDay(new_incremental); + } } module.exports = { - 'TzolkinDay': TzolkinDay, - 'Tzolkin': Tzolkin, + 'TzolkinDay': TzolkinDay, + 'Tzolkin': Tzolkin, }; diff --git a/src/factory/base.js b/src/factory/base.js index 3c3147e..5f27c0a 100644 --- a/src/factory/base.js +++ b/src/factory/base.js @@ -2,44 +2,44 @@ * An abstract class to handle the create of an object from a string */ class Factory { + /** + * Define properties to be override by sub-classes + */ + constructor() { /** - * Define properties to be override by sub-classes + * Describes how to break a string into a date element + * @protected + * @type {RegExp} */ - constructor() { - /** - * Describes how to break a string into a date element - * @protected - * @type {RegExp} - */ - this.pattern = null; - } + this.pattern = null; + } - /** - * Split the provided date into its components - * @param {string} raw - * @access protected - * @returns {String[]} - */ - _split(raw) { - let matches = raw.match( - this.pattern, - ); - if (matches === null) { - return []; - } - return matches.slice(1); + /** + * Split the provided date into its components + * @param {string} raw + * @access protected + * @returns {String[]} + */ + _split(raw) { + let matches = raw.match( + this.pattern, + ); + if (matches === null) { + return []; } + return matches.slice(1); + } - /** - * Checks if the string contains a partial date - * @param {string} raw - Raw date string - * @access protected - * @returns {boolean} - */ - // _is_partial (raw) { - // let parts = this._split(raw) - // return parts.includes('*') - // } + /** + * Checks if the string contains a partial date + * @param {string} raw - Raw date string + * @access protected + * @returns {boolean} + */ + // _is_partial (raw) { + // let parts = this._split(raw) + // return parts.includes('*') + // } } module.exports = Factory; diff --git a/src/factory/calendar-round.js b/src/factory/calendar-round.js index 2029eda..ff1cce0 100644 --- a/src/factory/calendar-round.js +++ b/src/factory/calendar-round.js @@ -10,34 +10,34 @@ const CalendarRound = require('../cr/calendar-round'); * let cr = new CalendarRoundFactory().parse("4 Ajaw 8 Kumk'u"); */ class CalendarRoundFactory extends Factory { + /** + * Defines the pattern describing a Calendar Round + */ + constructor() { + super(); /** - * Defines the pattern describing a Calendar Round + * Describes how to break the string into a Calendar Round + * @type {RegExp} */ - constructor() { - super(); - /** - * Describes how to break the string into a Calendar Round - * @type {RegExp} - */ - this.pattern = /([*\d]+)\s?([^\s]+)\s?([*\d]+)\s?([^\s]+)/; - } + this.pattern = /([*\d]+)\s?([^\s]+)\s?([*\d]+)\s?([^\s]+)/; + } - /** - * Given a string, parse it and create a Calendar Round - * @param {string} raw - A string containing a Calendar Round - * @returns {CalendarRound} - */ - parse(raw) { - let parts = this._split(raw); - if (parts.length < 4) { - return null; - } else { - return new CalendarRound( - parts[0], parts[1], - parts[2], parts[3], - ); - } + /** + * Given a string, parse it and create a Calendar Round + * @param {string} raw - A string containing a Calendar Round + * @returns {CalendarRound} + */ + parse(raw) { + let parts = this._split(raw); + if (parts.length < 4) { + return null; + } else { + return new CalendarRound( + parts[0], parts[1], + parts[2], parts[3], + ); } + } } module.exports = CalendarRoundFactory; diff --git a/src/factory/full-date.js b/src/factory/full-date.js index 6e10eee..ad85648 100644 --- a/src/factory/full-date.js +++ b/src/factory/full-date.js @@ -13,20 +13,20 @@ const FullDate = require('../full-date'); * @extends {Factory} */ class FullDateFactory extends Factory { - /** - * - * @param {String} raw - * @return {FullDate} - */ - parse(raw) { - raw = raw.replace('**', '* *'); - let cr = new CalendarRoundFactory().parse(raw); - let lc = new LongCountFactory().parse(raw); - return new FullDate( - cr, - lc, - ); - } + /** + * + * @param {String} raw + * @return {FullDate} + */ + parse(raw) { + raw = raw.replace('**', '* *'); + let cr = new CalendarRoundFactory().parse(raw); + let lc = new LongCountFactory().parse(raw); + return new FullDate( + cr, + lc, + ); + } } module.exports = FullDateFactory; diff --git a/src/factory/index.js b/src/factory/index.js index 9fd9341..fd71252 100644 --- a/src/factory/index.js +++ b/src/factory/index.js @@ -1,5 +1,5 @@ module.exports = { - 'CalendarRoundFactory': require('./calendar-round'), - 'LongCountFactory': require('./long-count'), - 'FullDateFactory': require('./full-date'), + 'CalendarRoundFactory': require('./calendar-round'), + 'LongCountFactory': require('./long-count'), + 'FullDateFactory': require('./full-date'), }; diff --git a/src/factory/long-count.js b/src/factory/long-count.js index 254600d..d6ce079 100644 --- a/src/factory/long-count.js +++ b/src/factory/long-count.js @@ -14,33 +14,33 @@ const wildcard = require('../wildcard'); * let cr = new LongCountFactory().parse("9.4.2.*.1"); */ class LongCountFactory extends Factory { - /** - * Given a string, parse it and create a Long Count - * @param {string} raw - A string containing a Long Count - * @returns {LongCount} - */ - parse(raw) { - let parts = raw.split('.'); - for (let i = 0; i < parts.length; i++) { - if (i === 0) { - if (parts[i].indexOf(' ') >= 0) { - let first_parts = parts[i].split(' '); - parts[i] = first_parts[first_parts.length - 1]; - } - } else if (i === (parts.length - 1)) { - if (parts[i].indexOf(' ') >= 0) { - let first_parts = parts[i].split(' '); - parts[i] = first_parts[0]; - } - } - if (parts[i] === '*') { - parts[i] = wildcard; - } else { - parts[i] = parseInt(parts[i]); - } + /** + * Given a string, parse it and create a Long Count + * @param {string} raw - A string containing a Long Count + * @returns {LongCount} + */ + parse(raw) { + let parts = raw.split('.'); + for (let i = 0; i < parts.length; i++) { + if (i === 0) { + if (parts[i].indexOf(' ') >= 0) { + let first_parts = parts[i].split(' '); + parts[i] = first_parts[first_parts.length - 1]; } - return new LongCount(...parts.reverse()); + } else if (i === (parts.length - 1)) { + if (parts[i].indexOf(' ') >= 0) { + let first_parts = parts[i].split(' '); + parts[i] = first_parts[0]; + } + } + if (parts[i] === '*') { + parts[i] = wildcard; + } else { + parts[i] = parseInt(parts[i]); + } } + return new LongCount(...parts.reverse()); + } } module.exports = LongCountFactory; diff --git a/src/full-date.js b/src/full-date.js index 1c776ba..3c3298e 100644 --- a/src/full-date.js +++ b/src/full-date.js @@ -2,29 +2,29 @@ * An encapsulation of a LongCount and Calendar Round which match each other. */ class FullDate { + /** + * @param {CalendarRound} cr + * @param {LongCount} lc + */ + constructor(cr, lc) { /** - * @param {CalendarRound} cr - * @param {LongCount} lc + * @type {CalendarRound} */ - constructor(cr, lc) { - /** - * @type {CalendarRound} - */ - this.cr = cr; - - /** - * @type {LongCount} - */ - this.lc = lc; - } + this.cr = cr; /** - * Render the FullDate as a string of both the CR and the LC - * @returns {string} + * @type {LongCount} */ - toString() { - return `${this.cr} ${this.lc}`; - } + this.lc = lc; + } + + /** + * Render the FullDate as a string of both the CR and the LC + * @returns {string} + */ + toString() { + return `${this.cr} ${this.lc}`; + } } diff --git a/src/index.js b/src/index.js index 8ea605f..bda5199 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ module.exports = { - 'factory': require('./factory/index'), - 'cr': require('./cr/index'), - 'lc': require('./lc/index'), - 'op': require('./operations/index'), - 'wildcard': require('./wildcard'), + 'factory': require('./factory/index'), + 'cr': require('./cr/index'), + 'lc': require('./lc/index'), + 'op': require('./operations/index'), + 'wildcard': require('./wildcard'), }; diff --git a/src/lc/index.js b/src/lc/index.js index f5a8cf4..26975f2 100644 --- a/src/lc/index.js +++ b/src/lc/index.js @@ -1,4 +1,4 @@ module.exports = { - 'LongCount': require('./long-count'), - 'night': require('./night/lord-of-night'), + 'LongCount': require('./long-count'), + 'night': require('./night/lord-of-night'), }; diff --git a/src/lc/long-count.js b/src/lc/long-count.js index 4f4fb3f..d5b5513 100644 --- a/src/lc/long-count.js +++ b/src/lc/long-count.js @@ -11,305 +11,305 @@ const night = require('./night/lord-of-night'); * Long Count cycle */ class LongCount { + /** + * @param {...number|Wildcard} cycles - Components in the long count (eg, K'in, Winal, Bak'tun, etc) + */ + constructor(...cycles) { /** - * @param {...number|Wildcard} cycles - Components in the long count (eg, K'in, Winal, Bak'tun, etc) + * Date Components + * @type {number|Wildcard[]} */ - constructor(...cycles) { - /** - * Date Components - * @type {number|Wildcard[]} - */ - this.parts = cycles; - - /** - * Pattern to validate the date - * @type {RegExp} - */ - this.date_pattern = /([\d*]+\.?)+/; - - /** - * Convert individual components to a single string - * @type {string} - */ - this.raw = this.toString(); - } - - /** - * Create a copy object of this long count date - * @returns {LongCount} - */ - clone() { - return new LongCount(...this.parts); - } - - /** - * Get specific column in Long Count date - * @param {number} index - * @returns {number} - */ - _get_date_sections(index) { - let part = this.parts[index]; - if (part === undefined) { - return 0; - } - return part; - } + this.parts = cycles; /** - * Set specific column in Long Count date - * @param {number} index - * @param {number} value - * @returns {LongCount} - * @private + * Pattern to validate the date + * @type {RegExp} */ - _set_date_sections(index, value) { - this.parts[index] = value.toString(); - this.raw = this.toString(); - return this; - } + this.date_pattern = /([\d*]+\.?)+/; /** - * Return the number of positions in the long count - * @returns {number} + * Convert individual components to a single string + * @type {string} */ - get length() { - return this.parts.length; + this.raw = this.toString(); + } + + /** + * Create a copy object of this long count date + * @returns {LongCount} + */ + clone() { + return new LongCount(...this.parts); + } + + /** + * Get specific column in Long Count date + * @param {number} index + * @returns {number} + */ + _get_date_sections(index) { + let part = this.parts[index]; + if (part === undefined) { + return 0; } - - /** - * Set the k'in component of the date - * @returns {number} - */ - set k_in(new_k_in) { - this._set_date_sections(0, new_k_in); + return part; + } + + /** + * Set specific column in Long Count date + * @param {number} index + * @param {number} value + * @returns {LongCount} + * @private + */ + _set_date_sections(index, value) { + this.parts[index] = value.toString(); + this.raw = this.toString(); + return this; + } + + /** + * Return the number of positions in the long count + * @returns {number} + */ + get length() { + return this.parts.length; + } + + /** + * Set the k'in component of the date + * @returns {number} + */ + set k_in(new_k_in) { + this._set_date_sections(0, new_k_in); + } + + /** + * Return the k'in component of the date + * @returns {number} + */ + get k_in() { + return this._get_date_sections(0); + } + + /** + * Set the winal component of the date + * @returns {number} + */ + set winal(new_winal) { + this._set_date_sections(1, new_winal); + } + + /** + * Return the winal component of the date + * @returns {number} + */ + get winal() { + return this._get_date_sections(1); + } + + /** + * Set the tun component of the date + * @returns {number} + */ + set tun(new_tun) { + this._set_date_sections(2, new_tun); + } + + /** + * Return the tun component of the date + * @returns {number} + */ + get tun() { + return this._get_date_sections(2); + } + + /** + * Set the k'atun component of the date + * @returns {number} + */ + set k_atun(new_k_atun) { + this._set_date_sections(3, new_k_atun); + } + + /** + * Return the k'atun component of the date + * @returns {number} + */ + get k_atun() { + return this._get_date_sections(3); + } + + /** + * Set the bak'tun component of the date + * @returns {number} + */ + set bak_tun(new_bak_tun) { + this._set_date_sections(4, new_bak_tun); + } + + /** + * Return the bak'tun component of the date + * @returns {number} + */ + get bak_tun() { + return this._get_date_sections(4); + } + + /** + * Set the piktun component of the date + * @returns {number} + */ + set piktun(new_bak_tun) { + this._set_date_sections(5, new_bak_tun); + } + + /** + * Return the piktun component of the date + * @returns {number} + */ + get piktun() { + return this._get_date_sections(5); + } + + /** + * Set the kalabtun component of the date + * @returns {number} + */ + set kalabtun(new_bak_tun) { + this._set_date_sections(6, new_bak_tun); + } + + /** + * Return the kalabtun component of the date + * @returns {number} + */ + get kalabtun() { + return this._get_date_sections(6); + } + + /** + * Set the kinchiltun component of the date + * @returns {number} + */ + set kinchiltun(new_bak_tun) { + this._set_date_sections(7, new_bak_tun); + } + + /** + * Return the kinchiltun component of the date + * @returns {number} + */ + get kinchiltun() { + return this._get_date_sections(7); + } + + /** + * + * @return {any} + */ + get lord_of_night() { + return night.get( + `G${((this.get_position() - 1) % 9) + 1}`, + ); + } + + /** + * Ensure the date has only numbers and wildcards separated by points. + * @returns {boolean} + */ + is_valid() { + return this.date_pattern.test(this.toString()); + } + + /** + * Returns true if any of the positions in the Long Count have been assigned + * a {Wildcard} object. + * @return {boolean} + */ + is_partial() { + for (let part of this.parts) { + if (part === wildcard) { + return true; + } } - - /** - * Return the k'in component of the date - * @returns {number} - */ - get k_in() { - return this._get_date_sections(0); + return false; + } + + /** + * Count the number of days since 0.0.0.0.0 for this LC. + * @return {number} + */ + get_position() { + if (this.is_partial()) { + throw 'Can not get position of partial dates'; } - - /** - * Set the winal component of the date - * @returns {number} - */ - set winal(new_winal) { - this._set_date_sections(1, new_winal); + return this.k_in + + this.winal * 20 + + this.tun * 360 + + this.k_atun * 7200 + + this.bak_tun * 144000 + + this.piktun * 2880000 + + this.kalabtun * 57600000 + + this.kinchiltun * 1152000000; + } + + /** + * + * @return {CalendarRound} + */ + build_calendar_round() { + return origin.shift( + this.get_position(), + ); + } + + /** + * + * @return {FullDate} + */ + build_full_date() { + return new FullDate( + this.build_calendar_round(), + this.clone(), + ); + } + + /** + * Convert the LongCount to a string and pad the sections of the date + * @returns {string} + */ + toString() { + let significant_digits = []; + for (let i = this.parts.length - 1; i >= 0; i--) { + let part = this.parts[i]; + if (part !== 0) { + significant_digits = this.parts.slice(0, i + 1).reverse(); + break; + } } - /** - * Return the winal component of the date - * @returns {number} - */ - get winal() { - return this._get_date_sections(1); + for (let i = 0; i < significant_digits.length; i++) { + if (significant_digits[i] === undefined) { + significant_digits[i] = '0'; + } } - /** - * Set the tun component of the date - * @returns {number} - */ - set tun(new_tun) { - this._set_date_sections(2, new_tun); - } - - /** - * Return the tun component of the date - * @returns {number} - */ - get tun() { - return this._get_date_sections(2); - } - - /** - * Set the k'atun component of the date - * @returns {number} - */ - set k_atun(new_k_atun) { - this._set_date_sections(3, new_k_atun); - } - - /** - * Return the k'atun component of the date - * @returns {number} - */ - get k_atun() { - return this._get_date_sections(3); - } - - /** - * Set the bak'tun component of the date - * @returns {number} - */ - set bak_tun(new_bak_tun) { - this._set_date_sections(4, new_bak_tun); - } - - /** - * Return the bak'tun component of the date - * @returns {number} - */ - get bak_tun() { - return this._get_date_sections(4); - } - - /** - * Set the piktun component of the date - * @returns {number} - */ - set piktun(new_bak_tun) { - this._set_date_sections(5, new_bak_tun); + let date_length = significant_digits.length; + if (date_length < 5) { + for (let i = 0; i < 5 - date_length; i++) { + significant_digits.push(' 0'); + } } - /** - * Return the piktun component of the date - * @returns {number} - */ - get piktun() { - return this._get_date_sections(5); - } - - /** - * Set the kalabtun component of the date - * @returns {number} - */ - set kalabtun(new_bak_tun) { - this._set_date_sections(6, new_bak_tun); - } - - /** - * Return the kalabtun component of the date - * @returns {number} - */ - get kalabtun() { - return this._get_date_sections(6); - } - - /** - * Set the kinchiltun component of the date - * @returns {number} - */ - set kinchiltun(new_bak_tun) { - this._set_date_sections(7, new_bak_tun); - } - - /** - * Return the kinchiltun component of the date - * @returns {number} - */ - get kinchiltun() { - return this._get_date_sections(7); - } - - /** - * - * @return {any} - */ - get lord_of_night() { - return night.get( - `G${((this.get_position() - 1) % 9) + 1}`, - ); - } - - /** - * Ensure the date has only numbers and wildcards separated by points. - * @returns {boolean} - */ - is_valid() { - return this.date_pattern.test(this.toString()); - } - - /** - * Returns true if any of the positions in the Long Count have been assigned - * a {Wildcard} object. - * @return {boolean} - */ - is_partial() { - for (let part of this.parts) { - if (part === wildcard) { - return true; - } - } - return false; - } - - /** - * Count the number of days since 0.0.0.0.0 for this LC. - * @return {number} - */ - get_position() { - if (this.is_partial()) { - throw 'Can not get position of partial dates'; - } - return this.k_in + - this.winal * 20 + - this.tun * 360 + - this.k_atun * 7200 + - this.bak_tun * 144000 + - this.piktun * 2880000 + - this.kalabtun * 57600000 + - this.kinchiltun * 1152000000; - } - - /** - * - * @return {CalendarRound} - */ - build_calendar_round() { - return origin.shift( - this.get_position(), - ); - } - - /** - * - * @return {FullDate} - */ - build_full_date() { - return new FullDate( - this.build_calendar_round(), - this.clone(), - ); - } - - /** - * Convert the LongCount to a string and pad the sections of the date - * @returns {string} - */ - toString() { - let significant_digits = []; - for (let i = this.parts.length - 1; i >= 0; i--) { - let part = this.parts[i]; - if (part !== 0) { - significant_digits = this.parts.slice(0, i + 1).reverse(); - break; - } - } - - for (let i = 0; i < significant_digits.length; i++) { - if (significant_digits[i] === undefined) { - significant_digits[i] = '0'; - } - } - - let date_length = significant_digits.length; - if (date_length < 5) { - for (let i = 0; i < 5 - date_length; i++) { - significant_digits.push(' 0'); - } - } - - for (let i = 0; i < significant_digits.length; i++) { - let part = significant_digits[i].toString(); - if (part.length < 2) { - significant_digits[i] = ' ' + part; - } - } - return significant_digits.join('.'); + for (let i = 0; i < significant_digits.length; i++) { + let part = significant_digits[i].toString(); + if (part.length < 2) { + significant_digits[i] = ' ' + part; + } } + return significant_digits.join('.'); + } } module.exports = LongCount; diff --git a/src/lc/night/lord-of-night.js b/src/lc/night/lord-of-night.js index 02c0a6c..dae6a86 100644 --- a/src/lc/night/lord-of-night.js +++ b/src/lc/night/lord-of-night.js @@ -8,24 +8,24 @@ * console.log(lord_of_night_g8_1 === lord_of_night_g8_2) */ class LordOfNight { + /** + * @param {number} id + */ + constructor(id) { /** - * @param {number} id + * Number of the Lord of the Night + * @type {number} */ - constructor(id) { - /** - * Number of the Lord of the Night - * @type {number} - */ - this.id = id; - } + this.id = id; + } - /** - * Represent the Lord of the night as a string G1..G9. - * @return {string} - */ - toString() { - return `G${this.id}`; - } + /** + * Represent the Lord of the night as a string G1..G9. + * @return {string} + */ + toString() { + return `G${this.id}`; + } } /** @@ -34,18 +34,18 @@ class LordOfNight { * @return {LordOfNight} */ function get(id) { - return lords_of_the_night[`${id}`]; + return lords_of_the_night[`${id}`]; } /** @ignore */ const lords_of_the_night = [ - 1, 2, 3, 4, 5, 6, 7, 8, 9, + 1, 2, 3, 4, 5, 6, 7, 8, 9, ].reduce(function (obj, n) { - let lord = new LordOfNight(n); - obj[`${lord}`] = lord; - return obj; + let lord = new LordOfNight(n); + obj[`${lord}`] = lord; + return obj; }, { - 'get': get, + 'get': get, }); module.exports = lords_of_the_night; diff --git a/src/operations/calendar-round-wildcard.js b/src/operations/calendar-round-wildcard.js index c037d40..3065cc3 100644 --- a/src/operations/calendar-round-wildcard.js +++ b/src/operations/calendar-round-wildcard.js @@ -9,56 +9,56 @@ const origin = require('../cr/index').origin; * @ignore */ class _CalendarRoundIterator { + /** + * + * @param {CalendarRound} date + */ + constructor(date) { /** - * - * @param {CalendarRound} date + * @type {CalendarRound} */ - constructor(date) { - /** - * @type {CalendarRound} - */ - this.current = undefined; - - /** - * @type boolean - */ - this.is_first = undefined; - - if (date === undefined) { - date = origin; - } - /** - * @type {CalendarRound} - */ - this.date = date; - this.reset(); - } + this.current = undefined; /** - * Reset this iterator so that it can be reused. + * @type boolean */ - reset() { - this.current = this.date; - this.is_first = true; - } + this.is_first = undefined; + if (date === undefined) { + date = origin; + } /** - * Move to the next position in the iterator or end the iteration. - * @return {{value: null, done: boolean}|{value: CalendarRound, done: boolean}} + * @type {CalendarRound} */ - next() { - if (this.is_first) { - this.is_first = false; - return {value: this.current, done: false}; - } - let next = this.current.next(); - if (next.equal(this.date)) { - return {value: null, done: true}; - } else { - this.current = next; - return {value: next, done: false}; - } + this.date = date; + this.reset(); + } + + /** + * Reset this iterator so that it can be reused. + */ + reset() { + this.current = this.date; + this.is_first = true; + } + + /** + * Move to the next position in the iterator or end the iteration. + * @return {{value: null, done: boolean}|{value: CalendarRound, done: boolean}} + */ + next() { + if (this.is_first) { + this.is_first = false; + return {value: this.current, done: false}; + } + let next = this.current.next(); + if (next.equal(this.date)) { + return {value: null, done: true}; + } else { + this.current = next; + return {value: next, done: false}; } + } } @@ -74,33 +74,33 @@ const iter = new _CalendarRoundIterator(); * fully qualified Calendar Rounds. */ class CalendarRoundWildcard { + /** + * @param {CalendarRound} cr + */ + constructor(cr) { /** - * @param {CalendarRound} cr + * @type {CalendarRound} */ - constructor(cr) { - /** - * @type {CalendarRound} - */ - this.cr = cr; - } + this.cr = cr; + } - /** - * Run calculation to find all fully qualified Calendar Rounds - * @return {CalendarRound[]} - */ - run() { - let potentials = []; - // Iterate through dates and compare - let cr = iter.next(); - while (!cr.done) { - if (this.cr.match(cr.value)) { - potentials.push(cr.value); - } - cr = iter.next(); - } - iter.reset(); - return potentials; + /** + * Run calculation to find all fully qualified Calendar Rounds + * @return {CalendarRound[]} + */ + run() { + let potentials = []; + // Iterate through dates and compare + let cr = iter.next(); + while (!cr.done) { + if (this.cr.match(cr.value)) { + potentials.push(cr.value); + } + cr = iter.next(); } + iter.reset(); + return potentials; + } } diff --git a/src/operations/fulldate-wildcard.js b/src/operations/fulldate-wildcard.js index 0d31d94..151182a 100644 --- a/src/operations/fulldate-wildcard.js +++ b/src/operations/fulldate-wildcard.js @@ -10,55 +10,55 @@ const CalendarRoundWildcard = require('../operations/calendar-round-wildcard'); * matching fully qualified Long Counts with CalendarRounds. */ class FullDateWildcard { + /** + * @param {FullDate} partial_date + */ + constructor(partial_date) { /** - * @param {FullDate} partial_date + * @type {FullDate} */ - constructor(partial_date) { - /** - * @type {FullDate} - */ - this.partial = partial_date; - } + this.partial = partial_date; + } - /** - * Run calculation to find all fully qualified Long Counts with Calendar Rounds - * @return {FullDate[]} - */ - run() { - let potential_dates = []; - let potential_lc_fulldates = []; - let potential_crs; + /** + * Run calculation to find all fully qualified Long Counts with Calendar Rounds + * @return {FullDate[]} + */ + run() { + let potential_dates = []; + let potential_lc_fulldates = []; + let potential_crs; - let has_cr_partial = this.partial.cr.is_partial(); - let has_lc_partial = this.partial.lc.is_partial(); - if (has_lc_partial) { - let potential_lcs = new LongCountWildcard(this.partial.lc).run(); - for (let potential_lc of potential_lcs) { - potential_lc_fulldates.push(potential_lc.build_full_date()); - } - } else { - // If we have a full formed LC date, and a partial CR, then generate the - // CR for the LC, and compare them. The partial CR will either match the - // CR for the LC or it won't. - let static_cr = this.partial.lc.build_calendar_round(); - return (static_cr.match(this.partial.cr)) ? - [new FullDate(static_cr, this.partial.lc)] : - []; - } - if (has_cr_partial) { - potential_crs = new CalendarRoundWildcard(this.partial.cr).run(); - } else { - potential_crs = [this.partial.cr]; - } - for (let full_date of potential_lc_fulldates) { - for (let cr of potential_crs) { - if (cr.equal(full_date.cr)) { - potential_dates.push(full_date); - } - } + let has_cr_partial = this.partial.cr.is_partial(); + let has_lc_partial = this.partial.lc.is_partial(); + if (has_lc_partial) { + let potential_lcs = new LongCountWildcard(this.partial.lc).run(); + for (let potential_lc of potential_lcs) { + potential_lc_fulldates.push(potential_lc.build_full_date()); + } + } else { + // If we have a full formed LC date, and a partial CR, then generate the + // CR for the LC, and compare them. The partial CR will either match the + // CR for the LC or it won't. + let static_cr = this.partial.lc.build_calendar_round(); + return (static_cr.match(this.partial.cr)) ? + [new FullDate(static_cr, this.partial.lc)] : + []; + } + if (has_cr_partial) { + potential_crs = new CalendarRoundWildcard(this.partial.cr).run(); + } else { + potential_crs = [this.partial.cr]; + } + for (let full_date of potential_lc_fulldates) { + for (let cr of potential_crs) { + if (cr.equal(full_date.cr)) { + potential_dates.push(full_date); } - return potential_dates; + } } + return potential_dates; + } } module.exports = FullDateWildcard; diff --git a/src/operations/index.js b/src/operations/index.js index 4f8492a..1fe6ece 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -1,5 +1,5 @@ module.exports = { - 'LongCountWildcard': require('./longcount-wildcard'), - 'CalendarRoundWildcard': require('./calendar-round-wildcard'), - 'FullDateWildcard': require('./fulldate-wildcard'), + 'LongCountWildcard': require('./longcount-wildcard'), + 'CalendarRoundWildcard': require('./calendar-round-wildcard'), + 'FullDateWildcard': require('./fulldate-wildcard'), }; diff --git a/src/operations/longcount-wildcard.js b/src/operations/longcount-wildcard.js index 2638439..0b612e0 100644 --- a/src/operations/longcount-wildcard.js +++ b/src/operations/longcount-wildcard.js @@ -6,41 +6,41 @@ const wildcard = require('../wildcard'); * qualified Long Counts. */ class LongCountWildcard { + /** + * @param {LongCount} lc + */ + constructor(lc) { /** - * @param {LongCount} lc + * @type {LongCount} */ - constructor(lc) { - /** - * @type {LongCount} - */ - this.lc = lc; - } + this.lc = lc; + } - /** - * Run calculation to find all fully qualified Long Counts - * @return {LongCount[]} - */ - run() { - let potentials = [this.lc]; - let wildcard_positions = []; - for (let i = 0; i < this.lc.length; i++) { - if (this.lc._get_date_sections(i) === wildcard) { - wildcard_positions.push(i); - } - } - for (let position of wildcard_positions) { - let new_potentials = []; - let iterations = (position === 1) ? 15 : 20; - for (let possible of potentials) { - for (let k = 0; k < iterations; k++) { - let new_lc = possible.clone()._set_date_sections(position, k); - new_potentials.push(new_lc); - } - } - potentials = new_potentials; + /** + * Run calculation to find all fully qualified Long Counts + * @return {LongCount[]} + */ + run() { + let potentials = [this.lc]; + let wildcard_positions = []; + for (let i = 0; i < this.lc.length; i++) { + if (this.lc._get_date_sections(i) === wildcard) { + wildcard_positions.push(i); + } + } + for (let position of wildcard_positions) { + let new_potentials = []; + let iterations = (position === 1) ? 15 : 20; + for (let possible of potentials) { + for (let k = 0; k < iterations; k++) { + let new_lc = possible.clone()._set_date_sections(position, k); + new_potentials.push(new_lc); } - return potentials; + } + potentials = new_potentials; } + return potentials; + } } module.exports = LongCountWildcard; diff --git a/src/wildcard.js b/src/wildcard.js index 2a3ff46..1025ce5 100644 --- a/src/wildcard.js +++ b/src/wildcard.js @@ -10,13 +10,13 @@ * > true */ class Wildcard { - /** - * Represent the Wildcard as a string. ie, '*'. - * @returns {string} - */ - toString() { - return '*'; - } + /** + * Represent the Wildcard as a string. ie, '*'. + * @returns {string} + */ + toString() { + return '*'; + } } module.exports = new Wildcard();