Last active
August 29, 2015 14:22
-
-
Save Error601/509f7ace153107494c85 to your computer and use it in GitHub Desktop.
Robust conversion between camelCase and hyph-en-ated text.
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
/*! | |
* Robust string conversion between | |
* hyphenated 'text-string' and | |
* camelCase 'textString'. | |
* Allows use of '_' (underscore), | |
* ' ' (space), '.' (period), and | |
* '-' (hyphen) as word delimiters, | |
* or an array of words. | |
*/ | |
// convert from camelCase 'textString', | |
// or underscored 'text_string', | |
// or space-separated 'text string' | |
// or period-separated 'text.string' | |
// or ['array', 'of', 'separate', 'strings'] | |
// to hyphenated 'text-string' | |
// optionally preserving original case where | |
// possible and allowing 'other' characters | |
// 'delim' is optional custom word delimiter | |
function toDashed(str, other, preserve, delim){ | |
if (Array.isArray(str)){ | |
str = str.join(' '); | |
// force delim to space if 'str' is array | |
delim = /\s+/; | |
} | |
else { | |
str = str+''; | |
} | |
return splitWords(str, delim).map(function(word){ | |
// add hyphens before capital letters | |
word = word.replace(/[A-Z]/g, function(c){ | |
return '-' + c; | |
}); | |
// remove hyphens after capital letters | |
word = word.replace(/([A-Z]\-)/g, function(c){ | |
return c.replace(/\-$/, ''); | |
}); | |
if (!preserve){ | |
word = word.toLowerCase(); | |
} | |
if (!other){ | |
word = word.replace(/[^A-Za-z0-9\-]/g, '-'); | |
} | |
return word; | |
}).join('-').replace(/\-+/g, '-').replace(/^\-+|\-+$/g, ''); | |
} | |
// convert from hyphenated 'text-string', | |
// or underscored 'text_string', | |
// or space-separated 'text string' | |
// or period-separated 'text.string' | |
// or ['array', 'of', 'separate', 'strings'] | |
// to camelCase 'textString' | |
// optionally preserving original case where | |
// possible and allowing 'other' characters | |
// 'delim' is optional custom word delimiter | |
function toCamelCase(str, other, preserve, delim){ | |
if (Array.isArray(str)){ | |
str = str.join(' '); | |
// force delim to space if 'str' is array | |
delim = /\s+/; | |
} | |
else { | |
str = str+''; | |
} | |
return splitWords(str, delim).map(function(word, i){ | |
if (!preserve){ | |
word = word.toLowerCase(); | |
} | |
if (i > 0){ | |
word = word.charAt(0).toUpperCase() + word.substr(1); | |
} | |
if (!other){ | |
word = word.replace(/[^A-Za-z0-9]/g, ''); | |
} | |
return word; | |
}).join(''); | |
} | |
// split string into array of words using spaces, underscores, | |
// hyphens, periods, commas, or 'delim' (RegExp) as word delimeters | |
function splitWords(str, delim){ | |
str = str+''; | |
delim = delim || /(\s|,|_|\-|\.)+/g; | |
// insert spaces between words and remove multiple spaces | |
str = str.replace(delim,' '); | |
// split on whitespace char. | |
return str.split(/\s/); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sure someone with better regex chops could do each of these with one line of code. That's not me, so I give you this...