Skip to content

Commit

Permalink
Add getISOFields() method to calendar-dependent types
Browse files Browse the repository at this point in the history
Closes: #354
  • Loading branch information
ptomato committed May 11, 2020
1 parent d9ddea5 commit 84b55c3
Show file tree
Hide file tree
Showing 28 changed files with 536 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/date.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,17 @@ date = Temporal.Date.from('2006-08-24');
Object.assign({}, date).day // => undefined
Object.assign({}, date.getFields()).day // => 24
```

### date.**getISOFields()**(): { year: number, month: number, day: number }

**Returns:** a plain object with properties expressing `date` in the ISO 8601 calendar.

This method is mainly useful if you are implementing a custom calendar.
Most code will not need to use it.
Use `date.getFields()` instead.

Usage example:
```javascript
date = Temporal.Date.from('2006-08-24');
date.getISOFields().day // => 24
```
14 changes: 14 additions & 0 deletions docs/datetime.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,17 @@ dt = Temporal.DateTime.from('1995-12-07T03:24:30.000003500');
Object.assign({}, dt).day // => undefined
Object.assign({}, dt.getFields()).day // => 7
```

### datetime.**getISOFields()**(): { year: number, month: number, day: number, hour: number, minute: number, second: number, millisecond: number, microsecond: number, nanosecond: number }

**Returns:** a plain object with properties expressing `datetime` in the ISO 8601 calendar.

This method is mainly useful if you are implementing a custom calendar.
Most code will not need to use it.
Use `datetime.getFields()` instead.

Usage example:
```javascript
dt = Temporal.Date.from('1995-12-07T03:24:30.000003500');
date.getISOFields().day // => 7
```
14 changes: 14 additions & 0 deletions docs/monthday.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,17 @@ md = Temporal.MonthDay.from('08-24');
Object.assign({}, md).day // => undefined
Object.assign({}, md.getFields()).day // => 24
```

### monthDay.**getISOFields()**(): { month: number, day: number }

**Returns:** a plain object with properties expressing `monthDay` in the ISO 8601 calendar.

This method is mainly useful if you are implementing a custom calendar.
Most code will not need to use it.
Use `monthDay.getFields()` instead.

Usage example:
```javascript
md = Temporal.MonthDay.from('08-24');
md.getISOFields().day // => 24
```
14 changes: 14 additions & 0 deletions docs/yearmonth.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,17 @@ ym = Temporal.DateTime.from('2019-06');
Object.assign({}, ym).year // => undefined
Object.assign({}, ym.getFields()).year // => 2019
```

### yearMonth.**getISOFields()**(): { year: number, month: number }

**Returns:** a plain object with properties expressing `yearMonth` in the ISO 8601 calendar.

This method is mainly useful if you are implementing a custom calendar.
Most code will not need to use it.
Use `yearMonth.getFields()` instead.

Usage example:
```javascript
date = Temporal.YearMonth.from('2019-06');
date.getISOFields().year // => 2019
```
8 changes: 8 additions & 0 deletions polyfill/lib/date.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ export class Date {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return ES.ToRecord(this, [['day'], ['month'], ['year']]);
}
getISOFields() {
if (!ES.IsTemporalDate(this)) throw new TypeError('invalid receiver');
return {
year: GetSlot(this, ISO_YEAR),
month: GetSlot(this, ISO_MONTH),
day: GetSlot(this, ISO_DAY)
};
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let year, month, day;
Expand Down
14 changes: 14 additions & 0 deletions polyfill/lib/datetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,20 @@ export class DateTime {
['year']
]);
}
getISOFields() {
if (!ES.IsTemporalDateTime(this)) throw new TypeError('invalid receiver');
return {
year: GetSlot(this, ISO_YEAR),
month: GetSlot(this, ISO_MONTH),
day: GetSlot(this, ISO_DAY),
hour: GetSlot(this, HOUR),
minute: GetSlot(this, MINUTE),
second: GetSlot(this, SECOND),
millisecond: GetSlot(this, MILLISECOND),
microsecond: GetSlot(this, MICROSECOND),
nanosecond: GetSlot(this, NANOSECOND)
};
}

