Skip to content

Commit df118ba

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 3333f8e commit df118ba

File tree

3 files changed

+117
-162
lines changed

3 files changed

+117
-162
lines changed

polyfill/lib/ecmascript.mjs

+51-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,23 @@ 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 } = ParseTemporalDurationStringRaw(isoString);
699+
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
700+
return new TemporalDuration(
701+
years,
702+
months,
703+
weeks,
704+
days,
705+
hours,
706+
minutes,
707+
seconds,
708+
milliseconds,
709+
microseconds,
710+
nanoseconds
711+
);
712+
}
713+
697714
export function RegulateISODate(year, month, day, overflow) {
698715
switch (overflow) {
699716
case 'reject':
@@ -738,49 +755,6 @@ export function RegulateISOYearMonth(year, month, overflow) {
738755
return { year, month };
739756
}
740757

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-
784758
export function ToTemporalPartialDurationRecord(temporalDurationLike) {
785759
if (Type(temporalDurationLike) !== 'Object') {
786760
throw new TypeErrorCtor('invalid duration-like');
@@ -1312,20 +1286,41 @@ export function ToTemporalDateTime(item, options = undefined) {
13121286

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

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)