PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.5.9
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.5.9
1.7.1 1.7.0 1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / frontend / vue / utils / caseConversion.ts
hostinger-reach / frontend / vue / utils Last commit date
helpers 9 months ago services 10 months ago caseConversion.ts 11 months ago translate.ts 11 months ago
caseConversion.ts
69 lines
1 const isObject = (obj: unknown): boolean =>
2 obj !== null && typeof obj === 'object' && !Array.isArray(obj) && !(obj instanceof Date);
3
4 const toCamelCase = (str: string): string =>
5 str.replace(/([-_][a-z])/g, (group) => group.toUpperCase().replace('-', '').replace('_', ''));
6
7 const toSnakeCase = (str: string): string => str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
8
9 const toKebabCase = (str: string): string =>
10 str
11 .replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
12 .replace(/[\s_]+/g, '-')
13 .replace(/^-+|-+$/g, '')
14 .toLowerCase();
15
16 export const camelToSnakeObj = (obj: unknown): unknown => {
17 if (!isObject(obj)) return obj;
18
19 const result: Record<string, unknown> = {};
20
21 for (const key in obj as Record<string, unknown>) {
22 if (Object.prototype.hasOwnProperty.call(obj, key)) {
23 const snakeKey = toSnakeCase(key);
24 const value = (obj as Record<string, unknown>)[key];
25
26 if (isObject(value)) {
27 result[snakeKey] = camelToSnakeObj(value);
28 } else if (Array.isArray(value)) {
29 result[snakeKey] = value.map((item) => (isObject(item) ? camelToSnakeObj(item) : item));
30 } else {
31 result[snakeKey] = value;
32 }
33 }
34 }
35
36 return result;
37 };
38
39 export const snakeToCamelObj = <T>(obj: unknown): T => {
40 if (obj === null || typeof obj !== 'object' || obj instanceof Date) {
41 return obj as T;
42 }
43
44 if (Array.isArray(obj)) {
45 return obj.map(snakeToCamelObj) as unknown as T;
46 }
47
48 const result: Record<string, unknown> = {};
49
50 for (const key in obj as Record<string, unknown>) {
51 if (Object.prototype.hasOwnProperty.call(obj, key)) {
52 const camelKey = toCamelCase(key);
53 const value = (obj as Record<string, unknown>)[key];
54
55 if (isObject(value)) {
56 result[camelKey] = snakeToCamelObj(value);
57 } else if (Array.isArray(value)) {
58 result[camelKey] = value.map((item) => (isObject(item) ? snakeToCamelObj(item) : item));
59 } else {
60 result[camelKey] = value;
61 }
62 }
63 }
64
65 return result as T;
66 };
67
68 export { toKebabCase };
69