@kubb/react-fabric 0.7.2 → 0.7.3

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,10 +35,14 @@ 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
+
38
42
  export type Params = Record<string, Param | undefined>
39
43
 
40
44
  type Options = {
41
- type: 'constructor' | 'call' | 'object' | 'objectValue'
45
+ type: 'constructor' | 'call' | 'object' | 'objectValue' | 'callback'
42
46
  transformName?: (name: string) => string
43
47
  transformType?: (type: string) => string
44
48
  }
@@ -64,44 +68,55 @@ function order(items: Array<[key: string, item?: ParamItem]>) {
64
68
  )
65
69
  }
66
70
 
67
- function parseChild(key: string, item: ParamItem, options: Options): string | null {
68
- // @ts-expect-error
69
- const entries = order(Object.entries(item.children))
71
+ function processEntry(options: Options): ([key, item]: ParamEntry) => string | null {
72
+ return function ([key, item]: ParamEntry): string | null {
73
+ if (!item) return null
70
74
 
71
- const types: string[] = []
72
- const names: string[] = []
75
+ if (!item.children) return parseItem(key, item, options)
76
+ if (Object.keys(item.children).length === 0) return null
73
77
 
74
- const optional = entries.every(([_key, item]) => item?.optional)
78
+ if (item.mode === 'inlineSpread') {
79
+ return getFunctionParams(item.children, options)
80
+ }
75
81
 
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
- }
82
+ return parseChild(key, item, options)
83
+ }
84
+ }
98
85
 
99
- if (entries.some(([_key, item]) => item?.type)) {
100
- types.push(parseItem(key, { ...entryItem, default: undefined }, options))
101
- }
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
89
+
90
+ const result = { name: null, type: null } as { name: string | null; type: string | null }
91
+
92
+ const name = parseItem(key, { ...item, type: undefined }, options, item.type)
93
+
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
+ }
100
+
101
+ const anySiblingHasType = entries.some(([_k, entries]) => !!entries?.type)
102
+ if (anySiblingHasType) {
103
+ result.type = parseItem(key, { ...item, default: undefined }, options, item.type)
102
104
  }
103
- })
104
105
 
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)
105
120
  const name = item.mode === 'inline' ? key : names.length ? `{ ${names.join(', ')} }` : undefined
106
121
  const type = item.type ? item.type : types.length ? `{ ${types.join('; ')} }` : undefined
107
122
 
@@ -117,11 +132,11 @@ function parseChild(key: string, item: ParamItem, options: Options): string | nu
117
132
  optional: !item.default ? optional : undefined,
118
133
  } as ParamItem,
119
134
  options,
135
+ item.type,
120
136
  )
121
137
  }
122
138
 
123
- function parseItem(name: string, item: ParamItem, options: Options): string {
124
- const acc: string[] = []
139
+ function parseItem(name: string, item: ParamItem, options: Options, parentType?: string): string {
125
140
  const transformedName = options.transformName ? options.transformName(name) : name
126
141
  const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type
127
142
 
@@ -132,57 +147,59 @@ function parseItem(name: string, item: ParamItem, options: Options): string {
132
147
  if (options.type === 'objectValue') {
133
148
  return item.value ? `${transformedName}: ${item.value}` : transformedName
134
149
  }
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
+ }
135
172
 
136
- //LEGACY
137
- if (item.type && options.type === 'constructor') {
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
+ }
138
183
  if (item.optional) {
139
- acc.push(`${transformedName}?: ${transformedType}`)
140
- } else {
141
- acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ''}`)
184
+ return transformedType ? `${transformedName}?: ${transformedType}` : `${transformedName}`
142
185
  }
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)
186
+ if (transformedType) {
187
+ return `${transformedName}: ${transformedType}`
188
+ }
189
+
190
+ return transformedName
151
191
  }
152
192
 
153
- return acc[0] as string
193
+ return transformedName
154
194
  }
155
195
 
156
196
  export function getFunctionParams(params: Params, options: Options): string {
157
- const entries = order(Object.entries(params as Record<string, ParamItem | undefined>))
197
+ const entries: ParamEntry[] = order(Object.entries(params as Record<string, ParamItem | undefined>))
198
+ const entryProcessor = processEntry(options)
158
199
 
159
200
  return entries
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[])
201
+ .map(entryProcessor)
202
+ .filter((item): item is string => item !== null)
186
203
  .join(', ')
187
204
  }
188
205
 
@@ -233,4 +250,8 @@ export class FunctionParams {
233
250
  toConstructor(): string {
234
251
  return getFunctionParams(this.#params, { type: 'constructor' })
235
252
  }
253
+
254
+ toCallback(): string {
255
+ return getFunctionParams(this.#params, { type: 'callback' })
256
+ }
236
257
  }