source: trip-planner-front/node_modules/camelcase/index.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1'use strict';
2
3const preserveCamelCase = string => {
4 let isLastCharLower = false;
5 let isLastCharUpper = false;
6 let isLastLastCharUpper = false;
7
8 for (let i = 0; i < string.length; i++) {
9 const character = string[i];
10
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;
16 i++;
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;
22 } else {
23 isLastCharLower = character.toLowerCase() === character && character.toUpperCase() !== character;
24 isLastLastCharUpper = isLastCharUpper;
25 isLastCharUpper = character.toUpperCase() === character && character.toLowerCase() !== character;
26 }
27 }
28
29 return string;
30};
31
32const camelCase = (input, options) => {
33 if (!(typeof input === 'string' || Array.isArray(input))) {
34 throw new TypeError('Expected the input to be `string | string[]`');
35 }
36
37 options = Object.assign({
38 pascalCase: false
39 }, options);
40
41 const postProcess = x => options.pascalCase ? x.charAt(0).toUpperCase() + x.slice(1) : x;
42
43 if (Array.isArray(input)) {
44 input = input.map(x => x.trim())
45 .filter(x => x.length)
46 .join('-');
47 } else {
48 input = input.trim();
49 }
50
51 if (input.length === 0) {
52 return '';
53 }
54
55 if (input.length === 1) {
56 return options.pascalCase ? input.toUpperCase() : input.toLowerCase();
57 }
58
59 const hasUpperCase = input !== input.toLowerCase();
60
61 if (hasUpperCase) {
62 input = preserveCamelCase(input);
63 }
64
65 input = input
66 .replace(/^[_.\- ]+/, '')
67 .toLowerCase()
68 .replace(/[_.\- ]+(\w|$)/g, (_, p1) => p1.toUpperCase())
69 .replace(/\d+(\w|$)/g, m => m.toUpperCase());
70
71 return postProcess(input);
72};
73
74module.exports = camelCase;
75// TODO: Remove this for the next major release
76module.exports.default = camelCase;
Note: See TracBrowser for help on using the repository browser.