rajt 0.0.42 → 0.0.44
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 +1 -1
- package/src/dynamodb/compact.ts +24 -14
- package/src/dynamodb/schema.ts +33 -4
package/package.json
CHANGED
package/src/dynamodb/compact.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { SchemaStructure } from './types'
|
|
2
2
|
import getLength from '../utils/lenght'
|
|
3
|
+
import { isArraySchema } from './schema'
|
|
3
4
|
|
|
4
5
|
export default class Compact {
|
|
5
6
|
static #typeRegex: RegExp
|
|
@@ -13,19 +14,22 @@ export default class Compact {
|
|
|
13
14
|
'null': 'N',
|
|
14
15
|
// Array
|
|
15
16
|
'[]': 'A',
|
|
16
|
-
// Object
|
|
17
|
-
'{}': 'O',
|
|
18
|
-
// Commons
|
|
19
17
|
'["0"]': 'A0',
|
|
20
18
|
'["1"]': 'A1',
|
|
21
|
-
'["
|
|
22
|
-
'["
|
|
23
|
-
|
|
24
|
-
'
|
|
19
|
+
'["false"]': 'A2',
|
|
20
|
+
'["true"]': 'A3',
|
|
21
|
+
// Object
|
|
22
|
+
'{}': 'O',
|
|
23
|
+
// String
|
|
24
|
+
'""': 'S',
|
|
25
|
+
'"0"': 'S0',
|
|
26
|
+
'"1"': 'S1',
|
|
27
|
+
'"false"': 'S2',
|
|
28
|
+
'"true"': 'S3',
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
static {
|
|
28
|
-
this.#reverseTypeMap = Object.fromEntries(Object.entries(this.#typeMap).map(([k, v]) => [v, k]))
|
|
32
|
+
this.#reverseTypeMap = Object.fromEntries(Object.entries(this.#typeMap).map(([k, v]) => [v, k.replace(/"/g, "'")]))
|
|
29
33
|
this.#typeRegex = this.#mapRegex(Object.keys(this.#typeMap))
|
|
30
34
|
this.#reverseTypeRegex = this.#mapRegex(Object.keys(this.#reverseTypeMap))
|
|
31
35
|
}
|
|
@@ -46,7 +50,7 @@ export default class Compact {
|
|
|
46
50
|
if (!val) return val as T
|
|
47
51
|
|
|
48
52
|
if (Array.isArray(val))
|
|
49
|
-
return val.map((i: {
|
|
53
|
+
return val.map((i: {v: string}) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
|
|
50
54
|
|
|
51
55
|
return val?.V ? this.decode<T>(val.V, schema) : val
|
|
52
56
|
}
|
|
@@ -65,6 +69,9 @@ export default class Compact {
|
|
|
65
69
|
}
|
|
66
70
|
|
|
67
71
|
static zip(obj: any, schema: SchemaStructure, seen: any[]): any[] {
|
|
72
|
+
if (Array.isArray(obj))
|
|
73
|
+
return obj?.length ? obj.map(item => this.zip(item, schema, seen)) : []
|
|
74
|
+
|
|
68
75
|
if (!obj || [null, true, false].includes(obj)) return obj
|
|
69
76
|
|
|
70
77
|
return schema.map(key => {
|
|
@@ -82,16 +89,16 @@ export default class Compact {
|
|
|
82
89
|
})
|
|
83
90
|
}
|
|
84
91
|
|
|
85
|
-
static unzip(val: any, seen: any[] = []
|
|
92
|
+
static unzip(val: any, seen: any[] = []): any[] {
|
|
93
|
+
if (Array.isArray(val))
|
|
94
|
+
return val?.length ? val.map(item => this.unzip(item, seen)) : []
|
|
95
|
+
|
|
86
96
|
const type = typeof val
|
|
87
97
|
const length = getLength(val, type)
|
|
88
98
|
|
|
89
99
|
if ([null, true, false].includes(val) || type != 'object' && length < 2)
|
|
90
100
|
return val
|
|
91
101
|
|
|
92
|
-
if (Array.isArray(val))
|
|
93
|
-
return val.map(item => this.unzip(item, seen, deep))
|
|
94
|
-
|
|
95
102
|
if (type == 'object') {
|
|
96
103
|
for (const key in val)
|
|
97
104
|
val[key] = this.unzip(val[key], seen)
|
|
@@ -108,10 +115,13 @@ export default class Compact {
|
|
|
108
115
|
return val
|
|
109
116
|
}
|
|
110
117
|
|
|
111
|
-
static withSchema(value: any[], keys: any[]): any {
|
|
118
|
+
static withSchema(value: any[], keys: any[], deep = false): any {
|
|
112
119
|
if (!value || !Array.isArray(value))
|
|
113
120
|
return value
|
|
114
121
|
|
|
122
|
+
if (!deep && isArraySchema(keys))
|
|
123
|
+
return value?.length ? value.map(v => this.withSchema(v, keys, true)) : []
|
|
124
|
+
|
|
115
125
|
return Object.fromEntries(
|
|
116
126
|
keys.map((key, index) => this.entry(key, value[index])).filter(Boolean)
|
|
117
127
|
)
|
package/src/dynamodb/schema.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { z, ZodTypeAny } from 'zod'
|
|
2
2
|
import type { SchemaStructure } from './types'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const m = Symbol('a')
|
|
5
|
+
export function isArraySchema(v: any) : boolean {
|
|
6
|
+
return v[m] || false
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
5
10
|
if (schema instanceof z.ZodObject) {
|
|
6
11
|
return Object.entries(schema.shape).map(([key, value]) => {
|
|
7
12
|
const inner = unwrap(value as ZodTypeAny)
|
|
@@ -18,15 +23,39 @@ function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
|
|
|
18
23
|
})
|
|
19
24
|
}
|
|
20
25
|
|
|
26
|
+
if (schema instanceof z.ZodArray) {
|
|
27
|
+
const item = unwrap(schema._def.type as ZodTypeAny)
|
|
28
|
+
if (item instanceof z.ZodObject) {
|
|
29
|
+
const r = extractZodKeys(item)
|
|
30
|
+
// @ts-ignore
|
|
31
|
+
r[m] = true
|
|
32
|
+
return r
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return []
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
return []
|
|
22
39
|
}
|
|
23
40
|
|
|
24
|
-
function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
41
|
+
export function unwrap(schema: ZodTypeAny): ZodTypeAny {
|
|
25
42
|
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable)
|
|
26
43
|
return unwrap(schema._def.innerType)
|
|
27
44
|
|
|
28
|
-
if (schema instanceof z.
|
|
29
|
-
return unwrap(schema._def.
|
|
45
|
+
if (schema instanceof z.ZodDefault)
|
|
46
|
+
return unwrap(schema._def.innerType)
|
|
47
|
+
|
|
48
|
+
// if (schema instanceof z.ZodUnion)
|
|
49
|
+
// return unwrap(schema._def.options[0] as ZodTypeAny)
|
|
50
|
+
|
|
51
|
+
if (schema instanceof z.ZodUnion) {
|
|
52
|
+
const options = schema._def.options as ZodTypeAny[]
|
|
53
|
+
const nonEmptyOption = options.find(opt => !(opt instanceof z.ZodUndefined) && !(opt instanceof z.ZodNull))
|
|
54
|
+
return nonEmptyOption ? unwrap(nonEmptyOption) : options[0]
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (schema instanceof z.ZodEffects)
|
|
58
|
+
return unwrap(schema._def.schema)
|
|
30
59
|
|
|
31
60
|
return schema
|
|
32
61
|
}
|