rajt 0.0.43 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rajt",
3
3
  "description": "A serverless bundler layer, fully typed for AWS Lambda (Node.js and LLRT) and Cloudflare Workers.",
4
- "version": "0.0.43",
4
+ "version": "0.0.44",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "exports": {
@@ -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
@@ -49,7 +50,7 @@ export default class Compact {
49
50
  if (!val) return val as T
50
51
 
51
52
  if (Array.isArray(val))
52
- return val.map((i: { v: string }) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
53
+ return val.map((i: {v: string}) => this.decode<T>(i?.V, schema)).filter(Boolean) as T
53
54
 
54
55
  return val?.V ? this.decode<T>(val.V, schema) : val
55
56
  }
@@ -68,6 +69,9 @@ export default class Compact {
68
69
  }
69
70
 
70
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
+
71
75
  if (!obj || [null, true, false].includes(obj)) return obj
72
76
 
73
77
  return schema.map(key => {
@@ -85,16 +89,16 @@ export default class Compact {
85
89
  })
86
90
  }
87
91
 
88
- static unzip(val: any, seen: any[] = [], deep = false): 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
+
89
96
  const type = typeof val
90
97
  const length = getLength(val, type)
91
98
 
92
99
  if ([null, true, false].includes(val) || type != 'object' && length < 2)
93
100
  return val
94
101
 
95
- if (Array.isArray(val))
96
- return val.map(item => this.unzip(item, seen, deep))
97
-
98
102
  if (type == 'object') {
99
103
  for (const key in val)
100
104
  val[key] = this.unzip(val[key], seen)
@@ -111,10 +115,13 @@ export default class Compact {
111
115
  return val
112
116
  }
113
117
 
114
- static withSchema(value: any[], keys: any[]): any {
118
+ static withSchema(value: any[], keys: any[], deep = false): any {
115
119
  if (!value || !Array.isArray(value))
116
120
  return value
117
121
 
122
+ if (!deep && isArraySchema(keys))
123
+ return value?.length ? value.map(v => this.withSchema(v, keys, true)) : []
124
+
118
125
  return Object.fromEntries(
119
126
  keys.map((key, index) => this.entry(key, value[index])).filter(Boolean)
120
127
  )
@@ -1,7 +1,12 @@
1
1
  import { z, ZodTypeAny } from 'zod'
2
2
  import type { SchemaStructure } from './types'
3
3
 
4
- function extractZodKeys(schema: ZodTypeAny): SchemaStructure {
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.ZodUnion)
29
- return unwrap(schema._def.options[0] as ZodTypeAny)
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
  }