@kubb/react-fabric 0.7.3 → 0.7.4

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.
@@ -35,14 +35,10 @@ type ParamItem =
35
35
  children?: Params
36
36
  })
37
37
 
38
- const TSBasicTypes = ['number', 'string', 'null', 'undefined', 'bigint', 'boolean', 'symbol'] as const
39
-
40
- type ParamEntry = [key: string, item?: ParamItem | undefined]
41
-
42
38
  export type Params = Record<string, Param | undefined>
43
39
 
44
40
  type Options = {
45
- type: 'constructor' | 'call' | 'object' | 'objectValue' | 'callback'
41
+ type: 'constructor' | 'call' | 'object' | 'objectValue'
46
42
  transformName?: (name: string) => string
47
43
  transformType?: (type: string) => string
48
44
  }
@@ -68,55 +64,44 @@ function order(items: Array<[key: string, item?: ParamItem]>) {
68
64
  )
69
65
  }
70
66
 
71
- function processEntry(options: Options): ([key, item]: ParamEntry) => string | null {
72
- return function ([key, item]: ParamEntry): string | null {
73
- if (!item) return null
74
-
75
- if (!item.children) return parseItem(key, item, options)
76
- if (Object.keys(item.children).length === 0) return null
77
-
78
- if (item.mode === 'inlineSpread') {
79
- return getFunctionParams(item.children, options)
80
- }
81
-
82
- return parseChild(key, item, options)
83
- }
84
- }
85
-
86
- function processChildEntry(options: Options, entries: ParamEntry[]): ([key, item]: ParamEntry) => { name: string | null; type: string | null } | null {
87
- return function ([key, item]: ParamEntry): { name: string | null; type: string | null } | null {
88
- if (!item) return null
67
+ function parseChild(key: string, item: ParamItem, options: Options): string | null {
68
+ // @ts-expect-error
69
+ const entries = order(Object.entries(item.children))
89
70
 
90
- const result = { name: null, type: null } as { name: string | null; type: string | null }
71
+ const types: string[] = []
72
+ const names: string[] = []
91
73
 
92
- const name = parseItem(key, { ...item, type: undefined }, options, item.type)
74
+ const optional = entries.every(([_key, item]) => item?.optional)
93
75
 
94
- if (!item.children) {
95
- result.name = options.type === 'call' && options.transformName ? `${key}: ${name}` : name
96
- } else {
97
- const subTypes = Object.keys(item.children).join(', ')
98
- result.name = subTypes ? `${name}: { ${subTypes} }` : name
99
- }
76
+ entries.forEach(([key, entryItem]) => {
77
+ if (entryItem) {
78
+ const name = parseItem(key, { ...entryItem, type: undefined }, options)
79
+ if (entryItem.children) {
80
+ const subTypes = Object.entries(entryItem.children)
81
+ .map(([key]) => {
82
+ return key
83
+ })
84
+ .join(', ')
85
+
86
+ if (subTypes) {
87
+ names.push(`${name}: { ${subTypes} }`)
88
+ } else {
89
+ names.push(name)
90
+ }
91
+ } else {
92
+ if (options.type === 'call' && options.transformName) {
93
+ names.push(`${key}: ${name}`)
94
+ } else {
95
+ names.push(name)
96
+ }
97
+ }
100
98
 
101
- const anySiblingHasType = entries.some(([_k, entries]) => !!entries?.type)
102
- if (anySiblingHasType) {
103
- result.type = parseItem(key, { ...item, default: undefined }, options, item.type)
99
+ if (entries.some(([_key, item]) => item?.type)) {
100
+ types.push(parseItem(key, { ...entryItem, default: undefined }, options))
101
+ }
104
102
  }
103
+ })
105
104
 
106
- return result
107
- }
108
- }
109
-
110
- function parseChild(key: string, item: ParamItem, options: Options): string | null {
111
- const entries = order(Object.entries(item.children as Record<string, ParamItem | undefined>))
112
-
113
- const optional = entries.every(([_key, item]) => item?.optional)
114
-
115
- const childEntryProcessor = processChildEntry(options, entries)
116
- const result = entries.map(childEntryProcessor).filter((item): item is { name: string | null; type: string | null } => item !== null)
117
-
118
- const names = result.map(({ name }) => name).filter(Boolean)
119
- const types = result.map(({ type }) => type).filter(Boolean)
120
105
  const name = item.mode === 'inline' ? key : names.length ? `{ ${names.join(', ')} }` : undefined
121
106
  const type = item.type ? item.type : types.length ? `{ ${types.join('; ')} }` : undefined
122
107
 
@@ -132,11 +117,11 @@ function parseChild(key: string, item: ParamItem, options: Options): string | nu
132
117
  optional: !item.default ? optional : undefined,
133
118
  } as ParamItem,
134
119
  options,
135
- item.type,
136
120
  )
137
121
  }