static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
Expand Down
7 changes: 7 additions & 0 deletions polyfill/lib/monthday.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ export class MonthDay {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
return ES.ToRecord(this, [['day'], ['month']]);
}
getISOFields() {
if (!ES.IsTemporalMonthDay(this)) throw new TypeError('invalid receiver');
return {
month: GetSlot(this, ISO_MONTH),
day: GetSlot(this, ISO_DAY)
};
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let month, day;
Expand Down
7 changes: 7 additions & 0 deletions polyfill/lib/yearmonth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ export class YearMonth {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
return ES.ToRecord(this, [['month'], ['year']]);
}
getISOFields() {
if (!ES.IsTemporalYearMonth(this)) throw new TypeError('invalid receiver');
return {
year: GetSlot(this, ISO_YEAR),
month: GetSlot(this, ISO_MONTH)
};
}
static from(item, options = undefined) {
const disambiguation = ES.ToTemporalDisambiguation(options);
let year, month;
Expand Down
16 changes: 16 additions & 0 deletions polyfill/test/Date/prototype/getISOFields/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

const getISOFields = Temporal.Date.prototype.getISOFields;

assert.sameValue(typeof getISOFields, "function");

assert.throws(TypeError, () => getISOFields.call(undefined), "undefined");
assert.throws(TypeError, () => getISOFields.call(null), "null");
assert.throws(TypeError, () => getISOFields.call(true), "true");
assert.throws(TypeError, () => getISOFields.call(""), "empty string");
assert.throws(TypeError, () => getISOFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => getISOFields.call(1), "1");
assert.throws(TypeError, () => getISOFields.call({}), "plain object");
assert.throws(TypeError, () => getISOFields.call(Temporal.Date), "Temporal.Date");
assert.throws(TypeError, () => getISOFields.call(Temporal.Date.prototype), "Temporal.Date.prototype");
18 changes: 18 additions & 0 deletions polyfill/test/Date/prototype/getISOFields/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
includes: [propertyHelper.js]
---*/

assert.sameValue(
typeof Temporal.Date.prototype.getISOFields,
"function",
"`typeof Date.prototype.getISOFields` is `function`"
);

verifyProperty(Temporal.Date.prototype, "getISOFields", {
writable: true,
enumerable: false,
configurable: true,
});
31 changes: 31 additions & 0 deletions polyfill/test/Date/prototype/getISOFields/subclass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.date.prototype.getisofields
includes: [compareArray.js]
---*/

let called = 0;

const constructorArguments = [
[2000, 5, 2]
];

class MyDate extends Temporal.Date {
constructor(year, month, day) {
assert.compareArray([year, month, day], constructorArguments.shift(), "constructor arguments");
++called;
super(year, month, day);
}
}

const instance = MyDate.from("2000-05-02");
assert.sameValue(called, 1);

const result = instance.getISOFields();
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(called, 1);
assert.sameValue(Object.getPrototypeOf(result), Object.prototype);
16 changes: 16 additions & 0 deletions polyfill/test/DateTime/prototype/getISOFields/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

const getISOFields = Temporal.DateTime.prototype.getISOFields;

assert.sameValue(typeof getISOFields, "function");

assert.throws(TypeError, () => getISOFields.call(undefined), "undefined");
assert.throws(TypeError, () => getISOFields.call(null), "null");
assert.throws(TypeError, () => getISOFields.call(true), "true");
assert.throws(TypeError, () => getISOFields.call(""), "empty string");
assert.throws(TypeError, () => getISOFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => getISOFields.call(1), "1");
assert.throws(TypeError, () => getISOFields.call({}), "plain object");
assert.throws(TypeError, () => getISOFields.call(Temporal.DateTime), "Temporal.DateTime");
assert.throws(TypeError, () => getISOFields.call(Temporal.DateTime.prototype), "Temporal.DateTime.prototype");
18 changes: 18 additions & 0 deletions polyfill/test/DateTime/prototype/getISOFields/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
includes: [propertyHelper.js]
---*/

assert.sameValue(
typeof Temporal.DateTime.prototype.getISOFields,
"function",
"`typeof DateTime.prototype.getISOFields` is `function`"
);

verifyProperty(Temporal.DateTime.prototype, "getISOFields", {
writable: true,
enumerable: false,
configurable: true,
});
37 changes: 37 additions & 0 deletions polyfill/test/DateTime/prototype/getISOFields/subclass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.datetime.prototype.getisofields
includes: [compareArray.js]
---*/

let called = 0;

const constructorArguments = [
[2000, 5, 2, 12, 34, 56, 987, 654, 321]
];

class MyDateTime extends Temporal.DateTime {
constructor(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond) {
assert.compareArray([year, month, day, hour, minute, second, millisecond, microsecond, nanosecond], constructorArguments.shift(), "constructor arguments");
++called;
super(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond);
}
}

const instance = MyDateTime.from("2000-05-02T12:34:56.987654321");
assert.sameValue(called, 1);

const result = instance.getISOFields();
assert.sameValue(result.year, 2000, "year result");
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(result.hour, 12, "hour result");
assert.sameValue(result.minute, 34, "minute result");
assert.sameValue(result.second, 56, "second result");
assert.sameValue(result.millisecond, 987, "millisecond result");
assert.sameValue(result.microsecond, 654, "microsecond result");
assert.sameValue(result.nanosecond, 321, "nanosecond result");
assert.sameValue(called, 1);
assert.sameValue(Object.getPrototypeOf(result), Object.prototype);
16 changes: 16 additions & 0 deletions polyfill/test/MonthDay/prototype/getISOFields/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

const getISOFields = Temporal.MonthDay.prototype.getISOFields;

assert.sameValue(typeof getISOFields, "function");

assert.throws(TypeError, () => getISOFields.call(undefined), "undefined");
assert.throws(TypeError, () => getISOFields.call(null), "null");
assert.throws(TypeError, () => getISOFields.call(true), "true");
assert.throws(TypeError, () => getISOFields.call(""), "empty string");
assert.throws(TypeError, () => getISOFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => getISOFields.call(1), "1");
assert.throws(TypeError, () => getISOFields.call({}), "plain object");
assert.throws(TypeError, () => getISOFields.call(Temporal.MonthDay), "Temporal.MonthDay");
assert.throws(TypeError, () => getISOFields.call(Temporal.MonthDay.prototype), "Temporal.MonthDay.prototype");
18 changes: 18 additions & 0 deletions polyfill/test/MonthDay/prototype/getISOFields/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
includes: [propertyHelper.js]
---*/

assert.sameValue(
typeof Temporal.MonthDay.prototype.getISOFields,
"function",
"`typeof MonthDay.prototype.getISOFields` is `function`"
);

verifyProperty(Temporal.MonthDay.prototype, "getISOFields", {
writable: true,
enumerable: false,
configurable: true,
});
30 changes: 30 additions & 0 deletions polyfill/test/MonthDay/prototype/getISOFields/subclass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.monthday.prototype.getisofields
includes: [compareArray.js]
---*/

let called = 0;

const constructorArguments = [
[5, 2]
];

class MyMonthDay extends Temporal.MonthDay {
constructor(month, day) {
assert.compareArray([month, day], constructorArguments.shift(), "constructor arguments");
++called;
super(month, day);
}
}

const instance = MyMonthDay.from("05-02");
assert.sameValue(called, 1);

const result = instance.getISOFields();
assert.sameValue(result.month, 5, "month result");
assert.sameValue(result.day, 2, "day result");
assert.sameValue(called, 1);
assert.sameValue(Object.getPrototypeOf(result), Object.prototype);
16 changes: 16 additions & 0 deletions polyfill/test/YearMonth/prototype/getISOFields/branding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

const getISOFields = Temporal.YearMonth.prototype.getISOFields;

assert.sameValue(typeof getISOFields, "function");

assert.throws(TypeError, () => getISOFields.call(undefined), "undefined");
assert.throws(TypeError, () => getISOFields.call(null), "null");
assert.throws(TypeError, () => getISOFields.call(true), "true");
assert.throws(TypeError, () => getISOFields.call(""), "empty string");
assert.throws(TypeError, () => getISOFields.call(Symbol()), "symbol");
assert.throws(TypeError, () => getISOFields.call(1), "1");
assert.throws(TypeError, () => getISOFields.call({}), "plain object");
assert.throws(TypeError, () => getISOFields.call(Temporal.YearMonth), "Temporal.YearMonth");
assert.throws(TypeError, () => getISOFields.call(Temporal.YearMonth.prototype), "Temporal.YearMonth.prototype");
18 changes: 18 additions & 0 deletions polyfill/test/YearMonth/prototype/getISOFields/prop-desc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (C) 2020 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
includes: [propertyHelper.js]
---*/

assert.sameValue(
typeof Temporal.YearMonth.prototype.getISOFields,
"function",
"`typeof YearMonth.prototype.getISOFields` is `function`"
);

verifyProperty(Temporal.YearMonth.prototype, "getISOFields", {
writable: true,
enumerable: false,
configurable: true,
});
Loading

0 comments on commit 84b55c3

Please sign in to comment.