Skip to content

Commit 33f33eb

Browse files
committed
Allow calendar.fields() to accept an iterable
Temporal will only ever call the fields() method with an array, but other callers may want to supply a different iterable (such as a Set). See: #1427
1 parent 64440b8 commit 33f33eb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ export namespace Temporal {
686686
| /** @deprecated */ 'day'
687687
>
688688
): Temporal.Duration;
689-
fields?(fields: Array<string>): Iterable<string>;
689+
fields?(fields: Iterable<string>): Iterable<string>;
690690
mergeFields?(fields: Record<string, unknown>, additionalFields: Record<string, unknown>): Record<string, unknown>;
691691
toString(): string;
692692
}
@@ -769,7 +769,7 @@ export namespace Temporal {
769769
| /** @deprecated */ 'day'
770770
>
771771
): Temporal.Duration;
772-
fields(fields: Array<string>): Iterable<string>;
772+
fields(fields: Iterable<string>): Iterable<string>;
773773
mergeFields(fields: Record<string, unknown>, additionalFields: Record<string, unknown>): Record<string, unknown>;
774774
toString(): string;
775775
}

lib/calendar.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsiccl
55
import { CALENDAR_ID, ISO_YEAR, ISO_MONTH, ISO_DAY, CreateSlots, GetSlot, HasSlot, SetSlot } from './slots.mjs';
66

77
const ArrayIncludes = Array.prototype.includes;
8+
const ArrayPrototypePush = Array.prototype.push;
89
const ObjectAssign = Object.assign;
910

1011
const impl = {};
@@ -54,8 +55,12 @@ export class Calendar {
5455
}
5556
fields(fields) {
5657
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
57-
fields = ES.CreateListFromArrayLike(fields, ['String']);
58-
return impl[GetSlot(this, CALENDAR_ID)].fields(fields);
58+
const fieldsArray = [];
59+
for (const name of fields) {
60+
if (ES.Type(name) !== 'String') throw new TypeError('invalid fields');
61+
ArrayPrototypePush.call(fieldsArray, name);
62+
}
63+
return impl[GetSlot(this, CALENDAR_ID)].fields(fieldsArray);
5964
}
6065
mergeFields(fields, additionalFields) {
6166
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');

0 commit comments

Comments
 (0)