tc39 / proposal-extractors

Extractors for ECMAScript
http://tc39.es/proposal-extractors/
MIT License
200 stars 3 forks source link

Making `InstantExtractor` more readable #16

Closed codehag closed 4 months ago

codehag commented 4 months ago

One of the examples has the following:

const InstantExtractor = {
  [Symbol.customMatcher]: value =>
    value instanceof Temporal.Instant ? [value] :
    value instanceof Date ? [Temporal.Instant.fromEpochMilliseconds(value.getTime())] :
    typeof value === "string" ? [Temporal.Instant.from(value)] :
    false
};

This ternary statement is quite hard to read. Can we switch this for a clearer, longer version using if statements?

similar to elsewhere in the readme:

function toInstant(value) {
  if (value instanceof Temporal.Instant) {
    return value;
  } else if (value instanceof Date) {
    return Temporal.Instant.fromEpochMilliseconds(+value);
  } else if (typeof value === "string") {
    return Temporal.Instant.from(value);
  } else {
    throw new TypeError();
  }
}