rays-javascript-extension/javascript-extensions/array.js

285 lines
5.1 KiB
JavaScript

Array.isEmpty = function(value) {
const dataType = Object.getDataType(value);
if (dataType != "array") {
return true;
}
return (value.length <= 0);
};
Array.toFlatten = function (sourceArray, propName, destArray) {
for (let i=0; i<sourceArray.length; i++) {
destArray.push(sourceArray[i]);
if (typeof(sourceArray[i][propName]) != undefined) {
if (sourceArray[i][propName].length > 0) {
Array.toFlatten(sourceArray[i][propName], propName, destArray);
}
}
}
};
Array.prototype.addRange = function (array) {
if (array == null) {
return this;
}
for (let i = 0; i < array.length; i++) {
this.push(array[i]);
}
return this;
};
Array.prototype.any = function (propName, value) {
return (this.count(propName, value) > 0);
};
Array.prototype.copy = function () {
return JSON.parse(JSON.stringify(this));
};
Array.prototype.count = function (propName, value) {
let result = 0;
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
continue;
}
if (this[i][propName] == value){
result++;
}
}
return result;
};
Array.prototype.countMany = function (...filters) {
let result = 0;
filters.forEach(e => {
result += this.count(e.propName, e.value);
});
return result;
};
Array.prototype.flatten = function (propName) {
let result = [];
Array.toFlatten(this, propName, result);
return result;
};
Array.prototype.index = function (propName, value) {
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
continue;
}
if (this[i][propName] == value){
return i;
}
}
return -1;
};
Array.prototype.insert = function(index, item) {
if (index < 0) {
this.splice(0, 0, item);
} else if (index >= this.length) {
this.push(item);
} else {
this.splice(index, 0, item);
}
return this;
};
Array.prototype.joinIfNotNullOrWhitespace = function (separator) {
const a = this;
let result = "";
for (let i = 0; i < this.length; i++) {
if (String.isNullOrWhitespace(this[i])) {
continue;
}
if (!String.isNullOrWhitespace(result)) {
result += separator;
}
result += this[i];
};
return result;
};
Array.prototype.first = function (propName, value) {
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
continue;
}
if (this[i][propName] == value){
return this[i];
}
}
return null;
};
Array.prototype.forEachTree = function (propName, func) {
for (let i=0; i<this.length; i++) {
if (func(this[i]) === false) {
return;
}
if (this[i][propName].length <= 0) {
continue;
}
if (this[i][propName].forEachTree(propName, func) === false) {
return;
}
}
};
Array.prototype.indexes = function (propName, value) {
let result = [];
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
continue;
}
if (this[i][propName] == value){
result.push(i);
}
}
return result;
};
Array.prototype.orderBy = function (propName) {
this.sort(function(a, b) {
if (a[propName] < b[propName]) {
return -1;
} else if (a[propName] > b[propName]) {
return 1;
} else {
return 0;
}
});
return this;
};
Array.prototype.orderByDesc = function (propName) {
this.sort(function(a, b) {
if (a[propName] < b[propName]) {
return 1;
} else if (a[propName] > b[propName]) {
return -1;
} else {
return 0;
}
});
return this;
};
Array.prototype.remove = function (element) {
let result = [];
for (let i=0; i<this.length; i++) {
if (this[i] == element) {
result.push(i);
}
}
for (let i=(result.length - 1); i>=0; i--) {
this.removeAt(result[i]);
}
return this;
};
Array.prototype.removeAt = function(index) {
if ((index < 0) || (index >= this.length)) {
return this;
}
this.splice(index, 1);
return this;
};
Array.prototype.removeRange = function (array) {
for (let i=0; i<array.length; i++) {
this.remove(array[i]);
}
return this;
};
/**
* Find elements where a property equals a value.
* @returns {Array} Of elements found.
*/
Array.prototype.select = function (propName, value) {
let result = [];
for (let i = 0; i < this.length; i++) {
if (typeof(this[i][propName]) == "undefined") {
continue;
}
if (this[i][propName] == value){
result.push(this[i]);
}
}
return result;
};
Array.prototype.selectMany = function (...filters) {
let result = this;
filters.forEach(e => {
result = result.select(e.propName, e.value);
});
return result;
};
Array.prototype.toList = function (propName) {
let result = [];
this.forEach(e => {
if (typeof(e[propName]) == undefined) {
return;
}
result.push(e[propName]);
});
return result;
};
Array.prototype.sortTree = function (childPropName, sortPropName) {
this.orderBy(sortPropName);
for (let i=0; i<this.length; i++) {
if (this[i][childPropName].length <= 0) {
continue;
}
this[i][childPropName].orderBy(sortPropName);
}
};