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, locale) => {
|
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 = character.toLocaleLowerCase(locale) === character && character.toLocaleUpperCase(locale) !== character;
|
34 | isLastLastCharUpper = isLastCharUpper;
|
35 | isLastCharUpper = character.toLocaleUpperCase(locale) === character && character.toLocaleLowerCase(locale) !== character;
|
36 | }
|
37 | }
|
38 |
|
39 | return string;
|
40 | };
|
41 |
|
42 | const preserveConsecutiveUppercase = input => {
|
43 | LEADING_CAPITAL.lastIndex = 0;
|
44 |
|
45 | return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase());
|
46 | };
|
47 |
|
48 | const postProcess = (input, options) => {
|
49 | SEPARATORS_AND_IDENTIFIER.lastIndex = 0;
|
50 | NUMBERS_AND_IDENTIFIER.lastIndex = 0;
|
51 |
|
52 | return input.replace(SEPARATORS_AND_IDENTIFIER, (_, identifier) => identifier.toLocaleUpperCase(options.locale))
|
53 | .replace(NUMBERS_AND_IDENTIFIER, m => m.toLocaleUpperCase(options.locale));
|
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 | if (input.length === 1) {
|
80 | return options.pascalCase ? input.toLocaleUpperCase(options.locale) : input.toLocaleLowerCase(options.locale);
|
81 | }
|
82 |
|
83 | const hasUpperCase = input !== input.toLocaleLowerCase(options.locale);
|
84 |
|
85 | if (hasUpperCase) {
|
86 | input = preserveCamelCase(input, options.locale);
|
87 | }
|
88 |
|
89 | input = input.replace(LEADING_SEPARATORS, '');
|
90 |
|
91 | if (options.preserveConsecutiveUppercase) {
|
92 | input = preserveConsecutiveUppercase(input);
|
93 | } else {
|
94 | input = input.toLocaleLowerCase();
|
95 | }
|
96 |
|
97 | if (options.pascalCase) {
|
98 | input = input.charAt(0).toLocaleUpperCase(options.locale) + input.slice(1);
|
99 | }
|
100 |
|
101 | return postProcess(input, options);
|
102 | };
|
103 |
|
104 | module.exports = camelCase;
|
105 |
|
106 | module.exports.default = camelCase;
|