fix: correctly handle SvelteDate methods with arguments

pull/12738/head
Dominic Gannaway 2 years ago
parent e66416bec7
commit ca9a5c611e

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: correctly handle SvelteDate methods with arguments

@ -32,7 +32,9 @@ export class SvelteDate extends Date {
if (method.startsWith('get') || method.startsWith('to')) {
// @ts-ignore
proto[method] = function (...args) {
var d = this.#deriveds.get(method);
// @ts-ignore
var can_cache = args.length === 0;
var d = can_cache ? this.#deriveds.get(method) : undefined;
if (d === undefined) {
d = derived(() => {
@ -41,7 +43,9 @@ export class SvelteDate extends Date {
return date_proto[method].apply(this, args);
});
this.#deriveds.set(method, d);
if (can_cache) {
this.#deriveds.set(method, d);
}
}
return get(d);

@ -555,6 +555,30 @@ test('Date fine grained tests', () => {
cleanup();
});
test('Datae.toLocaleString', () => {
const date = new SvelteDate(initial_date);
const log: any = [];
const cleanup = effect_root(() => {
render_effect(() => {
log.push(date.toLocaleString(undefined, { month: 'long', year: 'numeric' }));
});
render_effect(() => {
log.push(date.toLocaleString(undefined, { month: 'long' }));
});
});
flushSync();
assert.deepEqual(log, [
initial_date.toLocaleString(undefined, { month: 'long', year: 'numeric' }),
initial_date.toLocaleString(undefined, { month: 'long' })
]);
cleanup();
});
test('Date.instanceOf', () => {
assert.equal(new SvelteDate() instanceof Date, true);
});

Loading…
Cancel
Save