-
-
Save JakeLabate/0f86c4b1141447ba1af59f3f94b504de to your computer and use it in GitHub Desktop.
This file contains 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
// generative id function | |
function id(type, identifier) { | |
// ensure identifier is a string | |
identifier = String(identifier); | |
// set placeholder | |
if (!identifier) { | |
// set identifier to 'PLACEHOLDER' | |
identifier = 'PLACEHOLDER'; | |
// log warning | |
console.warn('identifier is empty, using PLACEHOLDER'); | |
} | |
// convert string to camelCase function | |
function camel(identifier) { | |
// split the identifier into an array of words by spaces | |
identifier = identifier.split(' '); | |
// convert the array of words into camelCase format | |
return identifier.map((word, index) => { | |
// ensure each word is in lowercase | |
const lowerCasedWord = word.toLowerCase(); | |
// capitalize the first letter of all words (except the first word) | |
return index === 0 ? lowerCasedWord : lowerCasedWord.charAt(0).toUpperCase() + lowerCasedWord.slice(1); | |
// join the array of words back into a string | |
}).join(''); | |
} | |
// concatenate the type and identifier with camelCase | |
const identifierCamel = camel(identifier); | |
// set fallback type | |
type = type || 'Thing'; | |
// construct & return id | |
return 'id#' + type + '-' + identifierCamel; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment