Skip to content

Commit f16d095

Browse files
authored
Update PowerArray.js
Added Split and AllIndexes functions
1 parent f14553c commit f16d095

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

PowerArray.js

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ if (mainContainer.pa && console && console.warn) {
426426
condition.condition = pa.EqualTo3(condition.condition); //transforms an explicit value into an === evaluation
427427
}
428428

429-
429+
430430
const valueToEvaluate = condition.column ? item[condition.column] : item;
431431
if (!item || !condition.condition(valueToEvaluate)) { //if one condition is not fulfilled, just return false;
432432
return false;
@@ -981,7 +981,7 @@ if (mainContainer.pa && console && console.warn) {
981981
var l = value.length;
982982
while (l--) {
983983
valueCaseInsensitive = value[l].toUpperCase();
984-
if ((val+'').toUpperCase().indexOf(valueCaseInsensitive) === -1) {
984+
if ((val + '').toUpperCase().indexOf(valueCaseInsensitive) === -1) {
985985
return false;
986986
}
987987
}
@@ -1000,7 +1000,7 @@ if (mainContainer.pa && console && console.warn) {
10001000
var l = value.length;
10011001
while (l--) {
10021002
valueCaseInsensitive = value[l].toUpperCase();
1003-
if ((val+'').toUpperCase().indexOf(valueCaseInsensitive) > -1) {
1003+
if ((val + '').toUpperCase().indexOf(valueCaseInsensitive) > -1) {
10041004
return false;
10051005
}
10061006
}
@@ -1375,7 +1375,7 @@ if (mainContainer.pa && console && console.warn) {
13751375
if (sortConditions.hasOwnProperty(property)) {
13761376

13771377
//transform the keys into a better object with properties Column and SortOrder
1378-
var value = sortConditions[property+''].toUpperCase();
1378+
var value = sortConditions[property + ''].toUpperCase();
13791379

13801380
if (!mainContainer.pa.Sort._validSortConfigStrings.indexOf(sortConditions[property]) === -1) {
13811381
throw new Error("PowerArray Configuration Error => Invalid sort direction for property " + property + ": '" + sortConditions[property] + "'");
@@ -1598,7 +1598,7 @@ if (mainContainer.pa && console && console.warn) {
15981598
return pa.prototypedFunctions_Array.Where.call(this, whereConditions, true, true);
15991599
},
16001600
FirstIndex: function (whereConditions) {// jshint ignore:line
1601-
if (typeof (whereConditions) === 'string') {
1601+
if (typeof (whereConditions) === 'string' || typeof (whereConditions) === 'number') {
16021602
//transform single match strings to an EqualTo3
16031603
whereConditions = EqualTo3(whereConditions);
16041604
}
@@ -1607,6 +1607,26 @@ if (mainContainer.pa && console && console.warn) {
16071607
}
16081608
return pa.prototypedFunctions_Array.Where.call(this, whereConditions, true, true, true);
16091609
},
1610+
/*
1611+
returns an array of numbers, with the index position of all matching elements
1612+
given:
1613+
var arr = [1,2,3,4,2,56,2,64];
1614+
arr.AllIndexes(2) => [1,4,6];
1615+
arr.AllIndexes(325) => [];
1616+
arr.AllIndexes(1) => [0]
1617+
1618+
*/
1619+
AllIndexes: function (whereConditions) {
1620+
var newArray = this;
1621+
var foundIndex = newArray.FirstIndex(whereConditions);
1622+
var result = [], toSkip = 0;
1623+
while ((foundIndex = newArray.FirstIndex(whereConditions)) !== undefined) {
1624+
result.push(foundIndex + toSkip);
1625+
newArray = this.Skip(toSkip + foundIndex + 1);
1626+
toSkip += foundIndex + 1;
1627+
}
1628+
return result;
1629+
},
16101630
AttachIndex: function (getterName, fromProp) {
16111631
return pa.prototypedFunctions_Array.attachIndex.call(this, getterName, fromProp);
16121632
},
@@ -1649,8 +1669,8 @@ if (mainContainer.pa && console && console.warn) {
16491669
}
16501670
}
16511671

1652-
if(arguments.length === 1)
1653-
return result[ arguments[0]]; //return only the result, in order to avoid the duplication of the property name when only using 1 prop.
1672+
if (arguments.length === 1)
1673+
return result[arguments[0]]; //return only the result, in order to avoid the duplication of the property name when only using 1 prop.
16541674

16551675
return result;
16561676

@@ -1714,6 +1734,32 @@ if (mainContainer.pa && console && console.warn) {
17141734
Last: function () {
17151735
var idx = this.length - 1;
17161736
return (idx > -1) ? this[idx] : null;
1737+
},
1738+
/*returns an array of arrays, by splitting the current array by matching the first occurences of the passed whereconditions object
1739+
given:
1740+
arr = [1,2,3,4,0,2,3,6,3,0,5,2,5,54,0,2,6]
1741+
condition = 0; its a number, but it could be any where condition or array of them
1742+
arr.split(0) will return = [[1,2,3,4],[2,3,6,3],[5,2,5,54],[2,6]]
1743+
*/
1744+
Split: function (whereConditions) {
1745+
var result = [], nextStartIdx = 0;
1746+
var allIndexes = this.AllIndexes(whereConditions);
1747+
var prevIndexVal = -1;
1748+
allIndexes.RunEach((val, i) => {
1749+
var toTake = val - (prevIndexVal + 1);
1750+
result.push(this.Take(toTake, prevIndexVal < 0 ? 0 : prevIndexVal + 1));
1751+
prevIndexVal = val;
1752+
}, null, true);
1753+
1754+
if (allIndexes.length === 0)
1755+
return [this];
1756+
1757+
var lastIndex = allIndexes.Last();
1758+
if (lastIndex < this.length) { //add the elements between the last index and the end of the array
1759+
result.push(this.Take(this.length - lastIndex, lastIndex + 1));
1760+
}
1761+
1762+
return result;
17171763
}
17181764
};
17191765

0 commit comments

Comments
 (0)