Created
August 25, 2017 22:33
-
-
Save Lwdthe1/8a7a6a0e1eb781b75b1365f0400fe9b3 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
function convertCase(s, toDelim) { | |
if (!s || !s.length) return s | |
var existingType, lastFoundDelim = -1, camelCaseIndex = {} | |
if(s.indexOf(' ') > -1) existingType = ' '; | |
if(s.indexOf('-') > -1) existingType = '-'; | |
if(s.indexOf('_') > -1) existingType = '_'; | |
if(s.indexOf('.') > -1) existingType = '.'; | |
if(!existingType) { | |
for(var i = 0; i < s.length; i++) { | |
var v = s[i] | |
var currentType = '' | |
if (lastFoundDelim == i - 1 && v == v.toUpperCase()) { | |
lastFoundDelim = i | |
currentType = 'camel' | |
camelCaseIndex[i] = true | |
} else if (lastFoundDelim == i - 1 && v == v.toLowerCase()) { | |
lastFoundDelim = i | |
currentType = 'camel' | |
} | |
if(!currentType) continue | |
if(!existingType || currentType == existingType) { | |
existingType = currentType | |
} | |
} | |
} | |
function toType() { | |
if(existingType == toDelim) return s | |
if(!toDelim || toDelim == 'camel') { | |
return toCamel() | |
} | |
if(!toDelim || toDelim == 'uc') { | |
return toUpperCamel() | |
} | |
switch (existingType) { | |
case ' ': | |
case '_': | |
case '-': | |
case '.': | |
return fromDelim() | |
break | |
case 'camel': | |
return fromCamel() | |
break | |
default: | |
return s | |
} | |
} | |
function replaceAll(str, find, replace) { | |
return str.replace(new RegExp(find, 'g'), replace); | |
} | |
function fromDelim() { | |
return replaceAll(s, existingType, toDelim) | |
} | |
function fromCamel() { | |
var newS = '', lastLowerCase = -1, lastUpperCase = -1, lastDelim = -1 | |
for(var i = 0; i < s.length; i++) { | |
const v = s[i] | |
if(camelCaseIndex[i]) { | |
newS += toDelim + v.toLowerCase() | |
} else { | |
newS += v.toLowerCase() | |
} | |
} | |
return newS | |
} | |
function toCamel() { | |
var newS = '', lastLowerCase = -1, lastUpperCase = -1 | |
var lastFoundDelim = -1 | |
for(var i = 0; i < s.length; i++) { | |
const v = s[i] | |
if (v == existingType) { | |
lastFoundDelim = i | |
} else if (lastFoundDelim == i - 1 && v == v.toUpperCase() && i > 0) { | |
lastUpperCase = i | |
newS += v.toLowerCase() | |
} else if (lastFoundDelim == i - 1 && v == v.toLowerCase() && i > 0) { | |
lastLowerCase = i | |
newS += v.toUpperCase() | |
} else { | |
newS += v | |
} | |
} | |
return newS | |
} | |
function toUpperCamel() { | |
var newS = '' | |
var lastFoundDelim = -1 | |
for(var i = 0; i < s.length; i++) { | |
const v = s[i] | |
if (v == existingType) { | |
lastFoundDelim = i | |
} else if (lastFoundDelim == i - 1 && v == v.toUpperCase()) { | |
lastUpperCase = i | |
newS += v.toLowerCase() | |
} else if (lastFoundDelim == i - 1 && v == v.toLowerCase()) { | |
lastLowerCase = i | |
newS += v.toUpperCase() | |
} else { | |
newS += v | |
} | |
} | |
return newS | |
} | |
return toType() | |
} | |
console.log(convertCase('weAreHereToHelp', '.')) | |
//console.log(convertCase('we_are_here_to_help')) | |
//console.log(convertCase('we_are_here_to_help', 'uc')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment