Skip to content

Commit

Permalink
PlainMonthDay({month: Infinity, day}) handling
Browse files Browse the repository at this point in the history
The previous commit failed the`PlainMonthDay({month: Infinity, day})`
test because 'MInfinity' is obviously invalid. This commit adds a Symbol
that can signal to the ISO calendar implementation that both  `calendar`
and `monthCode` were omitted from the inputs. This differentiates from
the case where the calendar is explicitly ISO (not omitted) because in
that case `monthCode` is still required.
  • Loading branch information
justingrant committed Feb 7, 2021
1 parent dfe9e7e commit dd913bd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ impl['iso8601'] = {
if (fields.month !== undefined && fields.year === undefined && fields.monthCode === undefined) {
throw new TypeError('either year or monthCode required with month');
}
// If the user omits the calendar from `from`, then it's clearly not
// intended to be cross-calendar code because the user is unambiguously
// asking for the ISO calendar. Therefore there's no requirement for
// `monthCode` to be present. ES.UndefinedMonthCode is a symbol that is only
// passed in the "omits the calendar and omits monthCode" case. This is
// needed to differentiate from the case where the user specified the ISO
// calendar explicitly. In that case, `monthCode` is required and
// `undefined` will throw above.
if (fields.monthCode === ES.UndefinedMonthCode) fields.monthCode = undefined;
fields = resolveNonLunisolarMonth(fields);
let { month, day } = fields;
({ month, day } = ES.RegulateMonthDay(month, day, overflow));
Expand Down
7 changes: 4 additions & 3 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,11 @@ export const ES = ObjectAssign({}, ES2020, {
for (const fieldRecord of fields) {
const [property, defaultValue] = fieldRecord;
let value = bag[property];
if (value === undefined) {
if (value === undefined || value === ES.UndefinedMonthCode) {
if (fieldRecord.length === 1) {
throw new TypeError(`required property '${property}' missing or undefined`);
}
value = defaultValue;
if (value === undefined) value = defaultValue;
} else {
any = true;
if (BUILTIN_CASTS.has(property)) {
Expand Down Expand Up @@ -1270,6 +1270,7 @@ export const ES = ObjectAssign({}, ES2020, {
if (!ES.IsTemporalInstant(result)) throw new TypeError('invalid result');
return result;
},
UndefinedMonthCode: Symbol('undefined-month-code'),
ToTemporalMonthDay: (item, constructor, options = {}) => {
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalMonthDay(item)) return item;
Expand All @@ -1280,7 +1281,7 @@ export const ES = ObjectAssign({}, ES2020, {
const fieldNames = ES.CalendarFields(calendar, ['day', 'month', 'monthCode', 'year']);
const fields = ES.ToTemporalMonthDayFields(item, fieldNames);
if (calendarAbsent && fields.month !== undefined && fields.monthCode === undefined) {
fields.monthCode = `M${ES.ToString(fields.month)}`;
fields.monthCode = ES.UndefinedMonthCode;
}
return ES.MonthDayFromFields(calendar, fields, constructor, options);
}
Expand Down
3 changes: 3 additions & 0 deletions polyfill/test/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,9 @@ describe('Intl', () => {
if (test.monthCode === undefined) test.monthCode = `M${test.month}`;
const { calendar, monthCode, month, day, year, isoReferenceYear } = test;
const md = Temporal.PlainMonthDay.from({ year, month, day, calendar });
const isoString = md.toString();
const mdFromIso = Temporal.PlainMonthDay.from(isoString);
equal(mdFromIso, md);
const isoFields = md.getISOFields();
equal(md.monthCode, monthCode);
equal(md.day, day);
Expand Down

0 comments on commit dd913bd

Please sign in to comment.