export function pathToJsonPointer()

in src/lib/util/utils.ts [558:589]


export function pathToJsonPointer(jsonPath: string): string {
  const replaceAllReg = (src: string): RegExp => {
    return new RegExp(src, "g")
  }
  let result: string = jsonPath.replace(replaceAllReg("~"), "~0").replace(replaceAllReg("/"), "~1").replace(replaceAllReg("\\."), "/")

  // match subpath with special character which be surround by ' e.g. paths['~0test~1'] , and replace it to path/~0test~1
  let regex = /(\[\'.+\'\])/g
  let matchs = result.match(regex)
  if (matchs) {
    matchs.forEach(m => {
      result = result.replace(
        m,
        m
          .replace(replaceAllReg("/"), ".") // the `.` in [] was replaced by / first , here replace it back
          .replace(/^\[\'/gi, "/")
          .replace(/\'\]$/gi, "")
      )
    })
  }

  // match the array index e.g. path[0] and replace it to path/0
  regex = /(\[\d+\])/g
  matchs = result.match(regex)
  if (matchs) {
    matchs.forEach((m: string) => {
      result = result.replace(m, m.replace(/(\[)/gi, "/").replace(/\]$/gi, ""))
    })
  }

  return !result ? "" : "/" + result
}