3 const preserveCamelCase = string => {
4 let isLastCharLower = false;
5 let isLastCharUpper = false;
6 let isLastLastCharUpper = false;
8 for (let i = 0; i < string.length; i++) {
9 const character = string[i];
11 if (isLastCharLower && /[a-zA-Z]/.test(character) && character.toUpperCase() === character) {
12 string = string.slice(0, i) + '-' + string.slice(i);
13 isLastCharLower = false;
14 isLastLastCharUpper = isLastCharUpper;
15 isLastCharUpper = true;
17 } else if (isLastCharUpper && isLastLastCharUpper && /[a-zA-Z]/.test(character) && character.toLowerCase() === character) {
18 string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
19 isLastLastCharUpper = isLastCharUpper;
20 isLastCharUpper = false;
21 isLastCharLower = true;
23 isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;
24 isLastLastCharUpper = isLastCharUpper;
25 isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;
32 const camelCase = (input, options) => {
33 if (!(typeof input === 'string' || Array.isArray(input))) {
34 throw new TypeError('Expected the input to be `string | string[]`');
37 options = Object.assign({
41 const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
43 if (Array.isArray(input)) {
44 input = input.map(x => x.trim())
45 .filter(x => x.length)
51 if (input.length === 0) {
55 if (input.length === 1) {
56 return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
59 const hasUpperCase = input !== input.toLowerCase();
62 input = preserveCamelCase(input);
66 .replace(/^[_.\- ]+/, '')
68 .replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase())
69 .replace(/\d+(\w|$)/g, m => m.toUpperCase());
71 return postProcess(input);
74 module.exports = camelCase;
75 // TODO: Remove this for the next major release
76 module.exports.default = camelCase;