PluginProbe
Hostinger Tools / 3.0.12
Hostinger Tools v3.0.12
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / vue-frontend / src / utils / services / snakeCamelService.ts

snakeCamelService.ts in Hostinger Tools 3.0.12, at vue-frontend/src/utils/services/snakeCamelService.ts

110 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { isObject, isArray } from 'lodash';
2
3 export const snakeToCamel = (string: string, pascal?: boolean) => {
4 const converter = (matches: string) => matches[1]?.toUpperCase();
5
6 let result = string?.toString().replace(/(_\w)/g, converter);
7
8 if (pascal) {
9 result = result?.charAt(0)?.toUpperCase() + result?.slice(1);
10 }
11
12 return result;
13 };
14
15 export const kebabToCamel = (string: string, pascal?: boolean) => {
16 const converter = (matches: string) => matches[1]?.toUpperCase();
17
18 let result = string?.toString().replace(/(-\w)/g, converter);
19
20 if (pascal) {
21 result = result?.charAt(0)?.toUpperCase() + result?.slice(1);
22 }
23
24 return result;
25 };
26 export const snakeToPascal = (string: string) =>
27 string
28 .split('/')
29 .map((snake: string) =>
30 snake
31 .split('_')
32 .map((substr: string) => substr.charAt(0).toUpperCase() + substr.slice(1))
33 .join('')
34 )
35 .join('/');
36
37 export const stringToPascal = (string: string) =>
38 string
39 .replace(/(\w)(\w*)/g, (w: string) => w[0].toUpperCase() + w.slice(1).toLowerCase())
40 .split(' ')
41 .join('')
42 .replace(/(\w)(\w*)/g, (w: string | any[]) => w[0].toLowerCase() + w.slice(1));
43
44 export const camelToSnake = (str: string) =>
45 str
46 .replace(/(^[A-Z])/, ([first]) => first.toLowerCase())
47 .replace(/([A-Z])/g, ([letter]) => `_${letter.toLowerCase()}`);
48
49 export const camelToReadable = (str: string) => camelToSnake(str).replace('_', ' ');
50
51 export const snakeToCamelObj = (obj: any) => {
52 if (isObject(obj) && !isArray(obj)) {
53 const n: { [key: string]: string } = {};
54 Object.keys(obj).forEach((k) => {
55 n[snakeToCamel(k)] = snakeToCamelObj(obj[k as keyof typeof obj]);
56 });
57
58 return n;
59 }
60
61 if (isArray(obj)) {
62 const n: Array<any> = [];
63 obj.forEach((k: string) => n.push(snakeToCamelObj(k)));
64
65 return n;
66 }
67
68 return obj;
69 };
70
71 export const camelToSnakeObj = (obj: any) => {
72 if (isObject(obj) && !isArray(obj)) {
73 const n: { [key: string]: string } = {};
74 Object.keys(obj).forEach((k) => (n[camelToSnake(k)] = camelToSnakeObj(obj[k as keyof typeof obj])));
75
76 return n;
77 }
78
79 if (isArray(obj)) {
80 const n: any[] = [];
81 obj.forEach((k: any) => n.push(camelToSnakeObj(k)));
82
83 return n;
84 }
85
86 return obj;
87 };
88
89 export const kebabToCamelObj = (obj: any) => {
90 if (isObject(obj) && !isArray(obj)) {
91 const n: { [key: string]: string } = {};
92 Object.keys(obj).forEach((k) => {
93 n[kebabToCamel(k)] = kebabToCamelObj(obj[k as keyof typeof obj]);
94 });
95
96 return n;
97 }
98
99 if (isArray(obj)) {
100 const n: any[] = [];
101 obj.forEach((k: any) => n.push(kebabToCamelObj(k)));
102
103 return n;
104 }
105
106 return obj;
107 };
108
109 export const snakeToReadable = (str: string) => str?.replace(/_/g, ' ');
110