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