Skip to content

Instantly share code, notes, and snippets.

@dustinknopoff
Created July 24, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinknopoff/644c58bf21734131d6fe595ffe066034 to your computer and use it in GitHub Desktop.
Save dustinknopoff/644c58bf21734131d6fe595ffe066034 to your computer and use it in GitHub Desktop.
Simple JSON pointer walking on javascript objects with no error handling. A super simplified version of https://github.com/json-schema-spec/json-pointer-typescript
export const walkPointer = (object: { [key: string] }, pointer: string) => {
let [, ...tokens] = pointer.split("/")
tokens = tokens.map((token) => {
return token.replace(/~1/g, "/").replace(/~0/g, "~");
})
let instance = object
for (const token of tokens) {
instance = instance[token]
}
return instance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment