was.js – Diff between revs 5 and 7
?pathlinks?
Rev 5 | Rev 7 | |||
---|---|---|---|---|
1 | /*! was - v1.0.0 - 2017-05-28 |
1 | /*! was - v1.0.0 - 2017-05-28 |
|
2 | * http://grimore.org |
2 | * http://grimore.org |
|
3 | * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */ |
3 | * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */ |
|
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
5 | /*************************************************************************/ |
5 | /*************************************************************************/ |
|
6 | if (!Array.prototype.product) { |
6 | if (!Array.prototype.product) { |
|
7 | Array.prototype.product = function(b) { |
7 | Array.prototype.product = function(b) { |
|
8 | var a = this; |
8 | var a = this; |
|
9 | return $.map( |
9 | return $.map( |
|
10 | new Array(Math.max(this.length, a.length)), |
10 | new Array(Math.max(this.length, a.length)), |
|
11 | function(e, i) { |
11 | function(e, i) { |
|
12 | var o = {}; |
12 | var o = {}; |
|
13 | o[a[i]] = b[i]; |
13 | o[a[i]] = b[i]; |
|
14 | return o; |
14 | return o; |
|
15 | }); |
15 | }); |
|
16 | }; |
16 | }; |
|
17 | } |
17 | } |
|
18 | $.extend({ |
18 | $.extend({ |
|
19 | product: function(a, b) { |
19 | product: function(a, b) { |
|
20 | return $.map( |
20 | return $.map( |
|
21 | new Array(Math.max(this.length, a.length)), |
21 | new Array(Math.max(this.length, a.length)), |
|
22 | function(e, i) { |
22 | function(e, i) { |
|
23 | var o = {}; |
23 | var o = {}; |
|
24 | o[a[i]] = b[i]; |
24 | o[a[i]] = b[i]; |
|
25 | return o; |
25 | return o; |
|
26 | }); |
26 | }); |
|
27 | } |
27 | } |
|
28 | }); |
28 | }); |
|
29 | |
29 | |
|
30 | /*************************************************************************/ |
30 | /*************************************************************************/ |
|
31 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
31 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
32 | /*************************************************************************/ |
32 | /*************************************************************************/ |
|
33 | if (!Array.prototype.stride) { |
33 | if (!Array.prototype.stride) { |
|
34 | Array.prototype.stride = function(s) { |
34 | Array.prototype.stride = function(s) { |
|
35 | return this.filter(function(e, i) { |
35 | return this.filter(function(e, i) { |
|
36 | return i % s === 0; |
36 | return i % s === 0; |
|
37 | }); |
37 | }); |
|
38 | }; |
38 | }; |
|
39 | } |
39 | } |
|
40 | $.extend({ |
40 | $.extend({ |
|
41 | stride: function(a, s) { |
41 | stride: function(a, s) { |
|
42 | return a.filter(function(e, i) { |
42 | return a.filter(function(e, i) { |
|
43 | return i % s === 0; |
43 | return i % s === 0; |
|
44 | }); |
44 | }); |
|
45 | } |
45 | } |
|
46 | }); |
46 | }); |
|
47 | |
47 | |
|
48 | /*************************************************************************/ |
48 | /*************************************************************************/ |
|
49 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
49 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
50 | /*************************************************************************/ |
50 | /*************************************************************************/ |
|
51 | if (!Array.prototype.chunk) { |
51 | if (!Array.prototype.chunk) { |
|
52 | Array.prototype.chunk = function(n) { |
52 | Array.prototype.chunk = function(n) { |
|
53 | if (!this.length) { |
53 | if (!this.length) { |
|
54 | return []; |
54 | return []; |
|
55 | } |
55 | } |
|
56 | return [this.slice(0, n)] |
56 | return [this.slice(0, n)] |
|
57 | .concat(this.slice(n).chunk(n)); |
57 | .concat(this.slice(n).chunk(n)); |
|
58 | }; |
58 | }; |
|
59 | } |
59 | } |
|
60 | $.extend({ |
60 | $.extend({ |
|
61 | chunk: function(a, n) { |
61 | chunk: function(a, n) { |
|
62 | if (!a.length) { |
62 | if (!a.length) { |
|
63 | return []; |
63 | return []; |
|
64 | } |
64 | } |
|
65 | return [a.slice(0, n)] |
65 | return [a.slice(0, n)] |
|
66 | .concat(a.slice(n).chunk(n)); |
66 | .concat(a.slice(n).chunk(n)); |
|
67 | } |
67 | } |
|
68 | }); |
68 | }); |
|
69 | |
69 | |
|
70 | /*************************************************************************/ |
70 | /*************************************************************************/ |
|
71 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
71 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
72 | /*************************************************************************/ |
72 | /*************************************************************************/ |
|
73 | /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ |
73 | /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ |
|
74 | /*************************************************************************/ |
74 | /*************************************************************************/ |
|
75 | if (!Array.prototype.equals) { |
75 | if (!Array.prototype.equals) { |
|
76 | // attach the .equals method to Array's prototype to call it on any array |
76 | // attach the .equals method to Array's prototype to call it on any array |
|
77 | Array.prototype.equals = function(a) { |
77 | Array.prototype.equals = function(a) { |
|
78 | // if the other array is a falsy value, return |
78 | // if the other array is a falsy value, return |
|
79 | if (!a) { |
79 | if (!a) { |
|
80 | return false; |
80 | return false; |
|
81 | } |
81 | } |
|
82 | |
82 | |
|
83 | // compare lengths - can save a lot of time |
83 | // compare lengths - can save a lot of time |
|
84 | if (this.length !== a.length) { |
84 | if (this.length !== a.length) { |
|
85 | return false; |
85 | return false; |
|
86 | } |
86 | } |
|
87 | |
87 | |
|
88 | for (var i = 0, l = this.length; i < l; i++) { |
88 | for (var i = 0, l = this.length; i < l; i++) { |
|
89 | // Check if we have nested arrays |
89 | // Check if we have nested arrays |
|
90 | if (this[i] instanceof Array && a[i] instanceof Array) { |
90 | if (this[i] instanceof Array && a[i] instanceof Array) { |
|
91 | // recurse into the nested arrays |
91 | // recurse into the nested arrays |
|
92 | if (!this[i].equals(a[i])) { |
92 | if (!this[i].equals(a[i])) { |
|
93 | return false; |
93 | return false; |
|
94 | } |
94 | } |
|
95 | } else if (this[i] !== a[i]) { |
95 | } else if (this[i] !== a[i]) { |
|
96 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
96 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
|
97 | return false; |
97 | return false; |
|
98 | } |
98 | } |
|
99 | } |
99 | } |
|
100 | return true; |
100 | return true; |
|
101 | }; |
101 | }; |
|
102 | } |
102 | } |
|
103 | |
103 | |
|
104 | $.extend({ |
104 | $.extend({ |
|
105 | equals: function(a) { |
105 | equals: function(a) { |
|
106 | // if the other array is a falsy value, return |
106 | // if the other array is a falsy value, return |
|
107 | if (!a) { |
107 | if (!a) { |
|
108 | return false; |
108 | return false; |
|
109 | } |
109 | } |
|
110 | |
110 | |
|
111 | // compare lengths - can save a lot of time |
111 | // compare lengths - can save a lot of time |
|
112 | if (this.length !== a.length) { |
112 | if (this.length !== a.length) { |
|
113 | return false; |
113 | return false; |
|
114 | } |
114 | } |
|
115 | |
115 | |
|
116 | for (var i = 0, l = this.length; i < l; i++) { |
116 | for (var i = 0, l = this.length; i < l; i++) { |
|
117 | // Check if we have nested arrays |
117 | // Check if we have nested arrays |
|
118 | if (this[i] instanceof Array && a[i] instanceof Array) { |
118 | if (this[i] instanceof Array && a[i] instanceof Array) { |
|
119 | // recurse into the nested arrays |
119 | // recurse into the nested arrays |
|
120 | if (!this[i].equals(a[i])) { |
120 | if (!this[i].equals(a[i])) { |
|
121 | return false; |
121 | return false; |
|
122 | } |
122 | } |
|
123 | } else if (this[i] !== a[i]) { |
123 | } else if (this[i] !== a[i]) { |
|
124 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
124 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
|
125 | return false; |
125 | return false; |
|
126 | } |
126 | } |
|
127 | } |
127 | } |
|
128 | return true; |
128 | return true; |
|
129 | } |
129 | } |
|
130 | }); |
130 | }); |
|
131 | |
- | ||
132 | /////////////////////////////////////////////////////////////////////////// |
131 | |
|
133 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
132 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
134 | /////////////////////////////////////////////////////////////////////////// |
133 | /*************************************************************************/ |
|
135 | function wasCSVToArray(csv) { |
134 | function wasCSVToArray(csv) { |
|
136 | var l = []; |
135 | var l = []; |
|
137 | var s = []; |
136 | var s = []; |
|
138 | var m = ""; |
137 | var m = ""; |
|
139 | |
138 | |
|
140 | do { |
139 | do { |
|
141 | var a = csv.charAt(0); |
140 | var a = csv.charAt(0); |
|
142 | csv = csv.slice(1, csv.length); |
141 | csv = csv.slice(1, csv.length); |
|
143 | if(a === ",") { |
142 | if(a === ",") { |
|
144 | if(s[s.length-1] !== '"') { |
143 | if(s[s.length-1] !== '"') { |
|
145 | l.push(m); |
144 | l.push(m); |
|
146 | m = ""; |
145 | m = ""; |
|
147 | continue; |
146 | continue; |
|
148 | } |
147 | } |
|
149 | m += a; |
148 | m += a; |
|
150 | continue; |
149 | continue; |
|
151 | } |
150 | } |
|
152 | if(a === '"' && csv.charAt(0) === a) { |
151 | if(a === '"' && csv.charAt(0) === a) { |
|
153 | m += a; |
152 | m += a; |
|
154 | csv = csv.slice(1, csv.length); |
153 | csv = csv.slice(1, csv.length); |
|
155 | continue; |
154 | continue; |
|
156 | } |
155 | } |
|
157 | if(a === '"') { |
156 | if(a === '"') { |
|
158 | if(s[s.length-1] !== a) { |
157 | if(s[s.length-1] !== a) { |
|
159 | s.push(a); |
158 | s.push(a); |
|
160 | continue; |
159 | continue; |
|
161 | } |
160 | } |
|
162 | s.pop(); |
161 | s.pop(); |
|
163 | continue; |
162 | continue; |
|
164 | } |
163 | } |
|
165 | m += a; |
164 | m += a; |
|
166 | } while(csv !== ""); |
165 | } while(csv !== ""); |
|
167 | |
166 | |
|
168 | l.push(m); |
167 | l.push(m); |
|
169 | |
168 | |
|
170 | return l; |
169 | return l; |
|
171 | } |
170 | } |
|
172 | |
171 | |
|
173 | /////////////////////////////////////////////////////////////////////////// |
172 | /*************************************************************************/ |
|
174 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
173 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
|
175 | /////////////////////////////////////////////////////////////////////////// |
174 | /*************************************************************************/ |
|
176 | function wasArrayToCSV(a) { |
175 | function wasArrayToCSV(a) { |
|
177 | var csv = []; |
176 | var csv = []; |
|
178 | for(var i=0; i<a.length; ++i) { |
177 | for(var i=0; i<a.length; ++i) { |
|
179 | var cell = a[i].toString().replace('"', '""'); |
178 | var cell = a[i].toString().replace('"', '""'); |
|
180 | if(/"\s,\r\n/.test(cell)) { |
179 | if(/"\s,\r\n/.test(cell)) { |
|
181 | csv[i] = '"' + cell + '"'; |
180 | csv[i] = '"' + cell + '"'; |
|
182 | continue; |
181 | continue; |
|
183 | } |
182 | } |
|
184 | csv[i] = cell; |
183 | csv[i] = cell; |
|
185 | } |
184 | } |
|
186 | return csv.join(); |
185 | return csv.join(); |
|
187 | } |
186 | } |
|
- | 187 | |
||
- | 188 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
- | 189 | /*************************************************************************/ |
||
- | 190 | function wasMapValueToRange(value, xMin, xMax, yMin, yMax) { |
||
- | 191 | return yMin + ( |
||
- | 192 | ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) |
||
- | 193 | ); |
||
- | 194 | } |
||
- | 195 | |
||
- | 196 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
- | 197 | /*************************************************************************/ |
||
- | 198 | function HexToRGB(hex) { |
||
- | 199 | var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; |
||
- | 200 | hex = hex.replace( |
||
- | 201 | shortRegEx, |
||
- | 202 | function(m, r, g, b) { |
||
- | 203 | return r + r + g + g + b + b; |
||
- | 204 | } |
||
- | 205 | ); |
||
- | 206 | |
||
- | 207 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
||
- | 208 | return result ? { |
||
- | 209 | r: parseInt(result[1], 16), |
||
- | 210 | g: parseInt(result[2], 16), |
||
- | 211 | b: parseInt(result[3], 16) |
||
- | 212 | } : null; |
||
- | 213 | } |
||
188 | |
214 | |
|
- | 215 |
|
||
- | 216 | |
||
- | 217 | |
||
- | 218 | |