2019-09-18 08:11:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
const preserveCamelCase = string => {
|
|
|
|
let isLastCharLower = false;
|
|
|
|
let isLastCharUpper = false;
|
|
|
|
let isLastLastCharUpper = false;
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
for (let i = 0; i < string.length; i++) {
|
|
|
|
const character = string[i];
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) {
|
|
|
|
string = string.slice(0, i) + '-' + string.slice(i);
|
2019-09-18 08:11:16 +00:00
|
|
|
isLastCharLower = false;
|
2019-11-07 13:11:05 +00:00
|
|
|
isLastLastCharUpper = isLastCharUpper;
|
|
|
|
isLastCharUpper = true;
|
2019-09-18 08:11:16 +00:00
|
|
|
i++;
|
2019-11-07 13:11:05 +00:00
|
|
|
} else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(character) && character.toLowerCase() === character) {
|
|
|
|
string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
|
|
|
|
isLastLastCharUpper = isLastCharUpper;
|
|
|
|
isLastCharUpper = false;
|
|
|
|
isLastCharLower = true;
|
2019-09-18 08:11:16 +00:00
|
|
|
} else {
|
2019-11-07 13:11:05 +00:00
|
|
|
isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;
|
|
|
|
isLastLastCharUpper = isLastCharUpper;
|
|
|
|
isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
return string;
|
|
|
|
};
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
const camelCase = (input, options) => {
|
|
|
|
if (!(typeof input === 'string' || Array.isArray(input))) {
|
|
|
|
throw new TypeError('Expected the input to be `string | string[]`');
|
|
|
|
}
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
options = Object.assign({
|
|
|
|
pascalCase: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
|
|
|
|
|
|
|
|
if (Array.isArray(input)) {
|
|
|
|
input = input.map(x => x.trim())
|
|
|
|
.filter(x => x.length)
|
|
|
|
.join('-');
|
|
|
|
} else {
|
|
|
|
input = input.trim();
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (input.length === 0) {
|
|
|
|
return '';
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (input.length === 1) {
|
|
|
|
return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
|
|
|
|
}
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
const hasUpperCase = input !== input.toLowerCase();
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
if (hasUpperCase) {
|
|
|
|
input = preserveCamelCase(input);
|
2019-09-18 08:11:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
input = input
|
|
|
|
.replace(/^[_.\- ]+/, '')
|
|
|
|
.toLowerCase()
|
|
|
|
.replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase())
|
|
|
|
.replace(/\d+(\w|$)/g, m => m.toUpperCase());
|
2019-09-18 08:11:16 +00:00
|
|
|
|
2019-11-07 13:11:05 +00:00
|
|
|
return postProcess(input);
|
2019-09-18 08:11:16 +00:00
|
|
|
};
|
2019-11-07 13:11:05 +00:00
|
|
|
|
|
|
|
module.exports = camelCase;
|
|
|
|
// TODO: Remove this for the next major release
|
|
|
|
module.exports.default = camelCase;
|