UNPKG

3.12 kBJavaScriptView Raw
1'use strict';
2
3const UPPERCASE = /[\p{Lu}]/u;
4const LOWERCASE = /[\p{Ll}]/u;
5const LEADING_CAPITAL = /^[\p{Lu}](?![\p{Lu}])/gu;
6const IDENTIFIER = /([\p{Alpha}\p{N}_]|$)/u;
7const SEPARATORS = /[_.\- ]+/;
8
9const LEADING_SEPARATORS = new RegExp('^' + SEPARATORS.source);
10const SEPARATORS_AND_IDENTIFIER = new RegExp(SEPARATORS.source + IDENTIFIER.source, 'gu');
11const NUMBERS_AND_IDENTIFIER = new RegExp('\\d+' + IDENTIFIER.source, 'gu');
12
13const 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
42const preserveConsecutiveUppercase = input => {
43 LEADING_CAPITAL.lastIndex = 0;
44
45 return input.replace(LEADING_CAPITAL, m1 => m1.toLowerCase());
46};
47
48const 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
56const 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
104module.exports = camelCase;
105// TODO: Remove this for the next major release
106module.exports.default = camelCase;