Skip to content

Commit

Permalink
Update polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Aditi-1400 committed May 17, 2023
1 parent 78d6dfc commit 174fa2c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
24 changes: 19 additions & 5 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import * as ES from './ecmascript.mjs';
import { GetIntrinsic, MakeIntrinsicClass, DefineIntrinsic } from './intrinsicclass.mjs';
import ToString from 'es-abstract/2022/ToString.js';
import ToIntegerOrInfinity from 'es-abstract/2022/ToIntegerOrInfinity.js';
import {
CALENDAR_ID,
ISO_YEAR,
Expand Down Expand Up @@ -306,11 +308,10 @@ impl['iso8601'] = {
if (fields.month !== undefined && fields.year === undefined && fields.monthCode === undefined) {
throw new TypeError('either year or monthCode required with month');
}
const useYear = fields.monthCode === undefined;
const referenceISOYear = 1972;
fields = resolveNonLunisolarMonth(fields);
let { month, day, year } = fields;
({ month, day } = ES.RegulateISODate(useYear ? year : referenceISOYear, month, day, overflow));
({ month, day } = ES.RegulateISODate(year !== undefined ? year : referenceISOYear, month, day, overflow));
return ES.CreateTemporalMonthDay(month, day, calendarSlotValue, referenceISOYear);
},
fields(fields) {
Expand Down Expand Up @@ -1921,10 +1922,21 @@ const helperDangi = ObjectAssign({}, { ...helperChinese, id: 'dangi' });
* ISO and non-ISO implementations vs. code that was very different.
*/
const nonIsoGeneralImpl = {
CalendarFieldDescriptors(type) {
let fieldDescriptors = [];
if (type !== 'month-day') {
fieldDescriptors = [
{ property: 'era', conversion: ToString, required: false },
{ property: 'eraYear', conversion: ToIntegerOrInfinity, required: false }
];
}
return fieldDescriptors;
},
dateFromFields(fields, options, calendarSlotValue) {
const cache = new OneObjectCache();
const fieldNames = this.fields(['day', 'month', 'monthCode', 'year']);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);
const extraFieldDescriptors = this.CalendarFieldDescriptors('date');
fields = ES.PrepareTemporalFields(fields, fieldNames, [], '', extraFieldDescriptors);
const overflow = ES.ToTemporalOverflow(options);
const { year, month, day } = this.helper.calendarToIsoDate(fields, overflow, cache);
const result = ES.CreateTemporalDate(year, month, day, calendarSlotValue);
Expand All @@ -1934,7 +1946,8 @@ const nonIsoGeneralImpl = {
yearMonthFromFields(fields, options, calendarSlotValue) {
const cache = new OneObjectCache();
const fieldNames = this.fields(['month', 'monthCode', 'year']);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);
const extraFieldDescriptors = this.CalendarFieldDescriptors('year-month');
fields = ES.PrepareTemporalFields(fields, fieldNames, [], '', extraFieldDescriptors);
const overflow = ES.ToTemporalOverflow(options);
const { year, month, day } = this.helper.calendarToIsoDate({ ...fields, day: 1 }, overflow, cache);
const result = ES.CreateTemporalYearMonth(year, month, calendarSlotValue, /* referenceISODay = */ day);
Expand All @@ -1946,7 +1959,8 @@ const nonIsoGeneralImpl = {
// For lunisolar calendars, either `monthCode` or `year` must be provided
// because `month` is ambiguous without a year or a code.
const fieldNames = this.fields(['day', 'month', 'monthCode', 'year']);
fields = ES.PrepareTemporalFields(fields, fieldNames, []);
const extraFieldDescriptors = this.CalendarFieldDescriptors('date');
fields = ES.PrepareTemporalFields(fields, fieldNames, [], '', extraFieldDescriptors);
const overflow = ES.ToTemporalOverflow(options);
const { year, month, day } = this.helper.monthDayFromFields(fields, overflow, cache);
// `year` is a reference year where this month/day exists in this calendar
Expand Down
23 changes: 20 additions & 3 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const ArrayIncludes = Array.prototype.includes;
const ArrayPrototypePush = Array.prototype.push;
const ArrayPrototypeSort = Array.prototype.sort;
const ArrayPrototypeFind = Array.prototype.find;
const IntlDateTimeFormat = globalThis.Intl.DateTimeFormat;
const MathMin = Math.min;
const MathMax = Math.max;
Expand Down Expand Up @@ -164,8 +165,6 @@ const BUILTIN_CASTS = new Map([
['milliseconds', ToIntegerIfIntegral],
['microseconds', ToIntegerIfIntegral],
['nanoseconds', ToIntegerIfIntegral],
['era', ToString],
['eraYear', ToIntegerOrInfinity],
['offset', ToString]
]);

Expand Down Expand Up @@ -1060,10 +1059,20 @@ export function PrepareTemporalFields(
bag,
fields,
requiredFields,
{ emptySourceErrorMessage = 'no supported properties found' } = {}
{ emptySourceErrorMessage = 'no supported properties found' } = {},
extraFieldDescriptors = []
) {
const result = ObjectCreate(null);
let any = false;
if (extraFieldDescriptors) {
for (let index = 0; index < extraFieldDescriptors.length; index++) {
let desc = extraFieldDescriptors[index];
Call(ArrayPrototypePush, fields, [desc.property]);
if (desc.required === true && requiredFields !== 'partial') {
Call(ArrayPrototypePush, requiredFields, [desc.property]);
}
}
}
Call(ArrayPrototypeSort, fields, []);
for (let index = 0; index < fields.length; index++) {
const property = fields[index];
Expand All @@ -1072,6 +1081,14 @@ export function PrepareTemporalFields(
any = true;
if (BUILTIN_CASTS.has(property)) {
value = BUILTIN_CASTS.get(property)(value);
} else if (extraFieldDescriptors) {
const matchingDescriptor = Call(ArrayPrototypeFind, extraFieldDescriptors, [
(desc) => desc.property === property
]);
if (matchingDescriptor) {
const convertor = matchingDescriptor.conversion;
value = convertor(value);
}
}
result[property] = value;
} else if (requiredFields !== 'partial') {
Expand Down

0 comments on commit 174fa2c

Please sign in to comment.