138
122
 
139
- function parseItem(name: string, item: ParamItem, options: Options, parentType?: string): string {
123
+ function parseItem(name: string, item: ParamItem, options: Options): string {
124
+ const acc: string[] = []
140
125
  const transformedName = options.transformName ? options.transformName(name) : name
141
126
  const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type
142
127
 
@@ -147,59 +132,57 @@ function parseItem(name: string, item: ParamItem, options: Options, parentType?:
147
132
  if (options.type === 'objectValue') {
148
133
  return item.value ? `${transformedName}: ${item.value}` : transformedName
149
134
  }
150
- if (options.type === 'callback') {
151
- const isObject = (transformedType?.includes('{') && transformedType?.includes('}')) ?? false
152
-
153
- if (transformedType === parentType) {
154
- if (transformedType && !TSBasicTypes.includes(transformedType as (typeof TSBasicTypes)[number])) {
155
- return `${transformedName}: { [K in keyof ${transformedType}]: () => ${transformedType}[K] }`
156
- }
157
- }
158
- if (item.default) {
159
- if (isObject) return transformedType ? `${transformedName}: ${transformedType} = ${item.default}` : `${transformedName} = ${item.default}`
160
- return transformedType ? `${transformedName}: () => ${transformedType} = () => ${item.default}` : `${transformedName} = () => ${item.default}`
161
- }
162
- if (item.optional) {
163
- if (isObject) return transformedType ? `${transformedName}?: ${transformedType}` : transformedName
164
- return transformedType ? `${transformedName}?: () => ${transformedType}` : transformedName
165
- }
166
- if (transformedType) {
167
- if (isObject) return `${transformedName}: ${transformedType}`
168
- return `${transformedName}: () => ${transformedType}`
169
- }
170
- return transformedName
171
- }
172
135
 
173
- if (options.type === 'constructor') {
174
- if (item.mode === 'inlineSpread') {
175
- return `... ${transformedName}`
176
- }
177
- if (item.value) {
178
- return `${transformedName} : ${item.value}`
179
- }
180
- if (item.default) {
181
- return transformedType ? `${transformedName}: ${transformedType} = ${item.default}` : `${transformedName} = ${item.default}`
182
- }
136
+ //LEGACY
137
+ if (item.type && options.type === 'constructor') {
183
138
  if (item.optional) {
184
- return transformedType ? `${transformedName}?: ${transformedType}` : `${transformedName}`
185
- }
186
- if (transformedType) {
187
- return `${transformedName}: ${transformedType}`
139
+ acc.push(`${transformedName}?: ${transformedType}`)
140
+ } else {
141
+ acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ''}`)
188
142
  }
189
-
190
- return transformedName
143
+ } else if (item.default && options.type === 'constructor') {
144
+ acc.push(`${transformedName} = ${item.default}`)
145
+ } else if (item.value) {
146
+ acc.push(`${transformedName} : ${item.value}`)
147
+ } else if (item.mode === 'inlineSpread') {
148
+ acc.push(`... ${transformedName}`)
149
+ } else {
150
+ acc.push(transformedName)
191
151
  }
192
152
 
193
- return transformedName
153
+ return acc[0] as string
194
154
  }
195
155
 
196
156
  export function getFunctionParams(params: Params, options: Options): string {
197
- const entries: ParamEntry[] = order(Object.entries(params as Record<string, ParamItem | undefined>))
198
- const entryProcessor = processEntry(options)
157
+ const entries = order(Object.entries(params as Record<string, ParamItem | undefined>))
199
158
 
200
159
  return entries
201
- .map(entryProcessor)
202
- .filter((item): item is string => item !== null)
160
+ .reduce((acc, [key, item]) => {
161
+ if (!item) {
162
+ return acc
163
+ }
164
+
165
+ if (item.children) {
166
+ if (Object.keys(item.children).length === 0) {
167
+ return acc
168
+ }
169
+
170
+ if (item.mode === 'inlineSpread') {
171
+ return [...acc, getFunctionParams(item.children, options)]
172
+ }
173
+
174
+ const parsedItem = parseChild(key, item, options)
175
+ if (!parsedItem) {
176
+ return acc
177
+ }
178
+
179
+ return [...acc, parsedItem]
180
+ }
181
+
182
+ const parsedItem = parseItem(key, item, options)
183
+
184
+ return [...acc, parsedItem]
185
+ }, [] as string[])
203
186
  .join(', ')
204
187
  }
205
188
 
@@ -250,8 +233,4 @@ export class FunctionParams {
250
233
  toConstructor(): string {
251
234
  return getFunctionParams(this.#params, { type: 'constructor' })
252
235
  }
253
-
254
- toCallback(): string {
255
- return getFunctionParams(this.#params, { type: 'callback' })
256
- }
257
236
  }