blob: 6e81ba08a0f6f7f0e71986201cca45fc7ba8b7e8 [file] [log] [blame]
Tim van der Lippe6d109a92021-02-16 16:00:32 +00001export function camelCase(str) {
2 str = str.toLocaleLowerCase();
3 if (str.indexOf('-') === -1 && str.indexOf('_') === -1) {
4 return str;
5 }
6 else {
7 let camelcase = '';
8 let nextChrUpper = false;
9 const leadingHyphens = str.match(/^-+/);
10 for (let i = leadingHyphens ? leadingHyphens[0].length : 0; i < str.length; i++) {
11 let chr = str.charAt(i);
12 if (nextChrUpper) {
13 nextChrUpper = false;
14 chr = chr.toLocaleUpperCase();
15 }
16 if (i !== 0 && (chr === '-' || chr === '_')) {
17 nextChrUpper = true;
18 continue;
19 }
20 else if (chr !== '-' && chr !== '_') {
21 camelcase += chr;
22 }
23 }
24 return camelcase;
25 }
26}
27export function decamelize(str, joinString) {
28 const lowercase = str.toLocaleLowerCase();
29 joinString = joinString || '-';
30 let notCamelcase = '';
31 for (let i = 0; i < str.length; i++) {
32 const chrLower = lowercase.charAt(i);
33 const chrString = str.charAt(i);
34 if (chrLower !== chrString && i > 0) {
35 notCamelcase += `${joinString}${lowercase.charAt(i)}`;
36 }
37 else {
38 notCamelcase += chrString;
39 }
40 }
41 return notCamelcase;
42}
43export function looksLikeNumber(x) {
44 if (x === null || x === undefined)
45 return false;
46 // if loaded from config, may already be a number.
47 if (typeof x === 'number')
48 return true;
49 // hexadecimal.
50 if (/^0x[0-9a-f]+$/i.test(x))
51 return true;
52 // don't treat 0123 as a number; as it drops the leading '0'.
53 if (x.length > 1 && x[0] === '0')
54 return false;
55 return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x);
56}