primevue-mirror/components/utils/ObjectUtils.js

246 lines
7.2 KiB
JavaScript
Raw Normal View History

2021-10-02 15:58:31 +00:00
export default {
2019-05-29 07:11:07 +00:00
2021-10-02 15:58:31 +00:00
equals(obj1, obj2, field) {
2019-05-29 07:11:07 +00:00
if (field)
2018-12-08 09:15:53 +00:00
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') {
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;
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;
var dateA = a instanceof Date
, dateB = b instanceof Date;
if (dateA != dateB) return false;
if (dateA && dateB) return a.getTime() == b.getTime();
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);
length = keys.length;
if (length !== Object.keys(b).length)
return false;
for (i = length; i-- !== 0;)
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
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);
}
else if(field.indexOf('.') === -1) {
return data[field];
2018-12-08 09:15:53 +00:00
}
else {
let fields = field.split('.');
let value = data;
for(var i = 0, len = fields.length; i < len; ++i) {
if (value == null) {
return null;
}
value = value[fields[i]];
}
return value;
2018-12-08 09:15:53 +00:00
}
}
else {
return null;
}
2021-10-02 15:58:31 +00:00
},
2021-10-02 15:58:31 +00:00
isFunction(obj) {
return !!(obj && obj.constructor && obj.call && obj.apply);
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) {
2018-12-08 09:15:53 +00:00
let target;
2019-05-29 07:11:07 +00:00
if (value && (from !== to)) {
if (to >= value.length) {
2018-12-08 09:15:53 +00:00
target = to - value.length;
2019-05-29 07:11:07 +00:00
while ((target--) + 1) {
2018-12-08 09:15:53 +00:00
value.push(undefined);
}
}
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) {
if (this.equals(value, val))
return true;
}
}
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;
for (let i = 0; i < arr.length; i++) {
let currentItemIndex = this.findIndexInList(arr[i], sourceArr);
if (currentItemIndex > index) {
arr.splice(i, 0, item);
injected = true;
break;
}
}
if (!injected) {
arr.push(item);
}
}
else {
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
.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");
}
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;
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.props[prop].type === Boolean && props[propName] === '') ? true : props[propName]);
}
return null;
},
isEmpty(value) {
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);
},
isPrintableCharacter(char = '') {
return this.isNotEmpty(char) && char.length === 1 && char.match(/\S| /);
},
/**
* 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);
}
catch {
index = arr.lastIndexOf([...arr].reverse().find(callback));
}
}
return index;
}
}