From cb8a049bdae5573a061421cbaeb9c8c3403a9575 Mon Sep 17 00:00:00 2001 From: "Drew J. Sonne" Date: Thu, 13 Aug 2020 19:08:59 +0100 Subject: [PATCH 1/2] Add component interface --- src/cr/calendar-round.ts | 11 +++++++++- src/cr/component/iComponent.ts | 1 - src/cr/haab.ts | 10 ++++++++- src/cr/tzolkin.ts | 11 ++++++---- src/full-date.ts | 10 ++++++++- src/i-component.ts | 0 src/i-part.ts | 3 +++ src/lc/distance-number.ts | 26 +++++++++++++---------- src/lc/long-count.ts | 3 ++- src/operations/calendar-round-wildcard.ts | 10 ++++++++- src/operations/fulldate-wildcard.ts | 10 ++++++++- src/operations/longcount-addition.ts | 10 ++++++++- src/operations/longcount-subtraction.ts | 10 ++++++++- src/operations/longcount-wildcard.ts | 10 ++++++++- src/wildcard.ts | 8 ++++--- 15 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 src/i-component.ts create mode 100644 src/i-part.ts diff --git a/src/cr/calendar-round.ts b/src/cr/calendar-round.ts index ac19ed0..01aeb25 100644 --- a/src/cr/calendar-round.ts +++ b/src/cr/calendar-round.ts @@ -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 } = {}; @@ -25,6 +26,7 @@ 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. @@ -32,7 +34,7 @@ export function getCalendarRound( * @example * let cr = new CalendarRound(4, "Ajaw", 8, "Kumk'u"); */ -export class CalendarRound { +export class CalendarRound implements IPart { tzolkin: Tzolkin; haab: Haab; @@ -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 */ diff --git a/src/cr/component/iComponent.ts b/src/cr/component/iComponent.ts index c0fe93b..bceaeca 100644 --- a/src/cr/component/iComponent.ts +++ b/src/cr/component/iComponent.ts @@ -1,4 +1,3 @@ export default interface IComponent { isWildcard(): boolean - } diff --git a/src/cr/haab.ts b/src/cr/haab.ts index 9ea916c..da7c771 100644 --- a/src/cr/haab.ts +++ b/src/cr/haab.ts @@ -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 } = {}; @@ -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; @@ -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; + } } diff --git a/src/cr/tzolkin.ts b/src/cr/tzolkin.ts index 1779d7f..0b1d265 100644 --- a/src/cr/tzolkin.ts +++ b/src/cr/tzolkin.ts @@ -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 } = {}; @@ -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; @@ -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; } /** diff --git a/src/full-date.ts b/src/full-date.ts index 4fa0ce0..0178e08 100644 --- a/src/full-date.ts +++ b/src/full-date.ts @@ -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; @@ -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; + } } diff --git a/src/i-component.ts b/src/i-component.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/i-part.ts b/src/i-part.ts new file mode 100644 index 0000000..6dab847 --- /dev/null +++ b/src/i-part.ts @@ -0,0 +1,3 @@ +export default interface IPart { + equal(other: IPart): boolean; +} diff --git a/src/lc/distance-number.ts b/src/lc/distance-number.ts index 2baf222..046edb5 100644 --- a/src/lc/distance-number.ts +++ b/src/lc/distance-number.ts @@ -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; @@ -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; } /** diff --git a/src/lc/long-count.ts b/src/lc/long-count.ts index ecfde5b..0beaa0d 100644 --- a/src/lc/long-count.ts +++ b/src/lc/long-count.ts @@ -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 @@ -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. */ diff --git a/src/operations/calendar-round-wildcard.ts b/src/operations/calendar-round-wildcard.ts index 2d194da..ee11f8b 100644 --- a/src/operations/calendar-round-wildcard.ts +++ b/src/operations/calendar-round-wildcard.ts @@ -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 @@ -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; /** @@ -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 + } } diff --git a/src/operations/fulldate-wildcard.ts b/src/operations/fulldate-wildcard.ts index 6e72768..a252d4c 100644 --- a/src/operations/fulldate-wildcard.ts +++ b/src/operations/fulldate-wildcard.ts @@ -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); @@ -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; /** @@ -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[]} diff --git a/src/operations/longcount-addition.ts b/src/operations/longcount-addition.ts index ee36891..9048749 100644 --- a/src/operations/longcount-addition.ts +++ b/src/operations/longcount-addition.ts @@ -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; @@ -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} diff --git a/src/operations/longcount-subtraction.ts b/src/operations/longcount-subtraction.ts index c231e8a..5c2b4fe 100644 --- a/src/operations/longcount-subtraction.ts +++ b/src/operations/longcount-subtraction.ts @@ -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 @@ -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; + } } diff --git a/src/operations/longcount-wildcard.ts b/src/operations/longcount-wildcard.ts index 8df263a..4c2ae29 100644 --- a/src/operations/longcount-wildcard.ts +++ b/src/operations/longcount-wildcard.ts @@ -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; /** @@ -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[]} diff --git a/src/wildcard.ts b/src/wildcard.ts index 14c2c4d..83e2f85 100644 --- a/src/wildcard.ts +++ b/src/wildcard.ts @@ -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} @@ -18,8 +20,8 @@ export class Wildcard { return '*'; } - equal(otherWildcard: any) { - return otherWildcard instanceof Wildcard + equal(other: IPart): boolean { + return other instanceof Wildcard } } From 311c339c65e84cb6b683375f793fde1cae6891fd Mon Sep 17 00:00:00 2001 From: "Drew J. Sonne" Date: Thu, 13 Aug 2020 19:09:34 +0100 Subject: [PATCH 2/2] Version Bump --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9436b3e..6d6332e 100644 --- a/package.json +++ b/package.json @@ -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",