Skip to content

Commit cf17411

Browse files
feat: add lodash integration to util functions
1 parent 1b1c90e commit cf17411

File tree

2 files changed

+75
-28
lines changed

2 files changed

+75
-28
lines changed

src/utils/convertValues.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,106 @@
1+
import { camelCase, isEmpty, join, kebabCase, snakeCase, startCase, toArray, toLower, toNumber, toUpper, upperFirst } from "lodash";
2+
13
/**
24
* @description Convert string to lowercase
35
* @param {String} e
46
* @returns {String} Lowercase string
57
*/
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);
79

810
/**
911
* @description Convert string to uppercase
1012
* @param {String} e
1113
* @returns {String} Uppercase string
1214
*/
13-
export const convertStringToUppercase = (e) => (typeof e === "string" && e?.length > 0 ? e.toUpperCase() : e);
15+
export const convertStringToUppercase = (e) => (!isEmpty(e) ? toUpper(e) : e);
1416

1517
/**
1618
* @description Convert string to title case
1719
* @param {String} e
1820
* @returns {String} Title case string
1921
*/
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);
2823

2924
/**
3025
* @description Convert string to camel case
3126
* @param {String} e
3227
* @returns {String} Camel case string
3328
*/
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);
3530

3631
/**
3732
* @description Convert string to snake case
3833
* @param {String} e
3934
* @returns {String} Snake case string
4035
*/
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);
4237

4338
/**
4439
* @description Convert string to kebab case
4540
* @param {String} e
4641
* @returns {String} Kebab case string
4742
*/
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);
5544

5645
/**
5746
* @description Convert string to constant case
5847
* @param {String} e
5948
* @returns {String} Constant case string
6049
*/
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);
6251

6352
/**
6453
* @description Convert string to sentence case
6554
* @param {String} e
6655
* @returns {String} Sentence case string
6756
*/
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);
6958

7059
/**
7160
* @description Convert string to number
7261
* @param {String} e
7362
* @returns {number} Number
7463
*/
75-
export const convertStringToNumber = (e) => (typeof e === "string" && e?.length > 0 ? Number(e) : e);
64+
export const convertStringToNumber = (e) => (!isEmpty(e) ? toNumber(e) : e);
7665

7766
/**
7867
* @description Convert string to boolean
7968
* @param {String} e
8069
* @returns {boolean} Boolean
8170
*/
82-
export const convertStringToBoolean = (e) => (typeof e === "string" && e?.length > 0 ? e === "true" : e);
71+
export const convertStringToBoolean = (e) => (!isEmpty(e) ? e === "true" : e);
8372

8473
/**
8574
* @description Convert string to array
8675
* @param {String} e
8776
* @returns {array} Array
8877
*/
89-
export const convertStringToArray = (e) => (typeof e === "string" && e?.length > 0 ? e.split(",") : e);
78+
export const convertStringToArray = (e) => (!isEmpty(e) ? toArray(e) : e);
9079

9180
/**
9281
* @description Convert string to object
9382
* @param {String} e
9483
* @returns {object} Object
9584
*/
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);
9786

9887
/**
9988
* @description Convert object to string
10089
* @param {object} e
10190
* @returns {String} String
10291
*/
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);
10493

10594
/**
10695
* @description Convert array to string
10796
* @param {array} e
10897
* @returns {String} String
10998
*/
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);
111100

112101
/**
113102
* @description Convert number to string
114103
* @param {number} e
115104
* @returns {String} String
116105
*/
117-
export const convertNumberToString = (e) => (typeof e === "number" ? e.toString() : e);
106+
export const convertNumberToString = (e) => (!isEmpty(e) ? toString(e) : e);

src/utils/typeCheck.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { isArray, isBoolean, isFinite, isNull, isPlainObject, isString, isUndefined, size } from "lodash";
2+
3+
/**
4+
* @description Check if the value is not null or undefined
5+
* @param {Object|Array|String|Number} e
6+
* @returns {Boolean} True if the value is not null or undefined
7+
*/
8+
export const isNotNullOrUndefined = (e) => !isNull(e) && !isUndefined(e);
9+
10+
/**
11+
* @description Check if the given value is a object and not null or undefined
12+
* @param {Object} e
13+
* @returns {Boolean} True if the given value is a object and not null or undefined
14+
*/
15+
export const isObjectType = (e) => isNotNullOrUndefined(e) && isPlainObject(e);
16+
17+
/**
18+
* @description Check if the given value is a array and not null or undefined
19+
* @param {Array} e
20+
* @returns {Boolean} True if the given value is a array and not null or undefined
21+
*/
22+
export const isArrayType = (e) => isNotNullOrUndefined(e) && isArray(e);
23+
24+
/**
25+
* @description Check if the given value is a string and not null or undefined
26+
* @param {String} e
27+
* @returns {Boolean} True if the given value is a string and not null or undefined
28+
*/
29+
export const isStringType = (e) => isNotNullOrUndefined(e) && isString(e);
30+
31+
/**
32+
* @description Check if the given value is a number and not null or undefined
33+
* @param {Number} e
34+
* @returns {Boolean} True if the given value is a number and not null or undefined
35+
*/
36+
export const isNumberType = (e) => isNotNullOrUndefined(e) && isFinite(e);
37+
38+
/**
39+
* @description Check if the given value is a boolean and not null or undefined
40+
* @param {Boolean} e
41+
* @returns {Boolean} True if the given value is a boolean and not null or undefined
42+
*/
43+
export const isBooleanType = (e) => isNotNullOrUndefined(e) && isBoolean(e);
44+
45+
/**
46+
* @description Check if the given valid value is empty
47+
* @param {Object|Array|String|Number} e
48+
* @returns {Boolean} True if the given valid value is empty
49+
*/
50+
export const isEmpty = (e) => {
51+
if (isStringType(e) || isArrayType(e) || isObjectType(e)) {
52+
return size(e) === 0;
53+
} else if (isNumberType(e)) {
54+
return e === 0;
55+
} else {
56+
return true;
57+
}
58+
};

0 commit comments

Comments
 (0)