Skip to content
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ tsc-out/
.vscode/*
!.vscode/launch.json
*.tgz
.DS_Store
5 changes: 2 additions & 3 deletions lib/plaindate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as ES from './ecmascript';
import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass';
import { MakeIntrinsicClass } from './intrinsicclass';
import {
ISO_YEAR,
ISO_MONTH,
Expand All @@ -17,6 +17,7 @@ import {
import { Temporal } from '..';
import { DateTimeFormat } from './intl';
import type { PlainDateParams as Params, PlainDateReturn as Return } from './internaltypes';
import { Duration } from './duration';

const DISALLOWED_UNITS = ['hour', 'minute', 'second', 'millisecond', 'microsecond', 'nanosecond'] as const;

Expand Down Expand Up @@ -188,7 +189,6 @@ export class PlainDate implements Temporal.PlainDate {
this
));

const Duration = GetIntrinsic('%Temporal.Duration%');
return new Duration(years, months, weeks, days, 0, 0, 0, 0, 0, 0);
}
since(otherParam: Params['since'][0], optionsParam: Params['since'][1] = undefined): Return['since'] {
Expand All @@ -212,7 +212,6 @@ export class PlainDate implements Temporal.PlainDate {

const untilOptions = { ...options, largestUnit };
let { years, months, weeks, days } = ES.CalendarDateUntil(calendar, this, other, untilOptions);
const Duration = GetIntrinsic('%Temporal.Duration%');
if (smallestUnit === 'day' && roundingIncrement === 1) {
return new Duration(-years, -months, -weeks, -days, 0, 0, 0, 0, 0, 0);
}
Expand Down
3 changes: 3 additions & 0 deletions test/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import './calendar.mjs';
import './usertimezone.mjs';
import './usercalendar.mjs';

// monkeypatching
import './monkeypatch.mjs';

Promise.resolve()
.then(() => {
return Demitasse.report(Pretty.reporter);
Expand Down
28 changes: 28 additions & 0 deletions test/monkeypatch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env -S node --experimental-modules

import Demitasse from '@pipobscure/demitasse';
const { describe, it, report } = Demitasse;

import Pretty from '@pipobscure/demitasse-pretty';
const { reporter } = Pretty;

import { strict as assert } from 'assert';
const { equal } = assert;

import * as Temporal from '@js-temporal/polyfill';
const { PlainDate } = Temporal;

describe('Monkeypatch', () => {
describe('PlainDate', () => {
it("Monkeypatching Duration doesn't affect PlainDate", () => {
Temporal.Duration.prototype.constructor = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we also want to check Temporal.Duration = null and delete Temporal.Duration

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the tests are .mjs, imports will be read-only, so JS won't let us do those two things (although monkeypatching the prototype seems to be valid).

const date = PlainDate.from('2021-12-09');
equal(`${date.until('2021-12-10')}`, 'P1D');
});
});
});

import { normalize } from 'path';
if (normalize(import.meta.url.slice(8)) === normalize(process.argv[1])) {
report(reporter).then((failed) => process.exit(failed ? 1 : 0));
}