/trunk/lib/collections/arrays/arrays.js |
@@ -1,9 +1,8 @@ |
/*************************************************************************/ |
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
/*************************************************************************/ |
function wasProduct(b) { |
var a = this; |
var m = Math.max(this.length, a.length); |
function wasProduct(a, b) { |
var m = Math.max(a.length, b.length); |
var o = {}; |
for(var i = 0; i < m; ++i) { |
o[a[i]] = b[i]; |
@@ -11,7 +10,9 @@ |
return o; |
} |
if (!Array.prototype.product) { |
Array.prototype.product = wasProduct; |
Array.prototype.product = function(b) { |
return wasProduct(this, b); |
}; |
} |
// jQuery |
if(typeof jQuery === 'function') { |
@@ -23,13 +24,15 @@ |
/*************************************************************************/ |
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
/*************************************************************************/ |
function wasStride(s) { |
return this.filter(function(e, i) { |
function wasStride(a, s) { |
return a.filter(function(e, i) { |
return i % s === 0; |
}); |
} |
if (!Array.prototype.stride) { |
Array.prototype.stride = wasStride; |
Array.prototype.stride = function(s) { |
return wasStride(this, s); |
}; |
} |
// jQuery |
if(typeof jQuery === 'function') { |
@@ -42,15 +45,17 @@ |
/* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
/*************************************************************************/ |
// Vanilla JavaScript |
function wasChunk(n) { |
if (!this.length) { |
function wasChunk(a, n) { |
if (!a.length) { |
return []; |
} |
return [this.slice(0, n)] |
.concat(this.slice(n).wasChunk(n)); |
return [a.slice(0, n)] |
.concat(a.slice(n).wasChunk(n)); |
} |
if (!Array.prototype.chunk) { |
Array.prototype.chunk = wasChunk; |
Array.prototype.chunk = function(a, n) { |
return wasChunk(this, n); |
}; |
} |
// jQuery |
if(typeof jQuery === 'function') { |
@@ -65,25 +70,25 @@ |
/*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ |
/*************************************************************************/ |
// Vanilla JavaScript |
function wasEquals(a) { |
function wasEquals(a, b) { |
// if the other array is a falsy value, return |
if (!a) { |
if (!b) { |
return false; |
} |
|
// compare lengths - can save a lot of time |
if (this.length !== a.length) { |
if (a.length !== b.length) { |
return false; |
} |
|
for (var i = 0, l = this.length; i < l; i++) { |
for (var i = 0, l = a.length; i < l; i++) { |
// Check if we have nested arrays |
if (this[i] instanceof Array && a[i] instanceof Array) { |
if (a[i] instanceof Array && b[i] instanceof Array) { |
// recurse into the nested arrays |
if (!this[i].equals(a[i])) { |
if (!a[i].equals(b[i])) { |
return false; |
} |
} else if (this[i] !== a[i]) { |
} else if (a[i] !== b[i]) { |
// Warning - two different object instances will never be equal: {x:20} != {x:20} |
return false; |
} |
@@ -92,7 +97,9 @@ |
} |
if (!Array.prototype.equals) { |
// attach the .equals method to Array's prototype to call it on any array |
Array.prototype.equals = wasEquals; |
Array.prototype.equals = function(a, b) { |
return wasEquals(this, b); |
}; |
} |
// jQuery |
if(typeof jQuery === 'function') { |