-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Date field serialization Fixes #14 * Add links to JSON stringify and parse * Prove that the original object is not modified
- Loading branch information
Showing
5 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { deepReplaceDatesWithISOStrings } from './deep-replace-dates-with-strings'; | ||
|
||
describe('deepReplaceDatesWithISOStrings', () => { | ||
it('should replace Date objects with their ISO string representation', () => { | ||
const date1 = new Date(); | ||
const date2 = new Date(); | ||
const date3 = new Date(); | ||
|
||
const obj = { | ||
name: 'John', | ||
created: date1, | ||
friends: [ | ||
{ | ||
name: 'Jane', | ||
created: date2, | ||
}, | ||
], | ||
latestLog: { | ||
time: date3, | ||
message: 'Hello, world!', | ||
}, | ||
}; | ||
|
||
const result = deepReplaceDatesWithISOStrings(obj); | ||
|
||
expect(result.created).toBe(date1.toISOString()); | ||
expect(result.friends[0].created).toBe(date2.toISOString()); | ||
expect(result.latestLog.time).toBe(date3.toISOString()); | ||
expect(result).toEqual({ | ||
name: 'John', | ||
created: date1.toISOString(), | ||
friends: [ | ||
{ | ||
name: 'Jane', | ||
created: date2.toISOString(), | ||
}, | ||
], | ||
latestLog: { | ||
time: date3.toISOString(), | ||
message: 'Hello, world!', | ||
}, | ||
}); | ||
}); | ||
|
||
// Check that original object is unmodified | ||
it('should not modify the original object', () => { | ||
const date = new Date(); | ||
const obj = { | ||
name: 'John', | ||
created: date, | ||
}; | ||
|
||
deepReplaceDatesWithISOStrings(obj); | ||
|
||
expect(obj.created).toBe(date); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Deep clones the object and replaces all Date objects with their ISO string representation. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function deepReplaceDatesWithISOStrings(obj: any): any { | ||
if (obj instanceof Date) { | ||
return obj.toISOString(); | ||
} else if (Array.isArray(obj)) { | ||
return obj.map(deepReplaceDatesWithISOStrings); | ||
} else if (typeof obj === 'object' && obj !== null) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const result: any = {}; | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
result[key] = deepReplaceDatesWithISOStrings(obj[key]); | ||
} | ||
} | ||
return result; | ||
} else { | ||
return obj; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters