MCPcopy Index your code
hub / github.com/freeCodeCamp/freeCodeCamp / serializeDates

Function serializeDates

api/vitest.utils.ts:286–319  ·  view source on GitHub ↗
(data: T)

Source from the content-addressed store, hash-verified

284 * Recursively finds and converts Date objects to ISO strings while preserving shape.
285 */
286export function serializeDates<T>(data: T): ReplaceDates<T> {
287 if (data === null || data === undefined) {
288 return data as ReplaceDates<T>;
289 }
290
291 // Preserve Vitest/Jest asymmetric matchers (e.g., expect.any(Number))
292 if (
293 typeof data === 'object' &&
294 data !== null &&
295 typeof (data as { asymmetricMatch?: unknown }).asymmetricMatch ===
296 'function'
297 ) {
298 return data as unknown as ReplaceDates<T>;
299 }
300
301 if (data instanceof Date) {
302 return data.toISOString() as ReplaceDates<T>;
303 }
304
305 if (Array.isArray(data)) {
306 return (data as unknown[]).map(item =>
307 serializeDates(item)
308 ) as ReplaceDates<T>;
309 }
310
311 if (typeof data === 'object') {
312 const entries = Object.entries(data as Record<string, unknown>).map(
313 ([key, value]) => [key, serializeDates(value)] as const
314 );
315 return Object.fromEntries(entries) as ReplaceDates<T>;
316 }
317
318 return data as ReplaceDates<T>;
319}

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected