Skip to content

Commit 1d6a9eb

Browse files
committed
improve code comments
1 parent d5a0f01 commit 1d6a9eb

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/field/schema.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,19 @@ You can fix the json schema or skip this error by calling createHeadlessForm(sch
142142
return getInputTypeFromSchema(type || schema.type || 'string', schema)
143143
}
144144

145-
// Cache converted options by content hash (works even when schemas are cloned)
146-
const optionsByContent = new Map<string, Array<FieldOption>>()
145+
const optionsMap = new Map<string, Array<FieldOption>>()
147146

148-
// Create content hash from options array (uses length + sample for speed)
147+
/**
148+
* Create a hash from options array for caching
149+
* @param opts - The options to hash
150+
* @returns The hash
151+
*/
149152
function hashOptions(opts: JsfSchema[]): string {
150153
if (!opts.length) {
151154
return '0'
152155
}
153156

154-
// Extract the const value from a schema option for hashing
157+
// Extract the const value from an option
155158
const extractValue = (option: JsfSchema) => {
156159
return (typeof option === 'object' && option !== null) ? option.const : option
157160
}
@@ -161,7 +164,7 @@ function hashOptions(opts: JsfSchema[]): string {
161164
const middle = opts[Math.floor(length / 2)]
162165
const end = opts[length - 1]
163166

164-
// Sample first, middle, last options for a lightweight but reliable hash
167+
// Sample 3 parts of the array for a reliable hash
165168
const sampledValues = [
166169
extractValue(start),
167170
extractValue(middle),
@@ -182,14 +185,12 @@ function hashOptions(opts: JsfSchema[]): string {
182185
* If it doesn't, we skip the option.
183186
*/
184187
function convertToOptions(nodeOptions: JsfSchema[]): Array<FieldOption> {
185-
// Check cache by content hash (works even when schemas are cloned)
186188
const hash = hashOptions(nodeOptions)
187-
const cached = optionsByContent.get(hash)
189+
const cached = optionsMap.get(hash)
188190
if (cached) {
189191
return cached
190192
}
191193

192-
// Convert options
193194
const converted = nodeOptions
194195
.filter((option): option is NonBooleanJsfSchema =>
195196
option !== null && typeof option === 'object' && option.const !== null,
@@ -214,8 +215,7 @@ function convertToOptions(nodeOptions: JsfSchema[]): Array<FieldOption> {
214215
return { ...result, ...presentation, ...rest }
215216
})
216217

217-
// Cache for future use
218-
optionsByContent.set(hash, converted)
218+
optionsMap.set(hash, converted)
219219
return converted
220220
}
221221

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export function deepMergeSchemas<T extends Record<string, any>>(schema1: T, sche
100100
else if (schema1Value && Array.isArray(schema2Value)) {
101101
const originalArray = schema1Value
102102

103-
// For 'options' arrays, just replace the whole array (they're immutable and cached)
103+
// For 'options' arrays, just replace the whole array (they're immutable and cached) rather
104+
// than recursively deep merging them
104105
if (key === 'options') {
105106
schema1[key as keyof T] = schema2Value as T[keyof T]
106107
continue

0 commit comments

Comments
 (0)