Separate client and server codes. Keep everything in one git repo for simplicity
[vchess.git] / client / client_OLD / javascripts / utils / array.js
diff --git a/client/client_OLD/javascripts/utils/array.js b/client/client_OLD/javascripts/utils/array.js
new file mode 100644 (file)
index 0000000..ab1ae10
--- /dev/null
@@ -0,0 +1,37 @@
+// Remove item in array (if present)
+var removeItem = function(array, rfun)
+{
+       let index = array.findIndex(rfun);
+       if (index >= 0)
+               array.splice(index, 1);
+}
+
+// Remove several item matching a condition
+var removeMultiple = function(array, rfun)
+{
+       // Reverse loop because of the splice below
+       for (let i=array.length-1; i>=0; i--)
+       {
+               if (rfun(array[i]))
+                       array.splice(i, 1);
+       }
+}
+
+// Double array intialization
+var doubleArray = function(size1, size2, initElem)
+{
+       return _.map(_.range(size1), () => {
+               return _.map(_.range(size2), () => {
+                       return initElem; //can be undefined
+               })
+       });
+}
+
+var copyDoubleArray = function(arr)
+{
+       return _.map(_.range(arr.length), (el1,i) => {
+               return _.map(_.range(arr[0].length), (el2,j) => {
+                       return arr[i][j];
+               })
+       });
+}