core-services-sdk 1.3.33 → 1.3.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "core-services-sdk",
3
- "version": "1.3.33",
3
+ "version": "1.3.34",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "types": "types/index.d.ts",
@@ -97,3 +97,22 @@ export function toMongo(query = {}) {
97
97
  }),
98
98
  )
99
99
  }
100
+
101
+ export function castIsoDates(obj) {
102
+ if (Array.isArray(obj)) {
103
+ return obj.map(castIsoDates)
104
+ }
105
+ if (obj && typeof obj === 'object') {
106
+ for (const [key, value] of Object.entries(obj)) {
107
+ obj[key] = castIsoDates(value)
108
+ }
109
+ return obj
110
+ }
111
+ if (typeof obj === 'string' && /^\d{4}-\d{2}-\d{2}T/.test(obj)) {
112
+ const d = new Date(obj)
113
+ if (!isNaN(d)) {
114
+ return d
115
+ }
116
+ }
117
+ return obj
118
+ }
@@ -0,0 +1,67 @@
1
+ import { describe, it, expect } from 'vitest'
2
+
3
+ import { castIsoDates } from '../../src/mongodb/dsl-to-mongo.js'
4
+
5
+ describe('castIsoDates', () => {
6
+ it('should convert ISO date strings to Date objects', () => {
7
+ const input = {
8
+ createdAt: {
9
+ $gte: '2025-10-08T00:00:00.000Z',
10
+ $lte: '2025-10-09T23:59:59.999Z',
11
+ },
12
+ }
13
+
14
+ const output = castIsoDates(structuredClone(input))
15
+
16
+ expect(output.createdAt.$gte).toBeInstanceOf(Date)
17
+ expect(output.createdAt.$lte).toBeInstanceOf(Date)
18
+ expect(output.createdAt.$gte.toISOString()).toBe('2025-10-08T00:00:00.000Z')
19
+ expect(output.createdAt.$lte.toISOString()).toBe('2025-10-09T23:59:59.999Z')
20
+ })
21
+
22
+ it('should recursively convert nested ISO strings', () => {
23
+ const input = {
24
+ filters: [
25
+ { time: '2025-09-01T12:00:00.000Z' },
26
+ { inner: { deep: '2025-09-02T00:00:00.000Z' } },
27
+ ],
28
+ }
29
+
30
+ const output = castIsoDates(structuredClone(input))
31
+
32
+ expect(output.filters[0].time).toBeInstanceOf(Date)
33
+ expect(output.filters[1].inner.deep).toBeInstanceOf(Date)
34
+ })
35
+
36
+ it('should not modify non-date strings or numbers', () => {
37
+ const input = {
38
+ name: 'hello',
39
+ value: 123,
40
+ notIso: '2025-09-01', // missing 'T' — not ISO format
41
+ }
42
+
43
+ const output = castIsoDates(structuredClone(input))
44
+
45
+ expect(output.name).toBe('hello')
46
+ expect(output.value).toBe(123)
47
+ expect(output.notIso).toBe('2025-09-01')
48
+ })
49
+
50
+ it('should handle arrays directly', () => {
51
+ const input = ['2025-10-01T10:00:00.000Z', 'not-a-date', 5]
52
+
53
+ const output = castIsoDates(structuredClone(input))
54
+
55
+ expect(output[0]).toBeInstanceOf(Date)
56
+ expect(output[1]).toBe('not-a-date')
57
+ expect(output[2]).toBe(5)
58
+ })
59
+
60
+ it('should leave null and undefined unchanged', () => {
61
+ const input = { a: null, b: undefined }
62
+ const output = castIsoDates(structuredClone(input))
63
+
64
+ expect(output.a).toBeNull()
65
+ expect(output.b).toBeUndefined()
66
+ })
67
+ })