diff --git a/packages/shared/src/utils/compress.test.ts b/packages/shared/src/utils/compress.test.ts index ae6a8d6e41..ae84813a37 100644 --- a/packages/shared/src/utils/compress.test.ts +++ b/packages/shared/src/utils/compress.test.ts @@ -36,6 +36,14 @@ const geoJSON = { ], }; +const dateString = '2022-02-14T01:02:11.000Z'; +const dateInput = { + date_created: new Date(dateString), +}; +const dateOutput = { + date_created: dateString, +}; + describe('compress', () => { test('Compresses plain objects', () => { expect(compress(plain)).toBe( @@ -61,6 +69,10 @@ describe('compress', () => { ); }); + test('Compresses Date objects into strings', () => { + expect(compress(dateInput)).toBe('date_created|2022-02-14T01:02:11.000Z^^^$0|1]'); + }); + test('Throws error on non-supported types', () => { expect(() => compress({ method: () => true })).toThrowError(); }); @@ -99,6 +111,10 @@ describe('decompress', () => { ).toEqual(geoJSON); }); + test('Decompresses Date strings', () => { + expect(decompress('date_created|2022-02-14T01:02:11.000Z^^^$0|1]')).toEqual(dateOutput); + }); + test('Errors when not enough parts exist', () => { expect(() => decompress('a|b^1K6^')).toThrowError(); }); diff --git a/packages/shared/src/utils/compress.ts b/packages/shared/src/utils/compress.ts index dd99f14484..a284c6cfbd 100644 --- a/packages/shared/src/utils/compress.ts +++ b/packages/shared/src/utils/compress.ts @@ -51,6 +51,26 @@ export function compress(obj: Record | Record[]) { return ['@', ...part.map((subPart) => getAst(subPart))]; } + if (part instanceof Date) { + const value = encode(part.toJSON()); + + if (strings.has(value)) { + return { + type: Types.STRING, + index: strings.get(value)!, + }; + } + + const index = strings.size; + + strings.set(value, index); + + return { + type: Types.STRING, + index, + }; + } + if (typeof part === 'object') { return [ '$',