mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-26 07:18:38 -05:00
case insensitive lookup
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user