Skip to content

Commit 9df5d06

Browse files
12wrigjaptomato
andauthored
Co-authored-by: Philip Chimento <[email protected]>
1 parent 068e801 commit 9df5d06

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

lib/duration.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ export class Duration implements Temporal.Duration {
3232
microsecondsParam: Params['constructor'][8] = 0,
3333
nanosecondsParam: Params['constructor'][9] = 0
3434
) {
35-
const years = ES.ToIntegerThrowOnInfinity(yearsParam);
36-
const months = ES.ToIntegerThrowOnInfinity(monthsParam);
37-
const weeks = ES.ToIntegerThrowOnInfinity(weeksParam);
38-
const days = ES.ToIntegerThrowOnInfinity(daysParam);
39-
const hours = ES.ToIntegerThrowOnInfinity(hoursParam);
40-
const minutes = ES.ToIntegerThrowOnInfinity(minutesParam);
41-
const seconds = ES.ToIntegerThrowOnInfinity(secondsParam);
42-
const milliseconds = ES.ToIntegerThrowOnInfinity(millisecondsParam);
43-
const microseconds = ES.ToIntegerThrowOnInfinity(microsecondsParam);
44-
const nanoseconds = ES.ToIntegerThrowOnInfinity(nanosecondsParam);
35+
const years = ES.ToIntegerWithoutRounding(yearsParam);
36+
const months = ES.ToIntegerWithoutRounding(monthsParam);
37+
const weeks = ES.ToIntegerWithoutRounding(weeksParam);
38+
const days = ES.ToIntegerWithoutRounding(daysParam);
39+
const hours = ES.ToIntegerWithoutRounding(hoursParam);
40+
const minutes = ES.ToIntegerWithoutRounding(minutesParam);
41+
const seconds = ES.ToIntegerWithoutRounding(secondsParam);
42+
const milliseconds = ES.ToIntegerWithoutRounding(millisecondsParam);
43+
const microseconds = ES.ToIntegerWithoutRounding(microsecondsParam);
44+
const nanoseconds = ES.ToIntegerWithoutRounding(nanosecondsParam);
4545

4646
const sign = ES.DurationSign(
4747
years,

lib/ecmascript.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,16 @@ export function ToPositiveInteger(valueParam: unknown, property?: string): numbe
140140
return value;
141141
}
142142

143-
function ToIntegerNoFraction(valueParam: unknown): number {
143+
export function ToIntegerWithoutRounding(valueParam: unknown): number {
144144
const value = ToNumber(valueParam);
145+
if (NumberIsNaN(value)) return 0;
146+
if (!NumberIsFinite(value)) {
147+
throw new RangeError('infinity is out of range');
148+
}
145149
if (!IsInteger(value)) {
146150
throw new RangeError(`unsupported fractional value ${value}`);
147151
}
148-
return value;
152+
return ToInteger(value); // ℝ(value) in spec text; converts -0 to 0
149153
}
150154

151155
const BUILTIN_CASTS = new Map<PrimitivePropertyNames, (v: unknown) => string | number>([
@@ -159,16 +163,16 @@ const BUILTIN_CASTS = new Map<PrimitivePropertyNames, (v: unknown) => string | n
159163
['millisecond', ToIntegerThrowOnInfinity],
160164
['microsecond', ToIntegerThrowOnInfinity],
161165
['nanosecond', ToIntegerThrowOnInfinity],
162-
['years', ToIntegerNoFraction],
163-
['months', ToIntegerNoFraction],
164-
['weeks', ToIntegerNoFraction],
165-
['days', ToIntegerNoFraction],
166-
['hours', ToIntegerNoFraction],
167-
['minutes', ToIntegerNoFraction],
168-
['seconds', ToIntegerNoFraction],
169-
['milliseconds', ToIntegerNoFraction],
170-
['microseconds', ToIntegerNoFraction],
171-
['nanoseconds', ToIntegerNoFraction],
166+
['years', ToIntegerWithoutRounding],
167+
['months', ToIntegerWithoutRounding],
168+
['weeks', ToIntegerWithoutRounding],
169+
['days', ToIntegerWithoutRounding],
170+
['hours', ToIntegerWithoutRounding],
171+
['minutes', ToIntegerWithoutRounding],
172+
['seconds', ToIntegerWithoutRounding],
173+
['milliseconds', ToIntegerWithoutRounding],
174+
['microseconds', ToIntegerWithoutRounding],
175+
['nanoseconds', ToIntegerWithoutRounding],
172176
['era', ToString],
173177
['eraYear', ToInteger],
174178
['offset', ToString]

test/duration.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ describe('Duration', () => {
118118
throws(() => new Duration(1, 1, 1, 1, 1, 1, 1, 1, -1, 1), RangeError);
119119
throws(() => new Duration(1, 1, 1, 1, 1, 1, 1, 1, 1, -1), RangeError);
120120
});
121+
it('fractional values throw', () => {
122+
throws(() => new Duration(1.1), RangeError);
123+
throws(() => new Duration(0, 1.1), RangeError);
124+
throws(() => new Duration(0, 0, 1.1), RangeError);
125+
throws(() => new Duration(0, 0, 0, 1.1), RangeError);
126+
throws(() => new Duration(0, 0, 0, 0, 1.1), RangeError);
127+
throws(() => new Duration(0, 0, 0, 0, 0, 1.1), RangeError);
128+
throws(() => new Duration(0, 0, 0, 0, 0, 0, 1.1), RangeError);
129+
throws(() => new Duration(0, 0, 0, 0, 0, 0, 0, 1.1), RangeError);
130+
throws(() => new Duration(0, 0, 0, 0, 0, 0, 0, 0, 1.1), RangeError);
131+
throws(() => new Duration(0, 0, 0, 0, 0, 0, 0, 0, 0, 1.1), RangeError);
132+
});
121133
});
122134
describe('from()', () => {
123135
it('Duration.from(P5Y) is not the same object', () => {

0 commit comments

Comments
 (0)