case insensitive lookup

This commit is contained in:
Vikhyath Mondreti
2026-01-26 00:23:33 -08:00
parent 8aabf06f62
commit 87dcd53b98
2 changed files with 42 additions and 6 deletions

View File

@@ -53,17 +53,34 @@ function getProperties(schema: unknown): Record<string, unknown> | undefined {
: undefined
}
function getFieldCaseInsensitive(
obj: Record<string, unknown>,
fieldName: string
): unknown | undefined {
if (fieldName in obj) {
return obj[fieldName]
}
const lowerName = fieldName.toLowerCase()
for (const key of Object.keys(obj)) {
if (key.toLowerCase() === lowerName) {
return obj[key]
}
}
return undefined
}
function lookupField(schema: unknown, fieldName: string): unknown | undefined {
if (typeof schema !== 'object' || schema === null) return undefined
const typed = schema as Record<string, unknown>
if (fieldName in typed) {
return typed[fieldName]
const direct = getFieldCaseInsensitive(typed, fieldName)
if (direct !== undefined) {
return direct
}
const props = getProperties(schema)
if (props && fieldName in props) {
return props[fieldName]
if (props) {
return getFieldCaseInsensitive(props, fieldName)
}
return undefined

View File

@@ -20,6 +20,19 @@ export interface Resolver {
* navigatePath({a: {b: {c: 1}}}, ['a', 'b', 'c']) => 1
* navigatePath({items: [{name: 'test'}]}, ['items', '0', 'name']) => 'test'
*/
function getPropertyCaseInsensitive(obj: Record<string, unknown>, key: string): unknown {
if (key in obj) {
return obj[key]
}
const lowerKey = key.toLowerCase()
for (const k of Object.keys(obj)) {
if (k.toLowerCase() === lowerKey) {
return obj[k]
}
}
return undefined
}
export function navigatePath(obj: any, path: string[]): any {
let current = obj
for (const part of path) {
@@ -30,7 +43,10 @@ export function navigatePath(obj: any, path: string[]): any {
const arrayMatch = part.match(/^([^[]+)(\[.+)$/)
if (arrayMatch) {
const [, prop, bracketsPart] = arrayMatch
current = current[prop]
current =
typeof current === 'object' && current !== null
? getPropertyCaseInsensitive(current, prop)
: undefined
if (current === undefined || current === null) {
return undefined
}
@@ -49,7 +65,10 @@ export function navigatePath(obj: any, path: string[]): any {
const index = Number.parseInt(part, 10)
current = Array.isArray(current) ? current[index] : undefined
} else {
current = current[part]
current =
typeof current === 'object' && current !== null
? getPropertyCaseInsensitive(current, part)
: undefined
}
}
return current