export function findPath()

in src/dispatch/static/dispatch/src/util/jpath.ts [23:42]


export function findPath<T>(obj: T, key: keyof any, value: any, path: string = "$"): string | null {
  if (Array.isArray(obj)) {
    for (let i = 0; i < obj.length; i++) {
      const arrayPath = `${path}[*]`
      const result = findPath(obj[i], key, value, arrayPath)
      if (result) return result
    }
  } else if (typeof obj === "object" && obj !== null) {
    for (const [k, v] of Object.entries(obj)) {
      // Check if key contains special characters (non-alphanumeric or underscore)
      const currentPath = /\W/.test(k) ? `${path}['${k}']` : `${path}.${k}`
      if (k === key && simpleDeepEqual(v, value)) return currentPath
      if (typeof v === "object") {
        const result = findPath(v, key, value, currentPath)
        if (result) return result
      }
    }
  }
  return null
}