| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | const UPPERCASE = /[\p{Lu}]/u;
|
|---|
| 4 | const LOWERCASE = /[\p{Ll}]/u;
|
|---|
| 5 | const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
|
|---|
| 6 | const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
|
|---|
| 7 | const SEPARATORS = /[_.\- ]+/;
|
|---|
| 8 |
|
|---|
| 9 | const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
|
|---|
| 10 | const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
|
|---|
| 11 | const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
|
|---|
| 12 |
|
|---|
| 13 | const preserveCamelCase = (string, toLowerCase, toUpperCase) => {
|
|---|
| 14 | let isLastCharLower = false;
|
|---|
| 15 | let isLastCharUpper = false;
|
|---|
| 16 | let isLastLastCharUpper = false;
|
|---|
| 17 |
|
|---|
| 18 | for (let i = 0; i < string.length; i++) {
|
|---|
| 19 | const character = string[i];
|
|---|
| 20 |
|
|---|
| 21 | if (isLastCharLower && UPPERCASE.test(character)) {
|
|---|
| 22 | string = string.slice(0, i) + '-' + string.slice(i);
|
|---|
| 23 | isLastCharLower = false;
|
|---|
| 24 | isLastLastCharUpper = isLastCharUpper;
|
|---|
| 25 | isLastCharUpper = true;
|
|---|
| 26 | i++;
|
|---|
| 27 | } else if (isLastCharUpper && isLastLastCharUpper && LOWERCASE.test(character)) {
|
|---|
| 28 | string = string.slice(0, i - 1) + '-' + string.slice(i - 1);
|
|---|
| 29 | isLastLastCharUpper = isLastCharUpper;
|
|---|
| 30 | isLastCharUpper = false;
|
|---|
| 31 | isLastCharLower = true;
|
|---|
| 32 | } else {
|
|---|
| 33 | isLastCharLower = toLowerCase(character) === character && toUpperCase(character) !== character;
|
|---|
| 34 | isLastLastCharUpper = isLastCharUpper;
|
|---|
| 35 | isLastCharUpper = toUpperCase(character) === character && toLowerCase(character) !== character;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | return string;
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | const preserveConsecutiveUppercase = (input, toLowerCase) => {
|
|---|
| 43 | LEADING_CAPITAL.lastIndex = 0;
|
|---|
| 44 |
|
|---|
| 45 | return input.replace(LEADING_CAPITAL, m1 => toLowerCase(m1));
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | const postProcess = (input, toUpperCase) => {
|
|---|
| 49 | SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
|
|---|
| 50 | NUMBERS_AND_IDENTIFIER.lastIndex = 0;
|
|---|
| 51 |
|
|---|
| 52 | return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => toUpperCase(identifier))
|
|---|
| 53 | .replace(NUMBERS_AND_IDENTIFIER, m => toUpperCase(m));
|
|---|
| 54 | };
|
|---|
| 55 |
|
|---|
| 56 | const camelCase = (input, options) => {
|
|---|
| 57 | if (!(typeof input === 'string' || Array.isArray(input))) {
|
|---|
| 58 | throw new TypeError('Expected the input to be `string | string[]`');
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | options = {
|
|---|
| 62 | pascalCase: false,
|
|---|
| 63 | preserveConsecutiveUppercase: false,
|
|---|
| 64 | ...options
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | if (Array.isArray(input)) {
|
|---|
| 68 | input = input.map(x => x.trim())
|
|---|
| 69 | .filter(x => x.length)
|
|---|
| 70 | .join('-');
|
|---|
| 71 | } else {
|
|---|
| 72 | input = input.trim();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (input.length === 0) {
|
|---|
| 76 | return '';
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | const toLowerCase = options.locale === false ?
|
|---|
| 80 | string => string.toLowerCase() :
|
|---|
| 81 | string => string.toLocaleLowerCase(options.locale);
|
|---|
| 82 | const toUpperCase = options.locale === false ?
|
|---|
| 83 | string => string.toUpperCase() :
|
|---|
| 84 | string => string.toLocaleUpperCase(options.locale);
|
|---|
| 85 |
|
|---|
| 86 | if (input.length === 1) {
|
|---|
| 87 | return options.pascalCase ? toUpperCase(input) : toLowerCase(input);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | const hasUpperCase = input !== toLowerCase(input);
|
|---|
| 91 |
|
|---|
| 92 | if (hasUpperCase) {
|
|---|
| 93 | input = preserveCamelCase(input, toLowerCase, toUpperCase);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | input = input.replace(LEADING_SEPARATORS, '');
|
|---|
| 97 |
|
|---|
| 98 | if (options.preserveConsecutiveUppercase) {
|
|---|
| 99 | input = preserveConsecutiveUppercase(input, toLowerCase);
|
|---|
| 100 | } else {
|
|---|
| 101 | input = toLowerCase(input);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (options.pascalCase) {
|
|---|
| 105 | input = toUpperCase(input.charAt(0)) + input.slice(1);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | return postProcess(input, toUpperCase);
|
|---|
| 109 | };
|
|---|
| 110 |
|
|---|
| 111 | module.exports = camelCase;
|
|---|
| 112 | // TODO: Remove this for the next major release
|
|---|
| 113 | module.exports.default = camelCase;
|
|---|