Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Created December 18, 2019 04:32
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 jazzyjackson/a1e7d72465337bff98ddbdde5bf2cd29 to your computer and use it in GitHub Desktop.
Save jazzyjackson/a1e7d72465337bff98ddbdde5bf2cd29 to your computer and use it in GitHub Desktop.
let isRealNumber = key => /^[0-9]+$/.test(key)
let isOKforDotNotation = key => /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key)
function wrapKey(key){
if(isOKforDotNotation(key)){
return `.${key}`
} else if(isRealNumber(key)){
return `[${key}]`
} else {
return `["${key}"]`
}
}
module.exports = function formatKey(pathArray){
return pathArray.reduce((prev, curr) =>
prev + wrapKey(curr)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment