neo.mjs 4.6.5 → 4.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/collection/Base.mjs +32 -17
- package/src/manager/Instance.mjs +3 -1
package/package.json
CHANGED
package/src/collection/Base.mjs
CHANGED
@@ -743,23 +743,26 @@ class Base extends CoreBase {
|
|
743
743
|
}
|
744
744
|
|
745
745
|
/**
|
746
|
-
* Returns
|
746
|
+
* Returns items which match the property and value
|
747
747
|
* @param {Object|String} property
|
748
|
-
* @param {String|Number} [value]
|
749
|
-
* @
|
748
|
+
* @param {String|Number} [value] Only required in case the first param is a string
|
749
|
+
* @param {Boolean} returnFirstMatch=false
|
750
|
+
* @returns {Object|Object[]}
|
751
|
+
* returnFirstMatch=false: Returns an empty Array in case no items are found
|
752
|
+
* returnFirstMatch=true: Returns the first found item or null
|
750
753
|
*/
|
751
|
-
find(property, value) {
|
754
|
+
find(property, value, returnFirstMatch=false) {
|
752
755
|
let me = this,
|
753
756
|
items = [],
|
754
757
|
isObjectProperty = Neo.isObject(property),
|
755
|
-
matchArray, propertiesArray, propertiesLength;
|
758
|
+
item, matchArray, propertiesArray, propertiesLength;
|
756
759
|
|
757
760
|
if (isObjectProperty) {
|
758
761
|
propertiesArray = Object.entries(property);
|
759
762
|
propertiesLength = propertiesArray.length;
|
760
763
|
}
|
761
764
|
|
762
|
-
me.items
|
765
|
+
for (item of me.items) {
|
763
766
|
if (isObjectProperty) {
|
764
767
|
matchArray = [];
|
765
768
|
|
@@ -770,33 +773,35 @@ class Base extends CoreBase {
|
|
770
773
|
});
|
771
774
|
|
772
775
|
if (matchArray.length === propertiesLength) {
|
776
|
+
if (returnFirstMatch) {
|
777
|
+
return item;
|
778
|
+
}
|
779
|
+
|
773
780
|
items.push(item);
|
774
781
|
}
|
775
|
-
}
|
776
|
-
else if (item[property] === value) {
|
782
|
+
} else if (item[property] === value) {
|
777
783
|
items.push(item);
|
778
784
|
}
|
779
|
-
}
|
785
|
+
}
|
780
786
|
|
781
|
-
return items;
|
787
|
+
return returnFirstMatch ? null : items;
|
782
788
|
}
|
783
789
|
|
784
790
|
/**
|
785
791
|
* Returns all items in the collection for which the passed function returns true
|
786
792
|
* @param {function} fn The function to run for each item inside the start-end range. Return true for a match.
|
787
793
|
* @param {Object} fn.item The current collection item
|
788
|
-
* @param {Object}
|
789
|
-
* @param {Number}
|
790
|
-
* @param {Number}
|
794
|
+
* @param {Object} scope=this The scope in which the passed function gets executed
|
795
|
+
* @param {Number} start=0 The start index
|
796
|
+
* @param {Number} end=this.getCount() The end index (up to, last value excluded)
|
791
797
|
* @returns {Array} Returns an empty Array in case no items are found
|
792
798
|
*/
|
793
|
-
findBy(fn, scope=this, start, end) {
|
799
|
+
findBy(fn, scope=this, start=0, end=this.getCount()) {
|
794
800
|
let me = this,
|
795
801
|
items = [],
|
796
|
-
i = start
|
797
|
-
len = end || me.getCount();
|
802
|
+
i = start;
|
798
803
|
|
799
|
-
for (; i <
|
804
|
+
for (; i < end; i++) {
|
800
805
|
if (fn.call(scope, me.items[i])) {
|
801
806
|
items.push(me.items[i]);
|
802
807
|
}
|
@@ -805,6 +810,16 @@ class Base extends CoreBase {
|
|
805
810
|
return items;
|
806
811
|
}
|
807
812
|
|
813
|
+
/**
|
814
|
+
* Returns the first item which matches the property and value
|
815
|
+
* @param {Object|String} property
|
816
|
+
* @param {String|Number} [value] Only required in case the first param is a string
|
817
|
+
* @returns {Object} Returns the first found item or null
|
818
|
+
*/
|
819
|
+
findFirst(property, value) {
|
820
|
+
return this.find(property, value, true);
|
821
|
+
}
|
822
|
+
|
808
823
|
/**
|
809
824
|
* Returns the first item inside the collection
|
810
825
|
* @returns {Object}
|
package/src/manager/Instance.mjs
CHANGED