Last active
March 21, 2024 09:11
-
-
Save getify/3b4f46cdd0b204eb03f2ba36e84e5948 to your computer and use it in GitHub Desktop.
Revisions
-
getify revised this gist
Jul 20, 2022 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // abstract class, not intended to be instantiated directly class CalendarItem { static #UNSET = Symbol("unset") static #isUnset(v) { @@ -35,6 +36,7 @@ class CalendarItem { startDateTime = null constructor() { // enforcing: abstract class, no direct instantiation if (new.target !== CalendarItem) { let id = Math.round(Math.random() * 1e9); this.#setID(id); -
getify revised this gist
Jul 16, 2022 . 2 changed files with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ class Reminder extends CalendarItem { #complete = false; // <-- no ASI, semicolon needed [Symbol.toStringTag] = "Reminder" constructor(description,startDateTime) { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class Meeting extends CalendarItem { } } endDateTime = null; // <-- no ASI, semicolon needed [Symbol.toStringTag] = "Meeting" constructor(description,startDateTime,endDateTime) { -
getify revised this gist
Jul 16, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,12 @@ class Meeting extends CalendarItem { #getEndDateTimeStr() { if (this.endDateTime instanceof Date) { return this.endDateTime.toUTCString(); } } endDateTime = null [Symbol.toStringTag] = "Meeting" constructor(description,startDateTime,endDateTime) { super(); -
getify revised this gist
Jul 16, 2022 . 3 changed files with 18 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,7 @@ class Reminder extends CalendarItem { #complete = false [Symbol.toStringTag] = "Reminder" constructor(description,startDateTime) { super(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,7 @@ class Meeting extends CalendarItem { } } [Symbol.toStringTag] = "Meeting" constructor(description,startDateTime,endDateTime) { super(); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,28 +2,39 @@ var callMyParents = new Reminder( "Call my parents to say hi", new Date("July 7, 2022 11:00:00 UTC") ); callMyParents.toString(); // [object Reminder] callMyParents.summary(); // (586380912) Call my parents to say hi at // Thu, 07 Jul 2022 11:00:00 GMT callMyParents.markComplete(); callMyParents.summary(); // (586380912) Complete. callMyParents instanceof Reminder; // true callMyParents instanceof CalendarItem; // true callMyParents instanceof Meeting; // false var interview = new Meeting( "Job Interview: ABC Tech", new Date("June 23, 2022 08:30:00 UTC"), new Date("June 23, 2022 09:15:00 UTC") ); interview.toString(); // [object Meeting] interview.summary(); // (994337604) Job Interview: ABC Tech at Thu, // 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022 // 09:15:00 GMT interview instanceof Meeting; // true interview instanceof CalendarItem; // true interview instanceof Reminder; // false Reminder.isSameItem(callMyParents,callMyParents); -
getify revised this gist
Jul 16, 2022 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ class CalendarItem { "ID is unset.", "Don't instantiate 'CalendarItem' directly.", ].entries()) { this[`ERROR_${(idx+1)*100}`] = msg; } } static isSameItem(item1,item2) { @@ -27,7 +27,7 @@ class CalendarItem { this.#ID = id; } else { throw new Error(CalendarItem.ERROR_100); } } @@ -40,15 +40,15 @@ class CalendarItem { this.#setID(id); } else { throw new Error(CalendarItem.ERROR_300); } } getID() { if (!CalendarItem.#isUnset(this.#ID)) { return this.#ID; } else { throw new Error(CalendarItem.ERROR_200); } } getDateTimeStr() { -
getify revised this gist
Jul 16, 2022 . 2 changed files with 20 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,15 @@ class CalendarItem { static #isUnset(v) { return v === this.#UNSET; } static { for (let [idx,msg] of [ "ID is already set.", "ID is unset.", "Don't instantiate 'CalendarItem' directly.", ].entries()) { this[`ERROR_${idx}`] = msg; } } static isSameItem(item1,item2) { if (#ID in item1 && #ID in item2) { return item1.#ID === item2.#ID; @@ -18,7 +27,7 @@ class CalendarItem { this.#ID = id; } else { throw new Error(CalendarItem.ERROR_0); } } @@ -31,15 +40,15 @@ class CalendarItem { this.#setID(id); } else { throw new Error(CalendarItem.ERROR_2); } } getID() { if (!CalendarItem.#isUnset(this.#ID)) { return this.#ID; } else { throw new Error(CalendarItem.ERROR_1); } } getDateTimeStr() { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,4 +22,11 @@ var interview = new Meeting( interview.summary(); // (994337604) Job Interview: ABC Tech at Thu, // 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022 // 09:15:00 GMT Reminder.isSameItem(callMyParents,callMyParents); // true Meeting.isSameItem(callMyParents,interview); // false -
getify revised this gist
Jul 16, 2022 . 1 changed file with 7 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,13 @@ class CalendarItem { startDateTime = null constructor() { if (new.target !== CalendarItem) { let id = Math.round(Math.random() * 1e9); this.#setID(id); } else { throw new Error("Don't instantiate 'CalendarItem' directly."); } } getID() { if (!CalendarItem.#isUnset(this.#ID)) { -
getify created this gist
Jul 16, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ class CalendarItem { static #UNSET = Symbol("unset") static #isUnset(v) { return v === this.#UNSET; } static isSameItem(item1,item2) { if (#ID in item1 && #ID in item2) { return item1.#ID === item2.#ID; } else { return false; } } #ID = CalendarItem.#UNSET #setID(id) { if (CalendarItem.#isUnset(this.#ID)) { this.#ID = id; } else { throw new Error("ID is already set"); } } description = null startDateTime = null constructor() { var id = Math.round(Math.random() * 1e9); this.#setID(id); } getID() { if (!CalendarItem.#isUnset(this.#ID)) { return this.#ID; } else { throw new Error("ID is unset"); } } getDateTimeStr() { if (this.startDateTime instanceof Date) { return this.startDateTime.toUTCString(); } } summary() { console.log(`(${ this.getID() }) ${ this.description } at ${ this.getDateTimeStr() }`); } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ class Reminder extends CalendarItem { #complete = false constructor(description,startDateTime) { super(); this.description = description; this.startDateTime = startDateTime; } isComplete() { return !!this.#complete; } markComplete() { this.#complete = true; } summary() { if (this.isComplete()) { console.log(`(${this.getID()}) Complete.`); } else { super.summary(); } } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ class Meeting extends CalendarItem { endDateTime = null #getEndDateTimeStr() { if (this.endDateTime instanceof Date) { return this.endDateTime.toUTCString(); } } constructor(description,startDateTime,endDateTime) { super(); this.description = description; this.startDateTime = startDateTime; this.endDateTime = endDateTime; } getDateTimeStr() { return `${ super.getDateTimeStr() } - ${ this.#getEndDateTimeStr() }`; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ var callMyParents = new Reminder( "Call my parents to say hi", new Date("July 7, 2022 11:00:00 UTC") ); callMyParents.summary(); // (586380912) Call my parents to say hi at // Thu, 07 Jul 2022 11:00:00 GMT callMyParents.markComplete(); callMyParents.summary(); // (586380912) Complete. var interview = new Meeting( "Job Interview: ABC Tech", new Date("June 23, 2022 08:30:00 UTC"), new Date("June 23, 2022 09:15:00 UTC") ); interview.summary(); // (994337604) Job Interview: ABC Tech at Thu, // 23 Jun 2022 08:30:00 GMT - Thu, 23 Jun 2022 // 09:15:00 GMT