Skip to content

Commit

Permalink
Merge branch 'release/1.1.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsonne committed Aug 13, 2020
2 parents 7eef891 + 311c339 commit e1f38f6
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@drewsonne/maya-dates",
"version": "1.0.20",
"version": "1.1.0",
"description": "Node.js package to represent dates in the Maya Calendar",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
Expand Down
11 changes: 10 additions & 1 deletion src/cr/calendar-round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {getTzolkinDay} from "./component/tzolkinDay";
import NumberCoefficient from "./component/numberCoefficient";
import {Wildcard} from "../wildcard";
import WildcardCoefficient from "./component/wildcardCoefficient";
import IPart from "../i-part";

/** @ignore */
const singleton: { [key: string]: CalendarRound } = {};
Expand All @@ -25,14 +26,15 @@ export function getCalendarRound(
return singleton[crId];
}

// @ts-ignore
/**
* A combination of 260-day cycles and the Haab cycle. This class should not
* be instantiated directly, and should be accessed through getCalendarRound.
* @see {getCalendarRound}
* @example
* let cr = new CalendarRound(4, "Ajaw", 8, "Kumk'u");
*/
export class CalendarRound {
export class CalendarRound implements IPart {
tzolkin: Tzolkin;
haab: Haab;

Expand Down Expand Up @@ -170,6 +172,13 @@ export class CalendarRound {
toString(): string {
return `${this.tzolkin} ${this.haab}`;
}

equal(other: IPart): boolean {
if (other instanceof CalendarRound) {
throw new Error('Not Implemented')
}
return false;
}
}

/** @ignore */
Expand Down
1 change: 0 additions & 1 deletion src/cr/component/iComponent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export default interface IComponent {
isWildcard(): boolean

}
10 changes: 9 additions & 1 deletion src/cr/haab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Wildcard} from "../wildcard";
import NumberCoefficient from "./component/numberCoefficient";
import {coefficientParser as _} from "./component/coefficient";
import ICoefficient from "./component/iCoefficient";
import IPart from "../i-part";

const singleton: { [key: string]: Haab } = {};

Expand Down Expand Up @@ -33,7 +34,7 @@ export function getHaab(coeff: ICoefficient, month: Wildcard | HaabMonth): Haab
* let day = new Haab(8, new HaabMonth("Kumk'u"));
*
*/
export class Haab {
export class Haab implements IPart {
coeff: ICoefficient;
month: Wildcard | HaabMonth;
_privateNext: null | Haab;
Expand Down Expand Up @@ -176,5 +177,12 @@ export class Haab {
toString() {
return `${this.coeff} ${this.name}`;
}

equal(other: IPart): boolean {
if (other instanceof Haab) {
return other === this
}
return false;
}
}

11 changes: 7 additions & 4 deletions src/cr/tzolkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Wildcard} from "../wildcard";
import NumberCoefficient from "./component/numberCoefficient";
import WildcardCoefficient from "./component/wildcardCoefficient";
import ICoefficient from "./component/iCoefficient"
import IPart from "../i-part";

const singleton: { [key: string]: Tzolkin } = {};

Expand Down Expand Up @@ -32,7 +33,7 @@ export function getTzolkin(coeff: ICoefficient, day: TzolkinDay | Wildcard): Tzo
* let day = new Tzolkin(4, new TzolkinDay("Ajaw"));
*
*/
export class Tzolkin {
export class Tzolkin implements IPart {
day: TzolkinDay | Wildcard;
coeff: ICoefficient;
_privateNext: Tzolkin | null;
Expand Down Expand Up @@ -130,9 +131,11 @@ export class Tzolkin {
* @param {Tzolkin} newTzolkin
* @return {boolean}
*/
equal(newTzolkin: Tzolkin) {
return (this.coeff.equal(newTzolkin.coeff))
&& (this.name === newTzolkin.name);
equal(other: IPart): boolean {
if (other instanceof Tzolkin) {
return this === other
}
return false;
}

/**
Expand Down
10 changes: 9 additions & 1 deletion src/full-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
*/
import LongCount from "./lc/long-count";
import {CalendarRound} from "./cr/calendar-round";
import IPart from "./i-part";

export default class FullDate {
export default class FullDate implements IPart {

cr: CalendarRound;
lc: LongCount;
Expand All @@ -24,4 +25,11 @@ export default class FullDate {
isPartial(): boolean {
return this.cr.isPartial() || this.lc.isPartial()
}

equal(other: IPart): boolean {
if(other instanceof FullDate) {
throw new Error('Not Implemented')
}
return false;
}
}
Empty file added src/i-component.ts
Empty file.
3 changes: 3 additions & 0 deletions src/i-part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default interface IPart {
equal(other: IPart): boolean;
}
26 changes: 15 additions & 11 deletions src/lc/distance-number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import {isWildcard, Wildcard} from "../wildcard";
import LongcountAddition from "../operations/longcount-addition";
import LongcountSubtraction from "../operations/longcount-subtraction";
import IPart from "../i-part";

export default class DistanceNumber {
export default class DistanceNumber implements IPart {
parts: (number | Wildcard)[];
datePattern: RegExp;
sign: number;
Expand Down Expand Up @@ -79,18 +80,21 @@ export default class DistanceNumber {
* @param {DistanceNumber} other
* @return {boolean}
*/
equal(other: DistanceNumber): boolean {
const thisMinParts = this.sigParts;
const otherMinParts = other.sigParts;
equal(other: IPart): boolean {
if (other instanceof DistanceNumber) {
const thisMinParts = this.sigParts;
const otherMinParts = other.sigParts;

const signEqual = this.sign === other.sign;
const lengthEqual = thisMinParts.length === otherMinParts.length;
const partsEqual: boolean[] = thisMinParts.map((e, i) => {
return isWildcard(e) ? isWildcard(otherMinParts[i]) : e === otherMinParts[i]
});
const everyPartEqual = partsEqual.every((p) => p)
const signEqual = this.sign === other.sign;
const lengthEqual = thisMinParts.length === otherMinParts.length;
const partsEqual: boolean[] = thisMinParts.map((e, i) => {
return isWildcard(e) ? isWildcard(otherMinParts[i]) : e === otherMinParts[i]
});
const everyPartEqual = partsEqual.every((p) => p)

return signEqual && lengthEqual && everyPartEqual;
return signEqual && lengthEqual && everyPartEqual;
}
return false;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/lc/long-count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {origin} from "../cr/calendar-round";
import LongcountAddition from "../operations/longcount-addition";
import LongcountSubtraction from "../operations/longcount-subtraction";
import GregorianCalendarDate from "./western/gregorian";
import IPart from "../i-part";

/**
* Long Count cycle
Expand Down Expand Up @@ -118,7 +119,7 @@ export default class LongCount extends DistanceNumber {
* @param {LongCount} newLc
* @return {LongcountAddition}
*/
minus(newLc:LongCount) {
minus(newLc: LongCount) {
/* We pass the LongCount class in, as to require this in the operation
* will create a circular dependency.
*/
Expand Down
10 changes: 9 additions & 1 deletion src/operations/calendar-round-wildcard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CalendarRoundIterator from './calendar-round-iter';
import {CalendarRound} from "../cr/calendar-round";
import IPart from "../i-part";

/**
* A reusable singleton instance of the CalendarRoundIterator
Expand All @@ -12,7 +13,7 @@ const iter = new CalendarRoundIterator();
* Given a Calendar Round with a wildcard, calculate all possible matching
* fully qualified Calendar Rounds.
*/
export default class CalendarRoundWildcard {
export default class CalendarRoundWildcard implements IPart {
private cr: CalendarRound;

/**
Expand Down Expand Up @@ -46,4 +47,11 @@ export default class CalendarRoundWildcard {
iter.reset();
return potentials;
}

equal(other: IPart): boolean {
if (other instanceof CalendarRoundWildcard) {
return this.cr.equal(other.cr)
}
return false
}
}
10 changes: 9 additions & 1 deletion src/operations/fulldate-wildcard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FullDate from '../full-date';
import LongCountWildcard from './longcount-wildcard';
import IPart from "../i-part";

/** @ignore */
// const concat = (x, y) => x.concat(y);
Expand All @@ -17,7 +18,7 @@ import LongCountWildcard from './longcount-wildcard';
* Given a Calendar Round and Long Count with a wildcard, calculate all possible
* matching fully qualified Long Counts with CalendarRounds.
*/
export default class FullDateWildcard {
export default class FullDateWildcard implements IPart {
private fullDate: FullDate;

/**
Expand All @@ -30,6 +31,13 @@ export default class FullDateWildcard {
this.fullDate = partialDate;
}

equal(other: IPart): boolean {
if (other instanceof FullDateWildcard) {
return this.fullDate.equal(other.fullDate)
}
return false;
}

/**
* Run calculation to find all fully qualified Long Counts with Calendar Rounds
* @return {FullDate[]}
Expand Down
10 changes: 9 additions & 1 deletion src/operations/longcount-addition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import LongCount from "../lc/long-count";
import ILongcount from "./ILongcount";
import DistanceNumber from "../lc/distance-number";
import IPart from "../i-part";

export default class LongcountAddition {
export default class LongcountAddition implements IPart {
private a: DistanceNumber;
private b: DistanceNumber
private LcClass: ILongcount;
Expand All @@ -31,6 +32,13 @@ export default class LongcountAddition {
this.LcClass = lcClass;
}

equal(other: IPart): boolean {
if (other instanceof LongcountAddition) {
return this.a.equal(other.a) && this.b.equal(other.b)
}
return false
}

/**
* Return the sum result of this Addition operator.
* @return {LongCount}
Expand Down
10 changes: 9 additions & 1 deletion src/operations/longcount-subtraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import ILongcount from "./ILongcount";
import LongCount from "../lc/long-count";
import DistanceNumber from "../lc/distance-number";
import IPart from "../i-part";

export default class LongcountSubtraction {
export default class LongcountSubtraction implements IPart {
private a: DistanceNumber
private b: DistanceNumber
private LcClass: ILongcount
Expand Down Expand Up @@ -80,4 +81,11 @@ export default class LongcountSubtraction {
newLC.isPositive = !isInverted;
return newLC;
}

equal(other: IPart): boolean {
if (other instanceof LongcountSubtraction) {
return this.a.equal(other.a) && this.b.equal(other.b)
}
return false;
}
}
10 changes: 9 additions & 1 deletion src/operations/longcount-wildcard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import LongCount from "../lc/long-count";
import IPart from "../i-part";

/**
* Given a Long Count with a wildcard, calculate all possible matching fully
* qualified Long Counts.
*/
export default class LongCountWildcard {
export default class LongCountWildcard implements IPart {
private lc: LongCount;

/**
Expand All @@ -17,6 +18,13 @@ export default class LongCountWildcard {
this.lc = lc;
}

equal(other: IPart): boolean {
if (other instanceof LongCountWildcard) {
return other.lc.equal(other.lc)
}
return false
}

/**
* Run calculation to find all fully qualified Long Counts
* @return {LongCount[]}
Expand Down
8 changes: 5 additions & 3 deletions src/wildcard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
* console.log(cr.haab.month === mayadates.wildcard)
* > true
*/
export class Wildcard {
import IPart from "./i-part";

export class Wildcard implements IPart {
/**
* Represent the Wildcard as a string. ie, '*'.
* @returns {string}
Expand All @@ -18,8 +20,8 @@ export class Wildcard {
return '*';
}

equal(otherWildcard: any) {
return otherWildcard instanceof Wildcard
equal(other: IPart): boolean {
return other instanceof Wildcard
}
}

Expand Down

0 comments on commit e1f38f6

Please sign in to comment.