From 4ff18aaba5edb55441991c28974e7c39d9232af2 Mon Sep 17 00:00:00 2001 From: "Drew J. Sonne" Date: Mon, 2 Mar 2020 21:34:42 +0000 Subject: [PATCH] Implemented calendar round operations --- src/cr/calendar-round.js | 129 +++++++++++---------- src/lc/distance-number.js | 216 ++++++++++++++++++------------------ test/calendar-round.test.js | 115 ++++++++++--------- 3 files changed, 243 insertions(+), 217 deletions(-) diff --git a/src/cr/calendar-round.js b/src/cr/calendar-round.js index aece223..1324c7f 100644 --- a/src/cr/calendar-round.js +++ b/src/cr/calendar-round.js @@ -1,15 +1,14 @@ /** @ignore */ -const tzolkin = require('../cr/tzolkin'); +const tzolkin = require('../cr/tzolkin') /** @ignore */ -const haab = require('../cr/haab'); +const haab = require('../cr/haab') /** @ignore */ -const wildcard = require('../wildcard'); +const wildcard = require('../wildcard') /** @ignore */ -const DistanceNumber = require('../lc/distance-number'); - +const DistanceNumber = require('../lc/distance-number') /** @ignore */ -const singleton = {}; +const singleton = {} /** * Return a comparable instance of a Calendar Round. @@ -19,13 +18,14 @@ const singleton = {}; * @param {HaabMonth|string} haabMonth * @return {CalendarRound} */ -function getCalendarRound(tzolkinCoeff, tzolkinDay, haabCoeff, haabMonth) { - const crId = `${tzolkinCoeff} ${tzolkinDay} ${haabCoeff} ${haabMonth}`; +function getCalendarRound (tzolkinCoeff, tzolkinDay, haabCoeff, haabMonth) { + const crId = `${tzolkinCoeff} ${tzolkinDay} ${haabCoeff} ${haabMonth}` if (singleton[crId] === undefined) { // eslint-disable-next-line no-use-before-define - singleton[crId] = new CalendarRound(tzolkinCoeff, tzolkinDay, haabCoeff, haabMonth); + singleton[crId] = new CalendarRound(tzolkinCoeff, tzolkinDay, haabCoeff, + haabMonth) } - return singleton[crId]; + return singleton[crId] } /** @@ -42,19 +42,19 @@ class CalendarRound { * @param {number} haabCoeff Day in the Haab month * @param {string|HaabMonth} haabMonth Name of the Haab month */ - constructor(tzolkinCoeff, tzolkinDay, haabCoeff, haabMonth) { + constructor (tzolkinCoeff, tzolkinDay, haabCoeff, haabMonth) { /** * 260-day cycle component of the Calendar Round * @type {Tzolkin} */ - this.tzolkin = tzolkin.getTzolkin(tzolkinCoeff, tzolkinDay); + this.tzolkin = tzolkin.getTzolkin(tzolkinCoeff, tzolkinDay) /** * Haab cycle component of the Calendar Round * @type {Haab} */ - this.haab = haab.getHaab(haabCoeff, haabMonth); + this.haab = haab.getHaab(haabCoeff, haabMonth) - this.validate(); + this.validate() } /** @@ -62,38 +62,40 @@ class CalendarRound { * configuration * @throws {Error} If the Calendar Round is invalid. */ - validate() { - let validHaabCoeffs = []; + validate () { + let validHaabCoeffs = [] if ([ - 'Kaban', 'Ik\'', 'Manik\'', 'Eb' + 'Kaban', 'Ik\'', 'Manik\'', 'Eb', ].includes(this.tzolkin.name)) { - validHaabCoeffs = [0, 5, 10, 15]; + validHaabCoeffs = [0, 5, 10, 15] } else if ([ - 'Etz\'nab', 'Ak\'bal', 'Lamat', 'Ben' + 'Etz\'nab', 'Ak\'bal', 'Lamat', 'Ben', ].includes(this.tzolkin.name)) { - validHaabCoeffs = [1, 6, 11, 16]; + validHaabCoeffs = [1, 6, 11, 16] } else if ([ - 'Kawak', 'K\'an', 'Muluk', 'Ix' + 'Kawak', 'K\'an', 'Muluk', 'Ix', ].includes(this.tzolkin.name)) { - validHaabCoeffs = [2, 7, 12, 17]; + validHaabCoeffs = [2, 7, 12, 17] } else if ([ - 'Ajaw', 'Chikchan', 'Ok', 'Men' + 'Ajaw', 'Chikchan', 'Ok', 'Men', ].includes(this.tzolkin.name)) { - validHaabCoeffs = [3, 8, 13, 18]; + validHaabCoeffs = [3, 8, 13, 18] } else if ([ - 'Imix', 'Kimi', 'Chuwen', 'Kib' + 'Imix', 'Kimi', 'Chuwen', 'Kib', ].includes(this.tzolkin.name)) { - validHaabCoeffs = [4, 9, 14, 19]; + validHaabCoeffs = [4, 9, 14, 19] } else if ([wildcard].includes(this.tzolkin.name)) { - validHaabCoeffs = [...Array(19).keys()]; + validHaabCoeffs = [...Array(19).keys()] } else { - throw new Error(`Could not allocate Tzolk'in (${this.tzolkin.name}) to permissible month coeffs.`); + throw new Error( + `Could not allocate Tzolk'in (${this.tzolkin.name}) to permissible month coeffs.`) } - validHaabCoeffs.push(wildcard); + validHaabCoeffs.push(wildcard) if (!validHaabCoeffs.includes(this.haab.coeff)) { - throw new Error(`${this} should have Haab coeff in ${validHaabCoeffs} for day ${this.tzolkin.name}`); + throw new Error( + `${this} should have Haab coeff in ${validHaabCoeffs} for day ${this.tzolkin.name}`) } } @@ -101,8 +103,8 @@ class CalendarRound { * Increment both the Haab and 260-day cycle to the next day in the Calendar Round * @returns {CalendarRound} */ - next() { - return this.shift(1); + next () { + return this.shift(1) } /** @@ -111,8 +113,8 @@ class CalendarRound { * @param {CalendarRound} newCr * @return {Boolean} */ - equal(newCr) { - return this === newCr; + equal (newCr) { + return this === newCr } /** @@ -120,21 +122,29 @@ class CalendarRound { * @param {CalendarRound} targetCr * @return {LongCount} */ - minus(targetCr) { + minus (targetCr) { /** @ignore */ - const foundOrigin = false; - const foundTarget = false; - let current = this; - let count = 0; + let foundOrigin = false + const foundTarget = false + let current = this + let count = 0 + let cycleCount = undefined while (!foundTarget) { // eslint-disable-next-line no-use-before-define if (current.equal(origin)) { - debugger; + foundOrigin = true + cycleCount = count + count = 0 + current = current.next() } else if (current.equal(targetCr)) { - return new DistanceNumber(count).normalise(); + return new DistanceNumber( + foundOrigin + ? -(18979 - cycleCount - count) + : count, + ).normalise() } else { - current = current.next(); - count += 1; + current = current.next() + count += 1 } } } @@ -145,10 +155,10 @@ class CalendarRound { * @param {CalendarRound} newCr * @return {boolean} */ - match(newCr) { - const haabMatches = this.haab.match(newCr.haab); - const tzolkinMatches = this.tzolkin.match(newCr.tzolkin); - return haabMatches && tzolkinMatches; + match (newCr) { + const haabMatches = this.haab.match(newCr.haab) + const tzolkinMatches = this.tzolkin.match(newCr.tzolkin) + return haabMatches && tzolkinMatches } /** @@ -157,43 +167,42 @@ class CalendarRound { * @param {number} increment * @return {CalendarRound} */ - shift(increment) { - const newHaab = this.haab.shift(increment); - const newTzolkin = this.tzolkin.shift(increment); + shift (increment) { + const newHaab = this.haab.shift(increment) + const newTzolkin = this.tzolkin.shift(increment) // eslint-disable-next-line no-use-before-define return getCalendarRound( newTzolkin.coeff, newTzolkin.day, newHaab.coeff, - newHaab.month - ); + newHaab.month, + ) } /** * Return true, if this function has any wildcard portions. * @return {boolean} */ - isPartial() { + isPartial () { return (this.tzolkin.day === wildcard) || (this.tzolkin.coeff === wildcard) || (this.haab.month === wildcard) - || (this.haab.coeff === wildcard); + || (this.haab.coeff === wildcard) } /** * Render the CalendarRound cycle fullDate as a string * @returns {string} */ - toString() { - return `${this.tzolkin} ${this.haab}`; + toString () { + return `${this.tzolkin} ${this.haab}` } } /** @ignore */ const origin = getCalendarRound( 4, 'Ajaw', - 8, 'Kumk\'u' -); - + 8, 'Kumk\'u', +) -module.exports = {getCalendarRound, origin}; +module.exports = {getCalendarRound, origin} diff --git a/src/lc/distance-number.js b/src/lc/distance-number.js index 65f372f..ead8cf8 100644 --- a/src/lc/distance-number.js +++ b/src/lc/distance-number.js @@ -1,11 +1,11 @@ /** @ignore */ -const moonbeams = require('moonbeams'); +const moonbeams = require('moonbeams') /** @ignore */ -const wildcard = require('../wildcard'); +const wildcard = require('../wildcard') /** @ignore */ -const LongcountAddition = require('../operations/longcount-addition'); +const LongcountAddition = require('../operations/longcount-addition') /** @ignore */ -const LongcountSubtraction = require('../operations/longcount-subtraction'); +const LongcountSubtraction = require('../operations/longcount-subtraction') /** * Long Count cycle @@ -15,26 +15,27 @@ class DistanceNumber { * @param {...number|Wildcard} cycles - Components in the long count * (eg, K'in, Winal, Bak'tun, etc) */ - constructor(...cycles) { + constructor (...cycles) { /** * Date Components * @type {number[]|Wildcard[]} */ - this.parts = cycles; + this.parts = cycles /** * Pattern to validate the fullDate * @type {RegExp} */ - this.date_pattern = /([\d*]+\.?)+/; + this.date_pattern = /([\d*]+\.?)+/ /** * @private * @type {number} */ - this.sign = (this.parts[this.parts.length - 1] < 0) ? -1 : 1; + this.sign = (this.parts[this.parts.length - 1] < 0) ? -1 : 1 if (this.isNegative) { - this.parts[this.parts.length - 1] = -1 * this.parts[this.parts.length - 1]; + this.parts[this.parts.length - 1] = -1 * + this.parts[this.parts.length - 1] } } @@ -42,32 +43,32 @@ class DistanceNumber { * Return true if the Long Count is positive. * @return {boolean} */ - get isPositive() { - return this.sign === 1; + get isPositive () { + return this.sign === 1 } /** * Return true if the Long Count is operating as a negative Distance Number. * @return {boolean} */ - get isNegative() { - return this.sign === -1; + get isNegative () { + return this.sign === -1 } /** * Set this Long Count as being a Long Count or a positive Distance Number * @param {boolean} newPositive */ - set isPositive(newPositive) { - this.sign = newPositive === true ? 1 : -1; + set isPositive (newPositive) { + this.sign = newPositive === true ? 1 : -1 } /** * Set this Long Count as being a negative Distance Number * @param newNegative */ - set isNegative(newNegative) { - this.isPositive = !newNegative; + set isNegative (newNegative) { + this.isPositive = !newNegative } /** @@ -75,16 +76,16 @@ class DistanceNumber { * @param {DistanceNumber} other * @return {boolean} */ - equal(other) { - return `${this}` === `${other}`; + equal (other) { + return `${this}` === `${other}` } /** * Create a copy object of this long count fullDate * @returns {DistanceNumber} */ - clone() { - return new DistanceNumber(...this.parts); + clone () { + return new DistanceNumber(...this.parts) } /** @@ -92,12 +93,12 @@ class DistanceNumber { * @param {number} index * @returns {number} */ - getDateSections(index) { - const part = this.parts[index]; + getDateSections (index) { + const part = this.parts[index] if (part === undefined) { - return 0; + return 0 } - return part; + return part } /** @@ -106,9 +107,10 @@ class DistanceNumber { * @param {number|wildcard} newValue * @returns {DistanceNumber} */ - setDateSections(index, newValue) { - this.parts[index] = (newValue !== wildcard) ? newValue : parseInt(newValue, 10); - return this; + setDateSections (index, newValue) { + this.parts[index] = (newValue !== wildcard) ? newValue : parseInt(newValue, + 10) + return this } /** @@ -116,8 +118,8 @@ class DistanceNumber { * @param fn * @return {object[]} */ - map(fn) { - return this.parts.map(fn); + map (fn) { + return this.parts.map(fn) } /** @@ -125,8 +127,8 @@ class DistanceNumber { * @param {DistanceNumber} newLongCount * @return {boolean} */ - lt(newLongCount) { - return this.getPosition() < newLongCount.getPosition(); + lt (newLongCount) { + return this.getPosition() < newLongCount.getPosition() } /** @@ -134,136 +136,136 @@ class DistanceNumber { * @param {DistanceNumber} newLongCount * @return {boolean} */ - gt(newLongCount) { - return this.getPosition() > newLongCount.getPosition(); + gt (newLongCount) { + return this.getPosition() > newLongCount.getPosition() } /** * Set the k'in component of the fullDate */ - set kIn(newKIn) { - this.setDateSections(0, newKIn); + set kIn (newKIn) { + this.setDateSections(0, newKIn) } /** * Return the k'in component of the fullDate * @returns {number} */ - get kIn() { - return this.getDateSections(0); + get kIn () { + return this.getDateSections(0) } /** * Set the winal component of the fullDate */ - set winal(newWinal) { - this.setDateSections(1, newWinal); + set winal (newWinal) { + this.setDateSections(1, newWinal) } /** * Return the winal component of the fullDate * @returns {number} */ - get winal() { - return this.getDateSections(1); + get winal () { + return this.getDateSections(1) } /** * Set the tun component of the fullDate */ - set tun(newTun) { - this.setDateSections(2, newTun); + set tun (newTun) { + this.setDateSections(2, newTun) } /** * Return the tun component of the fullDate * @returns {number} */ - get tun() { - return this.getDateSections(2); + get tun () { + return this.getDateSections(2) } /** * Set the k'atun component of the fullDate */ - set kAtun(newKAtun) { - this.setDateSections(3, newKAtun); + set kAtun (newKAtun) { + this.setDateSections(3, newKAtun) } /** * Return the k'atun component of the fullDate * @returns {number} */ - get kAtun() { - return this.getDateSections(3); + get kAtun () { + return this.getDateSections(3) } /** * Set the bak'tun component of the fullDate */ - set bakTun(newBakTun) { - this.setDateSections(4, newBakTun); + set bakTun (newBakTun) { + this.setDateSections(4, newBakTun) } /** * Return the bak'tun component of the fullDate * @returns {number} */ - get bakTun() { - return this.getDateSections(4); + get bakTun () { + return this.getDateSections(4) } /** * Set the piktun component of the fullDate */ - set piktun(newBakTun) { - this.setDateSections(5, newBakTun); + set piktun (newBakTun) { + this.setDateSections(5, newBakTun) } /** * Return the piktun component of the fullDate * @returns {number} */ - get piktun() { - return this.getDateSections(5); + get piktun () { + return this.getDateSections(5) } /** * Set the kalabtun component of the fullDate */ - set kalabtun(newBakTun) { - this.setDateSections(6, newBakTun); + set kalabtun (newBakTun) { + this.setDateSections(6, newBakTun) } /** * Return the kalabtun component of the fullDate * @returns {number} */ - get kalabtun() { - return this.getDateSections(6); + get kalabtun () { + return this.getDateSections(6) } /** * Set the kinchiltun component of the fullDate */ - set kinchiltun(newBakTun) { - this.setDateSections(7, newBakTun); + set kinchiltun (newBakTun) { + this.setDateSections(7, newBakTun) } /** * Return the kinchiltun component of the fullDate * @returns {number} */ - get kinchiltun() { - return this.getDateSections(7); + get kinchiltun () { + return this.getDateSections(7) } /** * Ensure the fullDate has only numbers and wildcards separated by points. * @returns {boolean} */ - isValid() { - return this.date_pattern.test(this.toString()); + isValid () { + return this.date_pattern.test(this.toString()) } /** @@ -271,17 +273,17 @@ class DistanceNumber { * a {Wildcard} object. * @return {boolean} */ - isPartial() { - return this.parts.some((part) => part === wildcard); + isPartial () { + return this.parts.some((part) => part === wildcard) } /** * Count the number of days since 0.0.0.0.0 for this LC. * @return {number} */ - getPosition() { + getPosition () { if (this.isPartial()) { - throw new Error('Can not get position of fullDate dates'); + throw new Error('Can not get position of fullDate dates') } return (this.kIn + this.winal * 20 @@ -290,7 +292,7 @@ class DistanceNumber { + this.bakTun * 144000 + this.piktun * 2880000 + this.kalabtun * 57600000 - + this.kinchiltun * 1152000000) * this.sign; + + this.kinchiltun * 1152000000) * this.sign } /** @@ -298,11 +300,11 @@ class DistanceNumber { * @param {DistanceNumber} newLc * @return {LongcountAddition} */ - plus(newLc) { + plus (newLc) { /* We pass the LongCount class in, as to require this in the operation * will create a circular dependency. */ - return new LongcountAddition(DistanceNumber, this, newLc); + return new LongcountAddition(DistanceNumber, this, newLc) } /** @@ -310,77 +312,81 @@ class DistanceNumber { * @param {DistanceNumber} newLc * @return {LongcountAddition} */ - minus(newLc) { + minus (newLc) { /* We pass the LongCount class in, as to require this in the operation * will create a circular dependency. */ - return new LongcountSubtraction(DistanceNumber, this, newLc); + return new LongcountSubtraction(DistanceNumber, this, newLc) } /** * Make sure the elements of the Long Count do not exceed * @return {DistanceNumber} */ - normalise() { - const totalKIn = this.getPosition(); - const norm = new DistanceNumber(); - norm.kIn = totalKIn % 20; + normalise () { + const totalKIn = this.getPosition() + const norm = new DistanceNumber() + norm.kIn = totalKIn % 20 // eslint-disable-next-line no-mixed-operators - norm.winal = (totalKIn - norm.getPosition()) / 20 % 18; + norm.winal = (totalKIn - norm.getPosition()) / 20 % 18 // eslint-disable-next-line no-mixed-operators - norm.tun = (totalKIn - norm.getPosition()) / 360 % 20; + norm.tun = (totalKIn - norm.getPosition()) / 360 % 20 // eslint-disable-next-line no-mixed-operators - norm.kAtun = (totalKIn - norm.getPosition()) / 7200 % 20; + norm.kAtun = (totalKIn - norm.getPosition()) / 7200 % 20 // eslint-disable-next-line no-mixed-operators - norm.bakTun = (totalKIn - norm.getPosition()) / 144000 % 20; + norm.bakTun = (totalKIn - norm.getPosition()) / 144000 % 20 // eslint-disable-next-line no-mixed-operators - norm.piktun = (totalKIn - norm.getPosition()) / 2880000 % 20; + norm.piktun = (totalKIn - norm.getPosition()) / 2880000 % 20 // eslint-disable-next-line no-mixed-operators - norm.kalabtun = (totalKIn - norm.getPosition()) / 57600000 % 20; + norm.kalabtun = (totalKIn - norm.getPosition()) / 57600000 % 20 // eslint-disable-next-line no-mixed-operators - norm.kinchiltun = (totalKIn - norm.getPosition()) / 1152000000 % 20; - this.parts = norm.parts; - return this; + norm.kinchiltun = (totalKIn - norm.getPosition()) / 1152000000 % 20 + const foundNegative = norm.parts.reduce( + (found, part) => found || (part < 0), + false, + ) + this.sign = foundNegative ? -1 : 1 + this.parts = norm.parts.map((part) => Math.abs(part)) + return this } - /** * Convert the LongCount to a string and pad the sections of the fullDate * @returns {string} */ - toString() { - let significantDigits = []; + toString () { + let significantDigits = [] for (let i = this.parts.length - 1; i >= 0; i -= 1) { - const part = this.parts[i]; + const part = this.parts[i] if (part !== 0) { - significantDigits = this.parts.slice(0, i + 1).reverse(); - break; + significantDigits = this.parts.slice(0, i + 1).reverse() + break } } for (let i = 0; i < significantDigits.length; i += 1) { if (significantDigits[i] === undefined) { - significantDigits[i] = '0'; + significantDigits[i] = '0' } } - const dateLength = significantDigits.length; + const dateLength = significantDigits.length if (dateLength < 5) { - significantDigits = significantDigits.reverse(); + significantDigits = significantDigits.reverse() for (let i = 0; i < 5 - dateLength; i += 1) { - significantDigits.push(' 0'); + significantDigits.push(' 0') } - significantDigits = significantDigits.reverse(); + significantDigits = significantDigits.reverse() } for (let i = 0; i < significantDigits.length; i += 1) { - const part = significantDigits[i].toString(); + const part = significantDigits[i].toString() if (part.length < 2) { - significantDigits[i] = ` ${part}`; + significantDigits[i] = ` ${part}` } } - return significantDigits.join('.'); + return `${this.sign === -1 ? '-' : ''}${significantDigits.join('.')}` } } -module.exports = DistanceNumber; +module.exports = DistanceNumber diff --git a/test/calendar-round.test.js b/test/calendar-round.test.js index c5d9a0a..ad0b856 100644 --- a/test/calendar-round.test.js +++ b/test/calendar-round.test.js @@ -1,6 +1,6 @@ -const mayadates = require('../src/index'); +const mayadates = require('../src/index') -const {wildcard} = mayadates; +const {wildcard} = mayadates /** * @test {CalendarRoundFactory} * @test {CalendarRound#next} @@ -10,19 +10,19 @@ describe('increment calendar-rounds', () => { ['2Ak\'bal 6 Muwan', [3, 'K\'an', 7, 'Muwan']], ['4 Ajaw 8 Kumk\'u', [5, 'Imix', 9, 'Kumk\'u']], ['13 Kimi 4 Wayeb', [1, 'Manik\'', 0, 'Pop']], - ]; + ] it.each(tzolkinDays)( '%s -> %s', (today, next) => { - const cr = new mayadates.factory.CalendarRoundFactory().parse(today); - const tomorrow = cr.next(); - expect(tomorrow.tzolkin.coeff).toBe(next[0]); - expect(tomorrow.tzolkin.name).toBe(next[1]); - expect(tomorrow.haab.coeff).toBe(next[2]); - expect(tomorrow.haab.name).toBe(next[3]); + const cr = new mayadates.factory.CalendarRoundFactory().parse(today) + const tomorrow = cr.next() + expect(tomorrow.tzolkin.coeff).toBe(next[0]) + expect(tomorrow.tzolkin.name).toBe(next[1]) + expect(tomorrow.haab.coeff).toBe(next[2]) + expect(tomorrow.haab.name).toBe(next[3]) }, - ); -}); + ) +}) describe('shift calendar-rounds', () => { const tzolkinDays = [ @@ -30,24 +30,24 @@ describe('shift calendar-rounds', () => { ['4 Ajaw 8 Kumk\'u', 1, [5, 'Imix', 9, 'Kumk\'u']], ['4 Ajaw 8 Kumk\'u', 10, [1, 'Ok', 18, 'Kumk\'u']], ['4 Ajaw 8 Kumk\'u', 365, [5, 'Chikchan', 8, 'Kumk\'u']], - ]; + ] it.each(tzolkinDays)( '%s + %s = %s', (today, increment, expected) => { - const cr = new mayadates.factory.CalendarRoundFactory().parse(today); - const tomorrow = cr.shift(increment); - expect(tomorrow.tzolkin.coeff).toBe(expected[0]); - expect(tomorrow.tzolkin.name).toBe(expected[1]); - expect(tomorrow.haab.coeff).toBe(expected[2]); - expect(tomorrow.haab.name).toBe(expected[3]); + const cr = new mayadates.factory.CalendarRoundFactory().parse(today) + const tomorrow = cr.shift(increment) + expect(tomorrow.tzolkin.coeff).toBe(expected[0]) + expect(tomorrow.tzolkin.name).toBe(expected[1]) + expect(tomorrow.haab.coeff).toBe(expected[2]) + expect(tomorrow.haab.name).toBe(expected[3]) }, - ); -}); + ) +}) test('failed calendar-round parse', () => { - const cr = new mayadates.factory.CalendarRoundFactory().parse('World'); - expect(cr).toBeNull(); -}); + const cr = new mayadates.factory.CalendarRoundFactory().parse('World') + expect(cr).toBeNull() +}) describe('parse calendar-round', () => { const sources = [ @@ -55,18 +55,18 @@ describe('parse calendar-round', () => { ['2 Ak\'bal 6Muwan', [2, 'Ak\'bal', 6, 'Muwan']], ['2Ak\'bal 6 Muwan', [2, 'Ak\'bal', 6, 'Muwan']], ['2Ak\'bal 6Muwan', [2, 'Ak\'bal', 6, 'Muwan']], - ]; + ] it.each(sources)( '%s', (source, expected) => { - const cr = new mayadates.factory.CalendarRoundFactory().parse(source); - expect(cr.tzolkin.coeff).toBe(expected[0]); - expect(cr.tzolkin.name).toBe(expected[1]); - expect(cr.haab.coeff).toBe(expected[2]); - expect(cr.haab.name).toBe(expected[3]); + const cr = new mayadates.factory.CalendarRoundFactory().parse(source) + expect(cr.tzolkin.coeff).toBe(expected[0]) + expect(cr.tzolkin.name).toBe(expected[1]) + expect(cr.haab.coeff).toBe(expected[2]) + expect(cr.haab.name).toBe(expected[3]) }, - ); -}); + ) +}) describe('parse calendar-round wildcards', () => { const sources = [ @@ -87,20 +87,20 @@ describe('parse calendar-round wildcards', () => { [2, 'Ak\'bal', 6, wildcard], '2 Ak\'bal 6 *', ], - ]; + ] it.each(sources)( '%s', (source, expected, name) => { - const cr = new mayadates.factory.CalendarRoundFactory().parse(source); - expect(cr.tzolkin.coeff).toBe(expected[0]); - expect(cr.tzolkin.name).toBe(expected[1]); - expect(cr.haab.coeff).toBe(expected[2]); - expect(cr.haab.name).toBe(expected[3]); + const cr = new mayadates.factory.CalendarRoundFactory().parse(source) + expect(cr.tzolkin.coeff).toBe(expected[0]) + expect(cr.tzolkin.name).toBe(expected[1]) + expect(cr.haab.coeff).toBe(expected[2]) + expect(cr.haab.name).toBe(expected[3]) - expect(cr.toString()).toBe(name); + expect(cr.toString()).toBe(name) }, - ); -}); + ) +}) test('render calendar round', () => { expect( @@ -110,23 +110,34 @@ test('render calendar round', () => { ).toString(), ).toBe( '4 Ajaw 8 Kumk\'u', - ); -}); + ) +}) + +// 6Kimi 9Muwan --> 3227 +// 5Kimi 4Mol --> 3447 +// 6Manik' 5Mol --> 3448 +// 13Ix 12Mol --> 3455 +// 7Ok 13 Xul --> 5971 describe('calendar round diff\'s', () => { const dates = [ ['5 Kimi 4 Mol', '6 Manik\' 5 Mol', [1]], - ['5 Kimi 4 Mol', '7 Ok 13 Xul', [-1, 17, 14, 5, 16]] - ]; - const crFactory = new mayadates.factory.CalendarRoundFactory(); + ['5 Kimi 4 Mol', '13 Ix 12 Mol', [8]], + ['5 Kimi 4 Mol', '7 Ok 13 Xul', [4, 0, 7]], + ['5 Kimi 4 Mol', '6 Kimi 9 Muwan', [0, -11]], + ['5 Kimi 4 Mol', '4 Chikchan 3 Mol', [-1]], + ] + const crFactory = new mayadates.factory.CalendarRoundFactory() it.each(dates)( '%s - %s = %s', (fromRaw, toRaw, expectRaw) => { - const from = crFactory.parse(fromRaw); - const to = crFactory.parse(toRaw); - const expected = new mayadates.lc.DistanceNumber(...expectRaw).normalise(); + const from = crFactory.parse(fromRaw) + const to = crFactory.parse(toRaw) + const expected = new mayadates.lc.DistanceNumber( + ...expectRaw, + ).normalise() - expect(from.minus(to)).toStrictEqual(expected); - } - ); -}); + expect(from.minus(to)).toStrictEqual(expected) + }, + ) +})