diff --git a/src/caseconverter.js b/src/caseconverter.js index 6d36e46..c5b26df 100644 --- a/src/caseconverter.js +++ b/src/caseconverter.js @@ -3,7 +3,7 @@ * more information and online tools for this implementation on: https://en.toolpage.org/cat/case-converter * - * @author Yves Sorge + * @author Yves Sorge, Daniel Kendall (camelCase) */ function CaseConverterClass() { @@ -77,19 +77,44 @@ function CaseConverterClass() { } /** - * Converts a text into camel case. - * Example: "camel case" into "CamelCase". + * Converts a text into pascal case. + * Example: "Pascal case" into "PascalCase". * * @param {String} value * @return {String} converted string */ - this.convertToCamelCase = function(value) { + this.convertToPascalCase = function(value) { var replace = "/"; value = value.replace(/[\(\)\[\]\{\}\=\?\!\.\:,\-_\+\\\"#~\/]/g, " "); value = this.convertToStartCase(value); return value.replace(/\s+/g, ""); } + /** + * Converts a text into camel case. + * Example: "camel case" into "camelCase". + * + * @param {String} value + * @return {String} converted string + */ + this.convertToCamelCase = function(value) { + // Remove special characters and convert to start case + value = value.replace(/[\(\)\[\]\{\}\=\?\!\.\:,\-\_\+\\\"\#~\/]/g, " "); + value = this.convertToStartCase(value); + + // Convert to camelCase + const words = value.split(" "); + const camelCaseWords = words.map((word, index) => { + if (index === 0) { + return word.toLowerCase(); + } else { + return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); + } + }); + + return camelCaseWords.join(""); + } + /** * Converts a text into snake case. * Example: "snake case" into "Snake_Case".