Created
July 24, 2022 08:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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