Skip to content

Instantly share code, notes, and snippets.

@joepuzzo
Last active November 5, 2020 18:18
Show Gist options
  • Save joepuzzo/75a7595d30cebc2bfdb1d830f624a23b to your computer and use it in GitHub Desktop.
Save joepuzzo/75a7595d30cebc2bfdb1d830f624a23b to your computer and use it in GitHub Desktop.
Turns json pointer into Java Script Property Access Notation
/**
* Turns a JSON pointer into Java Script Propery Access Notation
*
* /foo/bar/baz/0/taz ---> foo.bar.baz[0].taz
*
* IF trim is set to true
*
* /foo/bar/baz/0/taz ---> foo.bar.baz[0]
*/
export default function getJSPAN($ref, trim) {
const path = $ref
.replace(/\//g, '.')
.replace(/\.properties\./, '')
.replace(/\.([0-9]+)/g, '[$1]');
if (trim) {
return path.replace(/(.*)[.[].*/, '$1');
}
return path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment