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