Apply store pattern to track global app state
[vchess.git] / client / src / utils / array.js
1 // Remove item(s) in array (if present)
2 export function remove(array, rfun, all)
3 {
4 const index = array.findIndex(rfun);
5 if (index >= 0)
6 {
7 array.splice(index, 1);
8 if (!!all)
9 {
10 // Reverse loop because of the splice below
11 for (let i=array.length-1; i>=index; i--)
12 {
13 if (rfun(array[i]))
14 array.splice(i, 1);
15 }
16 }
17 }
18 }
19
20 // Double array intialization
21 export function init(size1, size2, initElem)
22 {
23 return [...Array(size1)].map(e => Array(size2).fill(initElem));
24 }
25
26 export function range(max)
27 {
28 return [...Array(max).keys()];
29 }