Added doc comments for array

This commit is contained in:
Ray 2024-08-28 20:16:58 +01:00
parent e3ad9348b5
commit 1d9c6bd146

View File

@ -1,3 +1,8 @@
/**
* Check is array empty.
* @param {Array} value Array.
* @returns Boolean True, if array is empty or not an array type.
*/
Array.isEmpty = function(value) { Array.isEmpty = function(value) {
const dataType = Object.getDataType(value); const dataType = Object.getDataType(value);
if (dataType != "array") { if (dataType != "array") {
@ -7,6 +12,12 @@ Array.isEmpty = function(value) {
return (value.length <= 0); return (value.length <= 0);
}; };
/**
* Create a linear/one-dimension array of objects from an object's property.
* @param {*} sourceArray Source array.
* @param {*} propName Property name.
* @param {*} destArray Destination array.
*/
Array.toFlatten = function (sourceArray, propName, destArray) { Array.toFlatten = function (sourceArray, propName, destArray) {
for (let i=0; i<sourceArray.length; i++) { for (let i=0; i<sourceArray.length; i++) {
destArray.push(sourceArray[i]); destArray.push(sourceArray[i]);
@ -36,6 +47,12 @@ Array.prototype.addRange = function (array) {
return this; return this;
}; };
/**
* Check all match property value.
* @param {String} propName Property name.
* @param {Object} value Property value.
* @returns Boolean True, if all objects of property name match value.
*/
Array.prototype.all = function (propName, value) { Array.prototype.all = function (propName, value) {
for (let i = 0; i < this.length; i++) { for (let i = 0; i < this.length; i++) {
if (propName == null) { if (propName == null) {
@ -54,6 +71,12 @@ Array.prototype.all = function (propName, value) {
return true; return true;
}; };
/**
* Check any match property value.
* @param {String} propName Property name.
* @param {Object} value Property value.
* @returns Boolean True, if any objects of property name match value.
*/
Array.prototype.any = function (propName, value) { Array.prototype.any = function (propName, value) {
for (let i = 0; i < this.length; i++) { for (let i = 0; i < this.length; i++) {
if (propName == null) { if (propName == null) {
@ -72,10 +95,20 @@ Array.prototype.any = function (propName, value) {
return false; return false;
}; };
/**
* Create deep copy of array.
* @returns Array
*/
Array.prototype.copy = function () { Array.prototype.copy = function () {
return JSON.parse(JSON.stringify(this)); return JSON.parse(JSON.stringify(this));
}; };
/**
* Count of objects in array with the property value.
* @param {*} propName Property name.
* @param {*} value Property value.
* @returns Integer Number of matches.
*/
Array.prototype.count = function (propName, value) { Array.prototype.count = function (propName, value) {
let result = 0; let result = 0;
@ -96,6 +129,11 @@ Array.prototype.count = function (propName, value) {
return result; return result;
}; };
/**
* Count of objects in array with many matches.
* @param {Object} filters Array of matches [{propName, value}].
* @returns Integer Number of matches.
*/
Array.prototype.countMany = function (...filters) { Array.prototype.countMany = function (...filters) {
let result = 0; let result = 0;
@ -122,6 +160,11 @@ Array.prototype.create = function (length, value) {
return result return result
}; };
/**
* Convert this array of objects to a linear/one-dimensional array using a property.
* @param {*} propName Property name.
* @returns Array Array.
*/
Array.prototype.flatten = function (propName) { Array.prototype.flatten = function (propName) {
let result = []; let result = [];
@ -130,6 +173,23 @@ Array.prototype.flatten = function (propName) {
return result; return result;
}; };
/**
* Get element at index.
* @param {Object} index Index of element.
* @returns Object Element.
*/
Array.prototype.get = function (index) {
if (index < 0) {
return "";
}
if (index >= this.length) {
return "";
}
return this[index];
};
/** /**
* Find index of first occurrence of value in property. * Find index of first occurrence of value in property.
* @param {string} propName Property name. * @param {string} propName Property name.