Skip to content

Commit

Permalink
Reorganize from() to clone the object
Browse files Browse the repository at this point in the history
Each type's from() method now calls a new abstract operation,
ToFooRecord, which takes a property bag (which can be the actual type)
and returns a Record with the appropriate slots.

If the argument isn't an object, then from() calls ParseFooString
directly. We delete the FooFromString operations, and in most cases the
ToFoo operations unless there is a method somewhere in the API that
actually needs the casting behaviour (including returning the original
object if it's of the correct type.)

Reorganizes the abstract operations in the polyfill to match the
abstract operations in the spec a bit more.

Effectively, this makes Temporal.Foo.from(aFoo) clone the object instead
of returning the same object.

Closes: #232.
  • Loading branch information
ptomato authored and Ms2ger committed Mar 26, 2020
1 parent 6ddea73 commit 6b700dd
Show file tree
Hide file tree
Showing 25 changed files with 489 additions and 320 deletions.
10 changes: 7 additions & 3 deletions polyfill/lib/absolute.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,13 @@ export class Absolute {
return result;
}
static from(item) {
const absolute = ES.ToTemporalAbsolute(item);
if (this === Absolute) return absolute;
const result = new this(GetSlot(absolute, EPOCHNANOSECONDS).value);
let absolute;
if (ES.IsAbsolute(item)) {
absolute = GetSlot(item, EPOCHNANOSECONDS).value;
} else {
absolute = ES.TemporalAbsoluteFromString(ES.ToString(item));
}
const result = new this(absolute);
if (!ES.IsTemporalAbsolute(result)) throw new TypeError('invalid result');
return result;
}
Expand Down
16 changes: 12 additions & 4 deletions polyfill/lib/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Date {
with(temporalDateLike = {}, options) {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
const disambiguation = ES.ToTemporalDisambiguation(options);
const props = ES.ValidPropertyBag(temporalDateLike, ['year', 'month', 'day']);
const props = ES.ToPartialRecord(temporalDateLike, ['year', 'month', 'day']);
if (!props) {
throw new RangeError('invalid date-like');
}
Expand Down Expand Up @@ -170,9 +170,17 @@ export class Date {
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let result = ES.ToTemporalDate(item, disambiguation);
if (this === Date) return result;
return new this(GetSlot(result, YEAR), GetSlot(result, MONTH), GetSlot(result, DAY));
let year, month, day;
if (typeof item === 'object' && item) {
// Intentionally alphabetical
({ year, month, day } = ES.ToRecord(item, ['day', 'month', 'year']));
} else {
({ year, month, day } = ES.ParseDateString(ES.ToString(item)));
}
({ year, month, day } = ES.RegulateDate(year, month, day, disambiguation));
const result = new this(year, month, day);
if (!ES.IsDate(result)) throw new TypeError('invalid result');
return result;
}
static compare(one, two) {
if (!ES.IsTemporalDate(one) || !ES.IsTemporalDate(two)) throw new TypeError('invalid Date object');
Expand Down
48 changes: 34 additions & 14 deletions polyfill/lib/datetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class DateTime {
with(temporalDateTimeLike, options) {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
const disambiguation = ES.ToTemporalDisambiguation(options);
const props = ES.ValidPropertyBag(temporalDateTimeLike, [
const props = ES.ToPartialRecord(temporalDateTimeLike, [
'year',
'month',
'day',
Expand Down Expand Up @@ -316,19 +316,39 @@ export class DateTime {

static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let result = ES.ToTemporalDateTime(item, disambiguation);
if (this === DateTime) return result;
return new this(
GetSlot(result, YEAR),
GetSlot(result, MONTH),
GetSlot(result, DAY),
GetSlot(result, HOUR),
GetSlot(result, MINUTE),
GetSlot(result, SECOND),
GetSlot(result, MILLISECOND),
GetSlot(result, MICROSECOND),
GetSlot(result, NANOSECOND)
);
let year, month, day, hour, minute, second, millisecond, microsecond, nanosecond;
if (typeof item === 'object' && item) {
({
year,
month,
day,
hour = 0,
minute = 0,
second = 0,
millisecond = 0,
microsecond = 0,
nanosecond = 0
} = ES.ToDateTimeRecord(item));
} else {
({ year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = ES.ParseDateTimeString(
ES.ToString(item)
));
}
({ year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = ES.RegulateDateTime(
year,
month,
day,
hour,
minute,
second,
millisecond,
microsecond,
nanosecond,
disambiguation
));
const result = new this(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
if (!ES.IsDateTime(result)) throw new TypeError('invalid result');
return result;
}
static compare(one, two) {
if (!ES.IsTemporalDateTime(one) || !ES.IsTemporalDateTime(two)) throw new TypeError('invalid DateTime object');
Expand Down
59 changes: 46 additions & 13 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,52 @@ export class Duration {
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let result = ES.ToTemporalDuration(item, disambiguation);
if (this === Duration) return result;
return new this(
GetSlot(result, YEARS),
GetSlot(result, MONTHS),
GetSlot(result, DAYS),
GetSlot(result, HOURS),
GetSlot(result, MINUTES),
GetSlot(result, SECONDS),
GetSlot(result, MILLISECONDS),
GetSlot(result, MICROSECONDS),
GetSlot(result, NANOSECONDS)
);
let years, months, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds;
if (typeof arg === 'object' && arg) {
({
years = 0,
months = 0,
days = 0,
hours = 0,
minutes = 0,
seconds = 0,
milliseconds = 0,
microseconds = 0,
nanoseconds = 0
} = ES.ToRecord(
arg,
[],
// Intentionally alphabetical
['days', 'hours', 'microseconds', 'milliseconds', 'minutes', 'months', 'nanoseconds', 'seconds', 'years']
));
} else {
({
years,
months,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds
} = ES.ParseDurationString(ES.ToString(arg)));
}
({ years, months, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.RegulateDuration(
years,
months,
days,
hours,
minutes,
seconds,
milliseconds,
microseconds,
nanoseconds,
disambiguation
));
const result = new this(years, months, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
if (!ES.IsDuration(result)) throw new TypeError('invalid result');
return result;
}
}
Duration.prototype.toJSON = Duration.prototype.toString;
Expand Down
Loading

0 comments on commit 6b700dd

Please sign in to comment.