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