Last active
November 5, 2020 18:18
-
-
Save joepuzzo/75a7595d30cebc2bfdb1d830f624a23b to your computer and use it in GitHub Desktop.
Turns json pointer into Java Script Property Access Notation
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
/** | |
* 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