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) {
|
2018-12-08 22:14:46 +00:00
|
|
|
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
|
|
|
|
2018-12-08 22:14:46 +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;
|
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
return true;
|
2018-12-08 09:15:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
if (arrA != arrB) return false;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
var dateA = a instanceof Date,
|
|
|
|
dateB = b instanceof Date;
|
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
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;
|
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
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
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
length = keys.length;
|
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
if (length !== Object.keys(b).length) return false;
|
2018-12-08 22:14:46 +00:00
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
for (i = length; i-- !== 0; ) if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
|
2018-12-08 22:14:46 +00:00
|
|
|
|
2022-09-14 11:26:01 +00:00
|
|
|
for (i = length; i-- !== 0; ) {
|
2018-12-08 22:14:46 +00:00
|
|
|
key = keys[i];
|
|
|
|
if (!this.deepEquals(a[key], b[key])) return false;
|
2018-12-08 09:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-08 22:14:46 +00:00
|
|
|
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) {
|
2023-09-05 02:12:01 +00:00
|
|
|
if (!data || !field) {
|
|
|
|
// short circuit if there is nothing to resolve
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-09-05 02:08:36 +00:00
|
|
|
try {
|
|
|
|
const value = data[field];
|
2023-09-05 02:12:01 +00:00
|
|
|
|
|
|
|
if (this.isNotEmpty(value)) return value;
|
|
|
|
} catch {
|
|
|
|
// Performance optimization: https://github.com/primefaces/primereact/issues/4797
|
2023-09-05 02:08:36 +00:00
|
|
|
// do nothing and continue to other methods to resolve field data
|
|
|
|
}
|
|
|
|
|
2023-09-05 02:12:01 +00:00
|
|
|
if (Object.keys(data).length) {
|
2020-10-22 09:13:48 +00:00
|
|
|
if (this.isFunction(field)) {
|
|
|
|
return field(data);
|
2022-09-14 11:26:01 +00:00
|
|
|
} else if (field.indexOf('.') === -1) {
|
2020-10-22 09:13:48 +00:00
|
|
|
return data[field];
|
2022-09-14 11:26:01 +00:00
|
|
|
} else {
|
2020-10-22 09:13:48 +00:00
|
|
|
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) {
|
2020-10-22 09:13:48 +00:00
|
|
|
if (value == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2020-10-22 09:13:48 +00:00
|
|
|
value = value[fields[i]];
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2020-10-22 09:13:48 +00:00
|
|
|
return value;
|
2018-12-08 09:15:53 +00:00
|
|
|
}
|
2020-10-22 09:13:48 +00:00
|
|
|
}
|
2023-09-05 02:12:01 +00:00
|
|
|
|
|
|
|
return null;
|
2021-10-02 15:58:31 +00:00
|
|
|
},
|
2020-10-22 09:13:48 +00:00
|
|
|
|
2022-06-14 08:11:34 +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) {
|
2023-01-07 00:22:05 +00:00
|
|
|
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:41:59 +00:00
|
|
|
|
2021-10-02 15:58:31 +00:00
|
|
|
getVNodeProp(vnode, prop) {
|
2021-10-02 15:41:59 +00:00
|
|
|
let props = vnode.props;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2021-10-02 15:41:59 +00:00
|
|
|
if (props) {
|
2023-10-12 08:24:34 +00:00
|
|
|
let kebabProp = prop.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
|
|
let propName = Object.prototype.hasOwnProperty.call(props, kebabProp) ? kebabProp : prop;
|
2021-10-02 15:41:59 +00:00
|
|
|
|
2023-06-02 06:45:56 +00:00
|
|
|
return vnode.type.extends.props[prop].type === Boolean && props[propName] === '' ? true : props[propName];
|
2021-10-02 15:41:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2021-12-05 17:21:16 +00:00
|
|
|
},
|
|
|
|
|
2023-07-10 09:34:48 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2023-07-10 09:34:48 +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;
|
2023-07-10 09:34:48 +00:00
|
|
|
},
|
|
|
|
|
2021-12-05 17:21:16 +00:00
|
|
|
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);
|
2021-12-05 17:21:16 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
isNotEmpty(value) {
|
|
|
|
return !this.isEmpty(value);
|
2022-07-29 00:55:12 +00:00
|
|
|
},
|
2021-10-02 15:41:59 +00:00
|
|
|
|
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 !== '');
|
2023-07-11 22:42:43 +00:00
|
|
|
},
|
|
|
|
|
2022-07-29 00:55:12 +00:00
|
|
|
isPrintableCharacter(char = '') {
|
|
|
|
return this.isNotEmpty(char) && char.length === 1 && char.match(/\S| /);
|
2022-08-23 09:38:43 +00:00
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2022-08-23 09:38:43 +00:00
|
|
|
/**
|
|
|
|
* 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 {
|
2022-08-23 09:38:43 +00:00
|
|
|
index = arr.lastIndexOf([...arr].reverse().find(callback));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2023-08-10 22:29:08 +00:00
|
|
|
},
|
|
|
|
|
2023-10-06 02:00:36 +00:00
|
|
|
sort(value1, value2, order = 1, comparator, nullSortOrder = 1) {
|
|
|
|
const result = this.compare(value1, value2, comparator, order);
|
|
|
|
let finalSortOrder = order;
|
|
|
|
|
|
|
|
// nullSortOrder == 1 means Excel like sort nulls at bottom
|
|
|
|
if (this.isEmpty(value1) || this.isEmpty(value2)) {
|
|
|
|
finalSortOrder = nullSortOrder === 1 ? order : nullSortOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
return finalSortOrder * result;
|
|
|
|
},
|
|
|
|
|
|
|
|
compare(value1, value2, comparator, order = 1) {
|
|
|
|
let result = -1;
|
|
|
|
const emptyValue1 = this.isEmpty(value1);
|
|
|
|
const emptyValue2 = this.isEmpty(value2);
|
|
|
|
|
|
|
|
if (emptyValue1 && emptyValue2) result = 0;
|
|
|
|
else if (emptyValue1) result = order;
|
|
|
|
else if (emptyValue2) result = -order;
|
|
|
|
else if (typeof value1 === 'string' && typeof value2 === 'string') result = comparator(value1, value2);
|
|
|
|
else result = value1 < value2 ? -1 : value1 > value2 ? 1 : 0;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
localeComparator() {
|
|
|
|
//performance gain using Int.Collator. It is not recommended to use localeCompare against large arrays.
|
|
|
|
return new Intl.Collator(undefined, { numeric: true }).compare;
|
|
|
|
},
|
|
|
|
|
2023-08-10 22:29:08 +00:00
|
|
|
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-07-29 00:55:12 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|