Skip to content

Commit

Permalink
Implemented calendar round operations
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsonne committed Mar 2, 2020
1 parent 6fc3ac5 commit 4ff18aa
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 217 deletions.
129 changes: 69 additions & 60 deletions src/cr/calendar-round.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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]
}

/**
Expand All @@ -42,67 +42,69 @@ 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()
}

/**
* Validate that the Calendar Round has a correct 260-day and Haab
* 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}`)
}
}

/**
* 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)
}

/**
Expand All @@ -111,30 +113,38 @@ class CalendarRound {
* @param {CalendarRound} newCr
* @return {Boolean}
*/
equal(newCr) {
return this === newCr;
equal (newCr) {
return this === newCr
}

/**
* Return a long count date representing the difference between two dates.
* @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
}
}
}
Expand All @@ -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
}

/**
Expand All @@ -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}
Loading

0 comments on commit 4ff18aa

Please sign in to comment.