primevue-mirror/components/lib/utils/ObjectUtils.js

301 lines
8.7 KiB
JavaScript
Raw Normal View History

2021-10-02 15:58:31 +00:00
export default {
equals(obj1, obj2, field) {
2022-09-14 11:26:01 +00:00
if (field) return this.resolveFieldData(obj1, field) === this.resolveFieldData(obj2, field);
else return this.deepEquals(obj1, obj2);
2021-10-02 15:58:31 +00:00
},
2018-12-08 09:15:53 +00:00
2021-10-02 15:58:31 +00:00
deepEquals(a, b) {
if (a === b) return true;
if (a && b && typeof a == 'object' && typeof b == 'object') {
2022-09-14 11:26:01 +00:00
var arrA = Array.isArray(a),
arrB = Array.isArray(b),
i,
length,
key;
2018-12-08 09:15:53 +00:00
if (arrA && arrB) {
length = a.length;
if (length != b.length) return false;
2022-09-14 11:26:01 +00:00
for (i = length; i-- !== 0; ) if (!this.deepEquals(a[i], b[i])) return false;
return true;
2018-12-08 09:15:53 +00:00
}
if (arrA != arrB) return false;
2022-09-14 11:26:01 +00:00
var dateA = a instanceof Date,
dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();
2022-09-14 11:26:01 +00:00
var regexpA = a instanceof RegExp,
regexpB = b instanceof RegExp;
if (regexpA != regexpB) return false;
if (regexpA && regexpB) return a.toString() == b.toString();
var keys = Object.keys(a);
2022-09-14 11:26:01 +00:00
length = keys.length;
2022-09-14 11:26:01 +00:00
if (length !== Object.keys(b).length) return false;
2022-09-14 11:26:01 +00:00
for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
2022-09-14 11:26:01 +00:00
for (i = length; i-- !== 0; ) {
key = keys[i];
if (!this.deepEquals(a[key], b[key])) return false;
2018-12-08 09:15:53 +00:00
}
return true;
}
return a !== a && b !== b;
2021-10-02 15:58:31 +00:00
},
2019-05-29 07:11:07 +00:00
2021-10-02 15:58:31 +00:00
resolveFieldData(data, field) {
2020-11-25 14:52:03 +00:00
if (data && Object.keys(data).length && field) {
if (this.isFunction(field)) {
return field(data);
2022-09-14 11:26:01 +00:00
} else if (field.indexOf('.') === -1) {
return data[field];
2022-09-14 11:26:01 +00:00
} else {
let fields = field.split('.');
let value = data;
2022-09-14 11:26:01 +00:00
for (var i = 0, len = fields.length; i < len; ++i) {
if (value == null) {
return null;
}
2022-09-14 11:26:01 +00:00
value = value[fields[i]];
}
2022-09-14 11:26:01 +00:00
return value;
2018-12-08 09:15:53 +00:00
}
2022-09-14 11:26:01 +00:00
} else {
return null;
}
2021-10-02 15:58:31 +00:00
},
getItemValue(obj, ...params) {
return this.isFunction(obj) ? obj(...params) : obj;
},
2021-10-02 15:58:31 +00:00
filter(value, fields, filterValue) {
2019-05-29 07:11:07 +00:00
var filteredItems = [];
2018-12-08 09:15:53 +00:00
2019-05-29 07:11:07 +00:00
if (value) {
for (let item of value) {
for (let field of fields) {
if (String(this.resolveFieldData(item, field)).toLowerCase().indexOf(filterValue.toLowerCase()) > -1) {
2018-12-08 09:15:53 +00:00
filteredItems.push(item);
break;
}
}
}
}
return filteredItems;
2021-10-02 15:58:31 +00:00
},
2018-12-08 09:15:53 +00:00
2021-10-02 15:58:31 +00:00
reorderArray(value, from, to) {
2022-09-14 11:26:01 +00:00
if (value && from !== to) {
2019-05-29 07:11:07 +00:00
if (to >= value.length) {
to %= value.length;
from %= value.length;
2018-12-08 09:15:53 +00:00
}
2022-09-14 11:26:01 +00:00
2018-12-08 09:15:53 +00:00
value.splice(to, 0, value.splice(from, 1)[0]);
}
2021-10-02 15:58:31 +00:00
},
2019-05-29 07:11:07 +00:00
2021-10-02 15:58:31 +00:00
findIndexInList(value, list) {
2018-12-08 09:15:53 +00:00
let index = -1;
2019-05-29 07:11:07 +00:00
if (list) {
for (let i = 0; i < list.length; i++) {
if (list[i] === value) {
2018-12-08 09:15:53 +00:00
index = i;
break;
}
}
}
2019-05-29 07:11:07 +00:00
2018-12-08 09:15:53 +00:00
return index;
2021-10-02 15:58:31 +00:00
},
2018-12-08 09:15:53 +00:00
2021-10-02 15:58:31 +00:00
contains(value, list) {
2018-12-14 15:35:42 +00:00
if (value != null && list && list.length) {
for (let val of list) {
2022-09-14 11:26:01 +00:00
if (this.equals(value, val)) return true;
2018-12-14 15:35:42 +00:00
}
}
return false;
2021-10-02 15:58:31 +00:00
},
2019-07-16 12:47:00 +00:00
2021-10-02 15:58:31 +00:00
insertIntoOrderedArray(item, index, arr, sourceArr) {
2019-07-16 12:47:00 +00:00
if (arr.length > 0) {
let injected = false;
2022-09-14 11:26:01 +00:00
2019-07-16 12:47:00 +00:00
for (let i = 0; i < arr.length; i++) {
let currentItemIndex = this.findIndexInList(arr[i], sourceArr);
2022-09-14 11:26:01 +00:00
2019-07-16 12:47:00 +00:00
if (currentItemIndex > index) {
arr.splice(i, 0, item);
injected = true;
break;
}
}
if (!injected) {
arr.push(item);
}
2022-09-14 11:26:01 +00:00
} else {
2019-07-16 12:47:00 +00:00
arr.push(item);
}
2021-10-02 15:58:31 +00:00
},
2020-03-04 06:41:10 +00:00
2021-10-02 15:58:31 +00:00
removeAccents(str) {
2020-03-04 06:41:10 +00:00
if (str && str.search(/[\xC0-\xFF]/g) > -1) {
str = str
2022-09-14 11:26:01 +00:00
.replace(/[\xC0-\xC5]/g, 'A')
.replace(/[\xC6]/g, 'AE')
.replace(/[\xC7]/g, 'C')
.replace(/[\xC8-\xCB]/g, 'E')
.replace(/[\xCC-\xCF]/g, 'I')
.replace(/[\xD0]/g, 'D')
.replace(/[\xD1]/g, 'N')
.replace(/[\xD2-\xD6\xD8]/g, 'O')
.replace(/[\xD9-\xDC]/g, 'U')
.replace(/[\xDD]/g, 'Y')
.replace(/[\xDE]/g, 'P')
.replace(/[\xE0-\xE5]/g, 'a')
.replace(/[\xE6]/g, 'ae')
.replace(/[\xE7]/g, 'c')
.replace(/[\xE8-\xEB]/g, 'e')
.replace(/[\xEC-\xEF]/g, 'i')
.replace(/[\xF1]/g, 'n')
.replace(/[\xF2-\xF6\xF8]/g, 'o')
.replace(/[\xF9-\xFC]/g, 'u')
.replace(/[\xFE]/g, 'p')
.replace(/[\xFD\xFF]/g, 'y');
2020-03-04 06:41:10 +00:00
}
return str;
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
getVNodeProp(vnode, prop) {
let props = vnode.props;
2022-09-14 11:26:01 +00:00
if (props) {
let kebapProp = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
let propName = Object.prototype.hasOwnProperty.call(props, kebapProp) ? kebapProp : prop;
return vnode.type.extends.props[prop].type === Boolean && props[propName] === '' ? true : props[propName];
}
return null;
},
toFlatCase(str) {
2023-04-03 00:12:25 +00:00
// convert snake, kebab, camel and pascal cases to flat case
2023-07-13 21:34:23 +00:00
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;
2023-04-03 00:12:25 +00:00
},
toCapitalCase(str) {
2023-07-13 21:34:23 +00:00
return this.isString(str, { empty: false }) ? str[0].toUpperCase() + str.slice(1) : str;
},
isEmpty(value) {
2022-09-14 11:26:01 +00:00
return value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0) || (!(value instanceof Date) && typeof value === 'object' && Object.keys(value).length === 0);
},
isNotEmpty(value) {
return !this.isEmpty(value);
},
2023-05-23 23:40:14 +00:00
isFunction(value) {
return !!(value && value.constructor && value.call && value.apply);
},
2023-07-13 21:34:23 +00:00
isObject(value, empty = true) {
return value instanceof Object && value.constructor === Object && (empty || Object.keys(value).length !== 0);
2023-05-23 23:40:14 +00:00
},
isDate(value) {
2023-07-13 21:34:23 +00:00
return value instanceof Date && value.constructor === Date;
2023-05-23 23:40:14 +00:00
},
2023-07-13 21:34:23 +00:00
isArray(value, empty = true) {
return Array.isArray(value) && (empty || value.length !== 0);
2023-05-23 23:40:14 +00:00
},
2023-07-13 21:34:23 +00:00
isString(value, empty = true) {
return typeof value === 'string' && (empty || value !== '');
},
isPrintableCharacter(char = '') {
return this.isNotEmpty(char) && char.length === 1 && char.match(/\S| /);
},
2022-12-08 11:04:25 +00:00
/**
* Firefox-v103 does not currently support the "findLast" method. It is stated that this method will be supported with Firefox-v104.
* https://caniuse.com/mdn-javascript_builtins_array_findlast
*/
findLast(arr, callback) {
let item;
if (this.isNotEmpty(arr)) {
try {
item = arr.findLast(callback);
} catch {
item = [...arr].reverse().find(callback);
}
}
return item;
},
/**
* Firefox-v103 does not currently support the "findLastIndex" method. It is stated that this method will be supported with Firefox-v104.
* https://caniuse.com/mdn-javascript_builtins_array_findlastindex
*/
findLastIndex(arr, callback) {
let index = -1;
if (this.isNotEmpty(arr)) {
try {
index = arr.findLastIndex(callback);
2022-09-14 11:26:01 +00:00
} catch {
index = arr.lastIndexOf([...arr].reverse().find(callback));
}
}
return index;
},
nestedKeys(obj = {}, parentKey = '') {
return Object.entries(obj).reduce((o, [key, value]) => {
const currentKey = parentKey ? `${parentKey}.${key}` : key;
this.isObject(value) ? (o = o.concat(this.nestedKeys(value, currentKey))) : o.push(currentKey);
return o;
}, []);
}
2022-09-14 11:26:01 +00:00
};