Skip to content

Commit 748f27f

Browse files
ptomatoMs2ger
authored andcommitted
Editorial: Remove Duration Records
These are no longer necessary. We should either be using Normalized Duration Records (or their subcomponents, Date Duration Records and Normalized Time Duration Records) or Temporal.Duration instances. The records were still used in a few places: ToTemporalDurationRecord can be inlined into ToTemporalDuration since that is its only caller, and ParseTemporalDurationString can just return a Temporal.Duration instance directly. We keep ToTemporalDurationRecordRaw for the validStrings.js test.
1 parent 365e463 commit 748f27f

File tree

3 files changed

+118
-162
lines changed

3 files changed

+118
-162
lines changed

polyfill/lib/ecmascript.mjs

+52-56
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ export function ParseTemporalTimeZoneString(stringIdent) {
641641
throw new ErrorCtor('this line should not be reached');
642642
}
643643

644-
export function ParseTemporalDurationString(isoString) {
644+
export function ParseTemporalDurationStringRaw(isoString) {
645645
const match = Call(RegExpPrototypeExec, PARSE.duration, [isoString]);
646646
if (!match) throw new RangeErrorCtor(`invalid duration: ${isoString}`);
647647
if (Call(ArrayPrototypeEvery, match, [(part, i) => i < 2 || part === undefined])) {
@@ -694,6 +694,24 @@ export function ParseTemporalDurationString(isoString) {
694694
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds };
695695
}
696696

697+
function ParseTemporalDurationString(isoString) {
698+
const { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
699+
ParseTemporalDurationStringRaw(isoString);
700+
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
701+
return new TemporalDuration(
702+
years,
703+
months,
704+
weeks,
705+
days,
706+
hours,
707+
minutes,
708+
seconds,
709+
milliseconds,
710+
microseconds,
711+
nanoseconds
712+
);
713+
}
714+
697715
export function RegulateISODate(year, month, day, overflow) {
698716
switch (overflow) {
699717
case 'reject':
@@ -738,49 +756,6 @@ export function RegulateISOYearMonth(year, month, overflow) {
738756
return { year, month };
739757
}
740758

741-
export function ToTemporalDurationRecord(item) {
742-
if (Type(item) !== 'Object') {
743-
return ParseTemporalDurationString(RequireString(item));
744-
}
745-
if (IsTemporalDuration(item)) {
746-
return {
747-
years: GetSlot(item, YEARS),
748-
months: GetSlot(item, MONTHS),
749-
weeks: GetSlot(item, WEEKS),
750-
days: GetSlot(item, DAYS),
751-
hours: GetSlot(item, HOURS),
752-
minutes: GetSlot(item, MINUTES),
753-
seconds: GetSlot(item, SECONDS),
754-
milliseconds: GetSlot(item, MILLISECONDS),
755-
microseconds: GetSlot(item, MICROSECONDS),
756-
nanoseconds: GetSlot(item, NANOSECONDS)
757-
};
758-
}
759-
const result = {
760-
years: 0,
761-
months: 0,
762-
weeks: 0,
763-
days: 0,
764-
hours: 0,
765-
minutes: 0,
766-
seconds: 0,
767-
milliseconds: 0,
768-
microseconds: 0,
769-
nanoseconds: 0
770-
};
771-
let partial = ToTemporalPartialDurationRecord(item);
772-
for (let index = 0; index < DURATION_FIELDS.length; index++) {
773-
const property = DURATION_FIELDS[index];
774-
const value = partial[property];
775-
if (value !== undefined) {
776-
result[property] = value;
777-
}
778-
}
779-
let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = result;
780-
RejectDuration(years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
781-
return result;
782-
}
783-
784759
export function ToTemporalPartialDurationRecord(temporalDurationLike) {
785760
if (Type(temporalDurationLike) !== 'Object') {
786761
throw new TypeErrorCtor('invalid duration-like');
@@ -1312,20 +1287,41 @@ export function ToTemporalDateTime(item, options = undefined) {
13121287

13131288
export function ToTemporalDuration(item) {
13141289
if (IsTemporalDuration(item)) return item;
1315-
let { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } =
1316-
ToTemporalDurationRecord(item);
1290+
if (Type(item) !== 'Object') {
1291+
return ParseTemporalDurationString(RequireString(item));
1292+
}
1293+
const result = {
1294+
years: 0,
1295+
months: 0,
1296+
weeks: 0,
1297+
days: 0,
1298+
hours: 0,
1299+
minutes: 0,
1300+
seconds: 0,
1301+
milliseconds: 0,
1302+
microseconds: 0,
1303+
nanoseconds: 0
1304+
};
1305+
let partial = ToTemporalPartialDurationRecord(item);
1306+
for (let index = 0; index < DURATION_FIELDS.length; index++) {
1307+
const property = DURATION_FIELDS[index];
1308+
const value = partial[property];
1309+
if (value !== undefined) {
1310+
result[property] = value;
1311+
}
1312+
}
13171313
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
13181314
return new TemporalDuration(
1319-
years,
1320-
months,
1321-
weeks,
1322-
days,
1323-
hours,
1324-
minutes,
1325-
seconds,
1326-
milliseconds,
1327-
microseconds,
1328-
nanoseconds
1315+
result.years,
1316+
result.months,
1317+
result.weeks,
1318+
result.days,
1319+
result.hours,
1320+
result.minutes,
1321+
result.seconds,
1322+
result.milliseconds,
1323+
result.microseconds,
1324+
result.nanoseconds
13291325
);
13301326
}
13311327

spec/abstractops.html

+2-3
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ <h1>
17471747
<h1>
17481748
ParseTemporalDurationString (
17491749
_isoString_: a String,
1750-
): either a normal completion containing a Duration Record or a throw completion
1750+
): either a normal completion containing a Temporal.Duration or a throw completion
17511751
</h1>
17521752
<dl class="header">
17531753
<dt>description</dt>
@@ -1843,8 +1843,7 @@ <h1>
18431843
1. Set _millisecondsMV_ to floor(_millisecondsMV_) × _factor_.
18441844
1. Set _microsecondsMV_ to floor(_microsecondsMV_) × _factor_.
18451845
1. Set _nanosecondsMV_ to floor(_nanosecondsMV_) × _factor_.
1846-
1. If IsValidDuration(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_) is *false*, throw a *RangeError* exception.
1847-
1. Return CreateDurationRecord(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_).
1846+
1. Return ? CreateTemporalDuration(_yearsMV_, _monthsMV_, _weeksMV_, _daysMV_, _hoursMV_, _minutesMV_, _secondsMV_, _millisecondsMV_, _microsecondsMV_, _nanosecondsMV_).
18481847
</emu-alg>
18491848
</emu-clause>
18501849

0 commit comments

Comments
 (0)