Skip to content

Instantly share code, notes, and snippets.

@getify
Last active March 21, 2024 09:11
Show Gist options
  • Select an option

  • Save getify/3b4f46cdd0b204eb03f2ba36e84e5948 to your computer and use it in GitHub Desktop.

Select an option

Save getify/3b4f46cdd0b204eb03f2ba36e84e5948 to your computer and use it in GitHub Desktop.

Revisions

  1. getify revised this gist Jul 20, 2022. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions 1-CalendarItem.js
    Original 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);
  2. getify revised this gist Jul 16, 2022. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion 2-Reminder.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    class Reminder extends CalendarItem {
    #complete = false
    #complete = false; // <-- no ASI, semicolon needed

    [Symbol.toStringTag] = "Reminder"
    constructor(description,startDateTime) {
    2 changes: 1 addition & 1 deletion 3-Meeting.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ class Meeting extends CalendarItem {
    }
    }

    endDateTime = null
    endDateTime = null; // <-- no ASI, semicolon needed

    [Symbol.toStringTag] = "Meeting"
    constructor(description,startDateTime,endDateTime) {
  3. getify revised this gist Jul 16, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 3-Meeting.js
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,12 @@
    class Meeting extends CalendarItem {
    endDateTime = null

    #getEndDateTimeStr() {
    if (this.endDateTime instanceof Date) {
    return this.endDateTime.toUTCString();
    }
    }

    endDateTime = null

    [Symbol.toStringTag] = "Meeting"
    constructor(description,startDateTime,endDateTime) {
    super();
  4. getify revised this gist Jul 16, 2022. 3 changed files with 18 additions and 5 deletions.
    1 change: 1 addition & 0 deletions 2-Reminder.js
    Original 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();

    1 change: 1 addition & 0 deletions 3-Meeting.js
    Original 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();

    21 changes: 16 additions & 5 deletions 4.js
    Original 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);
  5. getify revised this gist Jul 16, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions 1-CalendarItem.js
    Original 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}`] = msg;
    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_0);
    throw new Error(CalendarItem.ERROR_100);
    }
    }

    @@ -40,15 +40,15 @@ class CalendarItem {
    this.#setID(id);
    }
    else {
    throw new Error(CalendarItem.ERROR_2);
    throw new Error(CalendarItem.ERROR_300);
    }
    }
    getID() {
    if (!CalendarItem.#isUnset(this.#ID)) {
    return this.#ID;
    }
    else {
    throw new Error(CalendarItem.ERROR_1);
    throw new Error(CalendarItem.ERROR_200);
    }
    }
    getDateTimeStr() {
  6. getify revised this gist Jul 16, 2022. 2 changed files with 20 additions and 4 deletions.
    15 changes: 12 additions & 3 deletions 1-CalendarItem.js
    Original 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("ID is already set");
    throw new Error(CalendarItem.ERROR_0);
    }
    }

    @@ -31,15 +40,15 @@ class CalendarItem {
    this.#setID(id);
    }
    else {
    throw new Error("Don't instantiate 'CalendarItem' directly.");
    throw new Error(CalendarItem.ERROR_2);
    }
    }
    getID() {
    if (!CalendarItem.#isUnset(this.#ID)) {
    return this.#ID;
    }
    else {
    throw new Error("ID is unset");
    throw new Error(CalendarItem.ERROR_1);
    }
    }
    getDateTimeStr() {
    9 changes: 8 additions & 1 deletion 4.js
    Original 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
    // 09:15:00 GMT



    Reminder.isSameItem(callMyParents,callMyParents);
    // true
    Meeting.isSameItem(callMyParents,interview);
    // false
  7. getify revised this gist Jul 16, 2022. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions 1-CalendarItem.js
    Original file line number Diff line number Diff line change
    @@ -26,8 +26,13 @@ class CalendarItem {
    startDateTime = null

    constructor() {
    var id = Math.round(Math.random() * 1e9);
    this.#setID(id);
    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)) {
  8. getify created this gist Jul 16, 2022.
    54 changes: 54 additions & 0 deletions 1-CalendarItem.js
    Original 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()
    }`);
    }
    }
    24 changes: 24 additions & 0 deletions 2-Reminder.js
    Original 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();
    }
    }
    }
    24 changes: 24 additions & 0 deletions 3-Meeting.js
    Original 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()
    }`;
    }
    }
    25 changes: 25 additions & 0 deletions 4.js
    Original 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