was.js – Blame information for rev 29
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | office | 1 | /*************************************************************************/ |
2 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
3 | /*************************************************************************/ |
||
29 | office | 4 | function wasProduct(a, b) { |
5 | var m = Math.max(a.length, b.length); |
||
25 | office | 6 | var o = {}; |
7 | for(var i = 0; i < m; ++i) { |
||
8 | o[a[i]] = b[i]; |
||
9 | } |
||
10 | return o; |
||
11 | } |
||
2 | office | 12 | if (!Array.prototype.product) { |
29 | office | 13 | Array.prototype.product = function(b) { |
14 | return wasProduct(this, b); |
||
15 | }; |
||
2 | office | 16 | } |
25 | office | 17 | // jQuery |
18 | if(typeof jQuery === 'function') { |
||
19 | $.extend({ |
||
20 | product: wasProduct |
||
21 | }); |
||
22 | } |
||
2 | office | 23 | |
24 | /*************************************************************************/ |
||
25 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
26 | /*************************************************************************/ |
||
29 | office | 27 | function wasStride(a, s) { |
28 | return a.filter(function(e, i) { |
||
25 | office | 29 | return i % s === 0; |
30 | }); |
||
31 | } |
||
2 | office | 32 | if (!Array.prototype.stride) { |
29 | office | 33 | Array.prototype.stride = function(s) { |
34 | return wasStride(this, s); |
||
35 | }; |
||
2 | office | 36 | } |
25 | office | 37 | // jQuery |
38 | if(typeof jQuery === 'function') { |
||
39 | $.extend({ |
||
40 | stride: wasStride |
||
41 | }); |
||
42 | } |
||
2 | office | 43 | |
44 | /*************************************************************************/ |
||
45 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
46 | /*************************************************************************/ |
||
25 | office | 47 | // Vanilla JavaScript |
29 | office | 48 | function wasChunk(a, n) { |
49 | if (!a.length) { |
||
25 | office | 50 | return []; |
51 | } |
||
29 | office | 52 | return [a.slice(0, n)] |
53 | .concat(a.slice(n).wasChunk(n)); |
||
25 | office | 54 | } |
2 | office | 55 | if (!Array.prototype.chunk) { |
29 | office | 56 | Array.prototype.chunk = function(a, n) { |
57 | return wasChunk(this, n); |
||
58 | }; |
||
2 | office | 59 | } |
25 | office | 60 | // jQuery |
61 | if(typeof jQuery === 'function') { |
||
62 | $.extend({ |
||
63 | chunk: wasChunk |
||
64 | }); |
||
65 | } |
||
5 | office | 66 | |
67 | /*************************************************************************/ |
||
68 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
69 | /*************************************************************************/ |
||
70 | /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ |
||
71 | /*************************************************************************/ |
||
25 | office | 72 | // Vanilla JavaScript |
29 | office | 73 | function wasEquals(a, b) { |
25 | office | 74 | // if the other array is a falsy value, return |
29 | office | 75 | if (!b) { |
25 | office | 76 | return false; |
77 | } |
||
5 | office | 78 | |
25 | office | 79 | // compare lengths - can save a lot of time |
29 | office | 80 | if (a.length !== b.length) { |
25 | office | 81 | return false; |
82 | } |
||
5 | office | 83 | |
29 | office | 84 | for (var i = 0, l = a.length; i < l; i++) { |
25 | office | 85 | // Check if we have nested arrays |
29 | office | 86 | if (a[i] instanceof Array && b[i] instanceof Array) { |
25 | office | 87 | // recurse into the nested arrays |
29 | office | 88 | if (!a[i].equals(b[i])) { |
5 | office | 89 | return false; |
90 | } |
||
29 | office | 91 | } else if (a[i] !== b[i]) { |
25 | office | 92 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
93 | return false; |
||
5 | office | 94 | } |
25 | office | 95 | } |
96 | return true; |
||
5 | office | 97 | } |
25 | office | 98 | if (!Array.prototype.equals) { |
99 | // attach the .equals method to Array's prototype to call it on any array |
||
29 | office | 100 | Array.prototype.equals = function(a, b) { |
101 | return wasEquals(this, b); |
||
102 | }; |
||
25 | office | 103 | } |
104 | // jQuery |
||
105 | if(typeof jQuery === 'function') { |
||
106 | $.extend({ |
||
107 | equals: wasEquals |
||
108 | }); |
||
109 | } |
||
5 | office | 110 | |
25 | office | 111 | /*************************************************************************/ |
112 | /* Node.JS package export. */ |
||
113 | /*************************************************************************/ |
||
27 | office | 114 | module.exports.collections = { |
26 | office | 115 | product: wasProduct, |
25 | office | 116 | stride: wasStride, |
117 | chunk: wasChunk, |
||
118 | equals: wasEquals |
||
119 | }; |