Skip to content

Commit 1060bea

Browse files
fix: consider valueOf in the reactive methods of SvelteDate (#14227)
1 parent f0c2d4c commit 1060bea

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.changeset/cyan-cooks-nail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: consider `valueOf` in the reactive methods of `SvelteDate`

packages/svelte/src/reactivity/date.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class SvelteDate extends Date {
3030
);
3131

3232
for (const method of methods) {
33-
if (method.startsWith('get') || method.startsWith('to')) {
33+
if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') {
3434
// @ts-ignore
3535
proto[method] = function (...args) {
3636
// don't memoize if there are arguments

packages/svelte/src/reactivity/date.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,30 @@ test('Date.toLocaleString', () => {
588588
cleanup();
589589
});
590590

591+
test('Date.valueOf', () => {
592+
const date = new SvelteDate(initial_date);
593+
594+
const log: any = [];
595+
596+
const cleanup = effect_root(() => {
597+
render_effect(() => {
598+
log.push(date.valueOf());
599+
});
600+
});
601+
602+
flushSync();
603+
604+
assert.deepEqual(log, [initial_date.valueOf()]);
605+
606+
flushSync(() => {
607+
date.setTime(date.getTime() + 10);
608+
});
609+
610+
assert.deepEqual(log, [initial_date.valueOf(), new Date(initial_date.getTime() + 10).valueOf()]);
611+
612+
cleanup();
613+
});
614+
591615
test('Date.instanceOf', () => {
592616
assert.equal(new SvelteDate() instanceof Date, true);
593617
});

0 commit comments

Comments
 (0)