|
| 1 | +import { camelCase, isEmpty, join, kebabCase, snakeCase, startCase, toArray, toLower, toNumber, toUpper, upperFirst } from "lodash"; |
| 2 | + |
1 | 3 | /** |
2 | 4 | * @description Convert string to lowercase |
3 | 5 | * @param {String} e |
4 | 6 | * @returns {String} Lowercase string |
5 | 7 | */ |
6 | | -export const convertStringToLowercase = (e) => (typeof e === "string" && e?.length > 0 && e?.length > 0 ? e.toLowerCase() : e); |
| 8 | +export const convertStringToLowercase = (e) => (!isEmpty(e) ? toLower(e) : e); |
7 | 9 |
|
8 | 10 | /** |
9 | 11 | * @description Convert string to uppercase |
10 | 12 | * @param {String} e |
11 | 13 | * @returns {String} Uppercase string |
12 | 14 | */ |
13 | | -export const convertStringToUppercase = (e) => (typeof e === "string" && e?.length > 0 ? e.toUpperCase() : e); |
| 15 | +export const convertStringToUppercase = (e) => (!isEmpty(e) ? toUpper(e) : e); |
14 | 16 |
|
15 | 17 | /** |
16 | 18 | * @description Convert string to title case |
17 | 19 | * @param {String} e |
18 | 20 | * @returns {String} Title case string |
19 | 21 | */ |
20 | | -export const convertStringToTitleCase = (e) => |
21 | | - typeof e === "string" && e?.length > 0 |
22 | | - ? (e = e |
23 | | - .toLowerCase() |
24 | | - .split(" ") |
25 | | - .map((e) => e.charAt(0).toUpperCase() + e.slice(1)) |
26 | | - .join(" ")) |
27 | | - : e; |
| 22 | +export const convertStringToTitleCase = (e) => (!isEmpty(e) ? startCase(camelCase(e)) : e); |
28 | 23 |
|
29 | 24 | /** |
30 | 25 | * @description Convert string to camel case |
31 | 26 | * @param {String} e |
32 | 27 | * @returns {String} Camel case string |
33 | 28 | */ |
34 | | -export const convertStringToCamelCase = (e) => (typeof e === "string" && e?.length > 0 ? e.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase()) : e); |
| 29 | +export const convertStringToCamelCase = (e) => (!isEmpty(e) ? camelCase(e) : e); |
35 | 30 |
|
36 | 31 | /** |
37 | 32 | * @description Convert string to snake case |
38 | 33 | * @param {String} e |
39 | 34 | * @returns {String} Snake case string |
40 | 35 | */ |
41 | | -export const convertStringToSnakeCase = (e) => (typeof e === "string" && e?.length > 0 ? e.replace(/\s/g, "_").toLowerCase() : e); |
| 36 | +export const convertStringToSnakeCase = (e) => (!isEmpty(e) ? snakeCase(e) : e); |
42 | 37 |
|
43 | 38 | /** |
44 | 39 | * @description Convert string to kebab case |
45 | 40 | * @param {String} e |
46 | 41 | * @returns {String} Kebab case string |
47 | 42 | */ |
48 | | -export const convertStringToKebabCase = (e) => |
49 | | - typeof e === "string" && e?.length > 0 |
50 | | - ? e |
51 | | - .match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) |
52 | | - .map((x) => x.toLowerCase()) |
53 | | - .join("-") |
54 | | - : e; |
| 43 | +export const convertStringToKebabCase = (e) => (!isEmpty(e) ? kebabCase(e) : e); |
55 | 44 |
|
56 | 45 | /** |
57 | 46 | * @description Convert string to constant case |
58 | 47 | * @param {String} e |
59 | 48 | * @returns {String} Constant case string |
60 | 49 | */ |
61 | | -export const convertStringToConstantCase = (e) => (typeof e === "string" && e?.length > 0 ? e.replace(/\s/g, "_").toUpperCase() : e); |
| 50 | +export const convertStringToConstantCase = (e) => (!isEmpty(e) ? toUpper(e).replace(/ /g, "_") : e); |
62 | 51 |
|
63 | 52 | /** |
64 | 53 | * @description Convert string to sentence case |
65 | 54 | * @param {String} e |
66 | 55 | * @returns {String} Sentence case string |
67 | 56 | */ |
68 | | -export const convertStringToSentenceCase = (e) => (typeof e === "string" && e?.length > 0 ? e.replace(/\s/g, " ").replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()) : e); |
| 57 | +export const convertStringToSentenceCase = (e) => (!isEmpty(e) ? upperFirst(toLower(e)) : e); |
69 | 58 |
|
70 | 59 | /** |
71 | 60 | * @description Convert string to number |
72 | 61 | * @param {String} e |
73 | 62 | * @returns {number} Number |
74 | 63 | */ |
75 | | -export const convertStringToNumber = (e) => (typeof e === "string" && e?.length > 0 ? Number(e) : e); |
| 64 | +export const convertStringToNumber = (e) => (!isEmpty(e) ? toNumber(e) : e); |
76 | 65 |
|
77 | 66 | /** |
78 | 67 | * @description Convert string to boolean |
79 | 68 | * @param {String} e |
80 | 69 | * @returns {boolean} Boolean |
81 | 70 | */ |
82 | | -export const convertStringToBoolean = (e) => (typeof e === "string" && e?.length > 0 ? e === "true" : e); |
| 71 | +export const convertStringToBoolean = (e) => (!isEmpty(e) ? e === "true" : e); |
83 | 72 |
|
84 | 73 | /** |
85 | 74 | * @description Convert string to array |
86 | 75 | * @param {String} e |
87 | 76 | * @returns {array} Array |
88 | 77 | */ |
89 | | -export const convertStringToArray = (e) => (typeof e === "string" && e?.length > 0 ? e.split(",") : e); |
| 78 | +export const convertStringToArray = (e) => (!isEmpty(e) ? toArray(e) : e); |
90 | 79 |
|
91 | 80 | /** |
92 | 81 | * @description Convert string to object |
93 | 82 | * @param {String} e |
94 | 83 | * @returns {object} Object |
95 | 84 | */ |
96 | | -export const convertStringToObject = (e) => (typeof e === "string" && e?.length > 0 ? JSON.parse(e) : e); |
| 85 | +export const convertStringToObject = (e) => (!isEmpty(e) ? JSON.parse(e) : e); |
97 | 86 |
|
98 | 87 | /** |
99 | 88 | * @description Convert object to string |
100 | 89 | * @param {object} e |
101 | 90 | * @returns {String} String |
102 | 91 | */ |
103 | | -export const convertObjectToString = (e) => (e && Object.prototype.toString.call(e) === "[object Object]" && Object.keys(e)?.length > 0 ? JSON.stringify(e) : e); |
| 92 | +export const convertObjectToString = (e) => (!isEmpty(e) ? JSON.stringify(e) : e); |
104 | 93 |
|
105 | 94 | /** |
106 | 95 | * @description Convert array to string |
107 | 96 | * @param {array} e |
108 | 97 | * @returns {String} String |
109 | 98 | */ |
110 | | -export const convertArrayToString = (e) => (e && Object.prototype.toString.call(e) === "[object Object]" && Object.keys(e)?.length > 0 ? e.join(" ") : e); |
| 99 | +export const convertArrayToString = (e) => (!isEmpty(e) ? join(e, " ") : e); |
111 | 100 |
|
112 | 101 | /** |
113 | 102 | * @description Convert number to string |
114 | 103 | * @param {number} e |
115 | 104 | * @returns {String} String |
116 | 105 | */ |
117 | | -export const convertNumberToString = (e) => (typeof e === "number" ? e.toString() : e); |
| 106 | +export const convertNumberToString = (e) => (!isEmpty(e) ? toString(e) : e); |
0 commit comments