Convert a dash/dot/underscore/space separated string to camelCase or PascalCase.
"use strict";
//
// camelCase v6.0.0
//
// Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: foo-bar > fooBar
// Correctly handles Unicode strings. Correctly handles Unicode strings.
//
// Author/License: Copyright (c) MIT Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
// GitHub: https://github.com/sindresorhus/camelcase
//
// Usage:
//
// camelCase('foo-bar');
// //=> 'fooBar'
//
// camelCase('foo_bar');
// //=> 'fooBar'
//
// camelCase('Foo-Bar');
// //=> 'fooBar'
//
// camelCase('розовый_пушистый_единороги');
// //=> 'розовыйПушистыйЕдинороги'
//
// camelCase('Foo-Bar', {pascalCase: true});
// //=> 'FooBar'
//
// camelCase('--foo.bar', {pascalCase: false});
// //=> 'fooBar'
//
// camelCase('foo bar');
// //=> 'fooBar'
//
// console.log(process.argv[3]);
// //=> '--foo-bar'
// camelCase(process.argv[3]);
// //=> 'fooBar'
//
// camelCase(['foo', 'bar']);
// //=> 'fooBar'
//
// camelCase(['__foo__', '--bar'], {pascalCase: true});
// //=> 'FooBar'
//
// Author/License: Copyright (c) MIT Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
// GitHub: https://github.com/sindresorhus/camelcase
//
var _preserveCamelCase = function (string) {
var isLastCharLower = false;
var isLastCharUpper = false;
var isLastLastCharUpper = false;
for (var i = 0; i < string.length; i++) {
var character = string[i];
if (
isLastCharLower &&
/[a-zA-Z]/.test(character) &&
character.toUpperCase() === character
) {
string = string.slice(0, i) + "-" + string.slice(i);
isLastCharLower = false;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper = true;
i++;
} 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;
} else {
isLastCharLower =
character.toLowerCase() === character &&
character.toUpperCase() !== character;
isLastLastCharUpper = isLastCharUpper;
isLastCharUpper =
character.toUpperCase() === character &&
character.toLowerCase() !== character;
}
}
return string;
};
var camelCase = function (input, options) {
if (!(typeof input === "string" || Array.isArray(input))) {
throw new TypeError("Expected the input to be `string | string[]`");
}
options = Object.assign({ pascalCase: false }, options);
var postProcess = function (x) {
return options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
};
if (Array.isArray(input)) {
input = input
.map(function (x) {
return x.trim();
})
.filter(function (x) {
return x.length;
})
.join("-");
} else {
input = input.trim();
}
if (input.length === 0) {
return "";
}
if (input.length === 1) {
return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
}
var hasUpperCase = input !== input.toLowerCase();
if (hasUpperCase) {
input = _preserveCamelCase(input);
}
input = input
.replace(/^[_.\- ]+/, "")
.toLowerCase()
.replace(/[_.\- ]+(\w|$)/g, function (_, p1) {
return p1.toUpperCase();
})
.replace(/\d+(\w|$)/g, function (m) {
return m.toUpperCase();
});
return postProcess(input);
};