Skip to content

Instantly share code, notes, and snippets.

@gitexec
Created November 1, 2017 17:39
Show Gist options
  • Save gitexec/36a409beba258c7f67bef5d6831dafa0 to your computer and use it in GitHub Desktop.
Save gitexec/36a409beba258c7f67bef5d6831dafa0 to your computer and use it in GitHub Desktop.
transform-dashed-strings-to-Caps.js
function buildReactExternals(dependenciesArray){
let externals = dependenciesArray.reduce((dependencies, curr, index, array) => {
let secondCapIndex = curr.indexOf('-');
let lastCapIndex = curr.lastIndexOf('-');
let firstCap, secondCap, middleCap, valueCap = curr, key, lastCap;
firstCap = curr.charAt(0).toUpperCase();
if (secondCapIndex !== -1) {
secondCapIndex++;
secondCap = valueCap.charAt(secondCapIndex).toUpperCase(); //curr.substring(1, secondCapIndex + 1) + curr.charAt(secondCapIndex+1).toUpperCase() + curr.substring(secondCapIndex + 2);
}
if (secondCapIndex !== -1 && lastCapIndex !== -1) {
lastCapIndex++;
lastCap = valueCap.charAt(lastCapIndex).toUpperCase(); //secondCap.substring(0, lastCapIndex) + curr.charAt(lastCapIndex+1).toUpperCase() + curr.substring(lastCapIndex + 2);
}
if (secondCap)
valueCap = firstCap + valueCap.substring(1, secondCapIndex) + secondCap + valueCap.substring(secondCapIndex + 1);
if (secondCap && lastCap)
valueCap = valueCap.substring(0, lastCapIndex) + lastCap + valueCap.substring(lastCapIndex + 1);
if (!secondCap || !lastCap)
valueCap = firstCap + valueCap.substring(1);
valueCap = valueCap.split('-').join('');
key = `${curr}`;
dependencies[key] = `${valueCap}`;
return dependencies;
}, {});
return externals;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment