Skip to content

Instantly share code, notes, and snippets.

@hheinsoee
Last active October 10, 2023 05:26
Show Gist options
  • Save hheinsoee/f6c3a10e731c7f78066de555326905fb to your computer and use it in GitHub Desktop.
Save hheinsoee/f6c3a10e731c7f78066de555326905fb to your computer and use it in GitHub Desktop.
export const camelize = sentence => {
return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(
camelCaseMatch,
i
) {
if (+camelCaseMatch === 0) return "";
return i === 0
? camelCaseMatch.toLowerCase()
: camelCaseMatch.toUpperCase();
});
};
export const camelizeJsonArray = array => {
const newArray = array.map(row => {
const finalFormat = {};
for (const [key, value] of Object.entries(row)) {
finalFormat[camelize(key)] = value;
}
return finalFormat;
});
return newArray;
};
function camelCaseToCapitalizedSentence(camelCaseStr) {
// Split the camelCase string into words
let words = [];
let currentWord = '';
for (let char of camelCaseStr) {
if (char === char.toUpperCase() && currentWord) {
words.push(currentWord);
currentWord = char;
} else {
currentWord += char;
}
}
if (currentWord) {
words.push(currentWord);
}
// Capitalize the first letter of each word and join them
const capitalizedSentence = words.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ');
return capitalizedSentence;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment