Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editorial: Rename "normalize" and "unnormalize" for durations #3007

Merged
merged 9 commits into from
Oct 9, 2024
45 changes: 24 additions & 21 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class Duration {
}

if (zonedRelativeTo) {
let duration = ES.NormalizeDuration(this);
let duration = ES.ToInternalDurationRecord(this);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const relativeEpochNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
Expand All @@ -247,12 +247,12 @@ export class Duration {
roundingMode
);
if (ES.TemporalUnitCategory(largestUnit) === 'date') largestUnit = 'hour';
return ES.UnnormalizeDuration(duration, largestUnit);
return ES.TemporalDurationFromInternal(duration, largestUnit);
}

if (plainRelativeTo) {
let duration = ES.NormalizeDurationWith24HourDays(this);
const targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);
let duration = ES.ToInternalDurationRecordWith24HourDays(this);
const targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.time);

// Delegate the date part addition to the calendar
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
Expand All @@ -271,7 +271,7 @@ export class Duration {
smallestUnit,
roundingMode
);
return ES.UnnormalizeDuration(duration, largestUnit);
return ES.TemporalDurationFromInternal(duration, largestUnit);
}

// No reference date to calculate difference relative to
Expand All @@ -282,9 +282,9 @@ export class Duration {
throw new RangeErrorCtor(`a starting point is required for ${largestUnit}s balancing`);
}
assert(!ES.IsCalendarUnit(smallestUnit), 'smallestUnit was larger than largestUnit');
let duration = ES.NormalizeDurationWith24HourDays(this);
let duration = ES.ToInternalDurationRecordWith24HourDays(this);
duration = ES.RoundTimeDuration(duration, roundingIncrement, smallestUnit, roundingMode);
return ES.UnnormalizeDuration(duration, largestUnit);
return ES.TemporalDurationFromInternal(duration, largestUnit);
}
total(totalOf) {
if (!ES.IsTemporalDuration(this)) throw new TypeErrorCtor('invalid receiver');
Expand All @@ -301,7 +301,7 @@ export class Duration {
const unit = ES.GetTemporalUnitValuedOption(totalOf, 'unit', 'datetime', ES.REQUIRED);

if (zonedRelativeTo) {
const duration = ES.NormalizeDuration(this);
const duration = ES.ToInternalDurationRecord(this);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const relativeEpochNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
Expand All @@ -310,8 +310,8 @@ export class Duration {
}

if (plainRelativeTo) {
const duration = ES.NormalizeDurationWith24HourDays(this);
let targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.norm);
const duration = ES.ToInternalDurationRecordWith24HourDays(this);
let targetTime = ES.AddTime(ES.MidnightTimeRecord(), duration.time);

// Delegate the date part addition to the calendar
const isoRelativeToDate = GetSlot(plainRelativeTo, ISO_DATE);
Expand All @@ -332,8 +332,8 @@ export class Duration {
if (ES.IsCalendarUnit(unit)) {
throw new RangeErrorCtor(`a starting point is required for ${unit}s total`);
}
const duration = ES.NormalizeDurationWith24HourDays(this);
return ES.TotalTimeDuration(duration.norm, unit);
const duration = ES.ToInternalDurationRecordWith24HourDays(this);
return ES.TotalTimeDuration(duration.time, unit);
}
toString(options = undefined) {
if (!ES.IsTemporalDuration(this)) throw new TypeErrorCtor('invalid receiver');
Expand All @@ -349,9 +349,12 @@ export class Duration {
if (unit === 'nanosecond' && increment === 1) return ES.TemporalDurationToString(this, precision);

const largestUnit = ES.DefaultTemporalLargestUnit(this);
let duration = ES.NormalizeDuration(this);
let duration = ES.ToInternalDurationRecord(this);
duration = ES.RoundTimeDuration(duration, increment, unit, roundingMode);
const roundedDuration = ES.UnnormalizeDuration(duration, ES.LargerOfTwoTemporalUnits(largestUnit, 'second'));
const roundedDuration = ES.TemporalDurationFromInternal(
duration,
ES.LargerOfTwoTemporalUnits(largestUnit, 'second')
);
return ES.TemporalDurationToString(roundedDuration, precision);
}
toJSON() {
Expand Down Expand Up @@ -395,8 +398,8 @@ export class Duration {

const largestUnit1 = ES.DefaultTemporalLargestUnit(one);
const largestUnit2 = ES.DefaultTemporalLargestUnit(two);
const duration1 = ES.NormalizeDuration(one);
const duration2 = ES.NormalizeDuration(two);
const duration1 = ES.ToInternalDurationRecord(one);
const duration2 = ES.ToInternalDurationRecord(two);

if (
zonedRelativeTo &&
Expand All @@ -417,12 +420,12 @@ export class Duration {
if (!plainRelativeTo) {
throw new RangeErrorCtor('A starting point is required for years, months, or weeks comparison');
}
d1 = ES.UnbalanceDateDurationRelative(duration1.date, plainRelativeTo);
d2 = ES.UnbalanceDateDurationRelative(duration2.date, plainRelativeTo);
d1 = ES.DateDurationDays(duration1.date, plainRelativeTo);
d2 = ES.DateDurationDays(duration2.date, plainRelativeTo);
}
const norm1 = duration1.norm.add24HourDays(d1);
const norm2 = duration2.norm.add24HourDays(d2);
return norm1.cmp(norm2);
const timeDuration1 = duration1.time.add24HourDays(d1);
const timeDuration2 = duration2.time.add24HourDays(d2);
return timeDuration1.cmp(timeDuration2);
}
}

Expand Down
Loading
Loading