Improve ObjectUtils methods

pull/4148/head
mertsincan 2023-07-13 22:34:23 +01:00
parent e0b97b693d
commit bf2cedf8da
1 changed files with 19 additions and 9 deletions

View File

@ -203,11 +203,21 @@ export default {
toFlatCase(str) { toFlatCase(str) {
// convert snake, kebab, camel and pascal cases to flat case // convert snake, kebab, camel and pascal cases to flat case
return this.isNotEmpty(str) && this.isString(str) ? str.replace(/(-|_)/g, '').toLowerCase() : str; return this.isString(str) ? str.replace(/(-|_)/g, '').toLowerCase() : str;
},
toKebabCase(str) {
// convert snake, camel and pascal cases to kebab case
return this.isString(str)
? str
.replace(/(_)/g, '-')
.replace(/[A-Z]/g, (c, i) => (i === 0 ? c : '-' + c.toLowerCase()))
.toLowerCase()
: str;
}, },
toCapitalCase(str) { toCapitalCase(str) {
return this.isNotEmpty(str) && this.isString(str) ? str[0].toUpperCase() + str.slice(1) : str; return this.isString(str, { empty: false }) ? str[0].toUpperCase() + str.slice(1) : str;
}, },
isEmpty(value) { isEmpty(value) {
@ -222,20 +232,20 @@ export default {
return !!(value && value.constructor && value.call && value.apply); return !!(value && value.constructor && value.call && value.apply);
}, },
isObject(value) { isObject(value, empty = true) {
return value !== null && value instanceof Object && value.constructor === Object; return value instanceof Object && value.constructor === Object && (empty || Object.keys(value).length !== 0);
}, },
isDate(value) { isDate(value) {
return value !== null && value instanceof Date && value.constructor === Date; return value instanceof Date && value.constructor === Date;
}, },
isArray(value) { isArray(value, empty = true) {
return value !== null && Array.isArray(value); return Array.isArray(value) && (empty || value.length !== 0);
}, },
isString(value) { isString(value, empty = true) {
return value !== null && typeof value === 'string'; return typeof value === 'string' && (empty || value !== '');
}, },
isPrintableCharacter(char = '') { isPrintableCharacter(char = '') {