Skip to content

Commit e2aa13f

Browse files
authored
feat(connect): allow to pass date object (#647)
1 parent b83dc79 commit e2aa13f

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

libs/ngxtension/connect/src/connect.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ describe(connect.name, () => {
105105
expect(state()).toEqual(5);
106106
});
107107
});
108+
109+
it('should allow connecting a date object', () => {
110+
const state = signal(new Date('December 17, 1995 03:24:00'));
111+
112+
TestBed.runInInjectionContext(() => {
113+
const sourceSignalOne = signal(new Date());
114+
connect(state).with(() => sourceSignalOne());
115+
TestBed.flushEffects();
116+
expect(state()).toEqual(sourceSignalOne());
117+
});
118+
});
108119
});
109120

110121
describe('connects an observable to a signal in injection context', () => {
@@ -164,6 +175,11 @@ describe(connect.name, () => {
164175
component.objectSource$.next(1);
165176
expect(component.objectSignal()).toEqual(1);
166177

178+
const date = new Date('December 17, 1995 03:24:00');
179+
component.objectSource$.next({});
180+
component.objectSource$.next(date);
181+
expect(component.objectSignal()).toEqual(date);
182+
167183
component.objectSource$.next({});
168184
component.objectSource$.next(undefined);
169185
expect(component.objectSignal()).toEqual(undefined);

libs/ngxtension/connect/src/connect.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,19 @@ export function connect(signal: WritableSignal<unknown>, ...args: any[]) {
125125

126126
if (!isObject(x)) {
127127
const reducedValue = reducer ? reducer(prev, x) : x;
128+
128129
return isObject(reducedValue)
129130
? { ...prev, ...(reducedValue as object) }
130131
: reducedValue;
131132
}
132133

133-
return { ...prev, ...((reducer?.(prev, x) || x) as object) };
134+
const curr = reducer?.(prev, x) || x;
135+
136+
if (isDate(curr)) {
137+
return new Date(curr);
138+
}
139+
140+
return { ...prev, ...(curr as object) };
134141
});
135142
};
136143

@@ -151,11 +158,17 @@ export function connect(signal: WritableSignal<unknown>, ...args: any[]) {
151158
return effect(
152159
() => {
153160
signal.update((prev) => {
161+
const curr = originSignal();
162+
154163
if (!isObject(prev)) {
155-
return originSignal();
164+
return curr;
156165
}
157166

158-
return { ...prev, ...(originSignal() as object) };
167+
if (isDate(curr)) {
168+
return new Date(curr);
169+
}
170+
171+
return { ...prev, ...(curr as object) };
159172
});
160173
},
161174
{ injector },
@@ -270,6 +283,10 @@ function parseArgs(
270283
return [null, null, args[0] as Injector | DestroyRef, false, null];
271284
}
272285

286+
function isDate(val: any): val is Date {
287+
return val instanceof Date;
288+
}
289+
273290
function isObject(val: any): val is object {
274291
return (
275292
typeof val === 'object' &&

0 commit comments

Comments
 (0)