Skip to content

Commit

Permalink
Add custom inspect for node without depedency (#1526)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropedruzzi committed Nov 12, 2023
1 parent 8ef0828 commit eefe64e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/datetime.js
Expand Up @@ -1935,6 +1935,18 @@ export default class DateTime {
return this.isValid ? this.toISO() : INVALID;
}

/**
* Returns a string representation of this DateTime appropriate for the REPL.
* @return {string}
*/
[Symbol.for("nodejs.util.inspect.custom")]() {
if (this.isValid) {
return `DateTime { ts: ${this.toISO()}, zone: ${this.zone.name}, locale: ${this.locale} }`;
} else {
return `DateTime { Invalid, reason: ${this.invalidReason} }`;
}
}

/**
* Returns the epoch milliseconds of this DateTime. Alias of {@link DateTime#toMillis}
* @return {number}
Expand Down
12 changes: 12 additions & 0 deletions src/duration.js
Expand Up @@ -608,6 +608,18 @@ export default class Duration {
return this.toISO();
}

/**
* Returns a string representation of this Duration appropriate for the REPL.
* @return {string}
*/
[Symbol.for("nodejs.util.inspect.custom")]() {
if (this.isValid) {
return `Duration { values: ${JSON.stringify(this.values)} }`;
} else {
return `Duration { Invalid, reason: ${this.invalidReason} }`;
}
}

/**
* Returns an milliseconds value of this Duration.
* @return {number}
Expand Down
12 changes: 12 additions & 0 deletions src/interval.js
Expand Up @@ -539,6 +539,18 @@ export default class Interval {
return `[${this.s.toISO()}${this.e.toISO()})`;
}

/**
* Returns a string representation of this Interval appropriate for the REPL.
* @return {string}
*/
[Symbol.for("nodejs.util.inspect.custom")]() {
if (this.isValid) {
return `Interval { start: ${this.s.toISO()}, end: ${this.e.toISO()} }`;
} else {
return `Interval { Invalid, reason: ${this.invalidReason} }`;
}
}

/**
* Returns a localized string representing this Interval. Accepts the same options as the
* Intl.DateTimeFormat constructor and any presets defined by Luxon, such as
Expand Down

0 comments on commit eefe64e

Please sign in to comment.