was.js – Blame information for rev 24
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
24 | office | 1 | /*! was.js - v1.0.0 - 2017-12-13 |
3 | office | 2 | * http://grimore.org |
3 | * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */ |
||
4 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
5 | /*************************************************************************/ |
||
6 | if (!Array.prototype.product) { |
||
7 | Array.prototype.product = function(b) { |
||
8 | var a = this; |
||
9 | return $.map( |
||
10 | new Array(Math.max(this.length, a.length)), |
||
11 | function(e, i) { |
||
12 | var o = {}; |
||
13 | o[a[i]] = b[i]; |
||
14 | return o; |
||
5 | office | 15 | }); |
3 | office | 16 | }; |
17 | } |
||
18 | $.extend({ |
||
19 | product: function(a, b) { |
||
20 | return $.map( |
||
21 | new Array(Math.max(this.length, a.length)), |
||
22 | function(e, i) { |
||
23 | var o = {}; |
||
24 | o[a[i]] = b[i]; |
||
25 | return o; |
||
5 | office | 26 | }); |
3 | office | 27 | } |
28 | }); |
||
29 | |||
30 | /*************************************************************************/ |
||
31 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
32 | /*************************************************************************/ |
||
33 | if (!Array.prototype.stride) { |
||
34 | Array.prototype.stride = function(s) { |
||
35 | return this.filter(function(e, i) { |
||
36 | return i % s === 0; |
||
37 | }); |
||
38 | }; |
||
39 | } |
||
40 | $.extend({ |
||
41 | stride: function(a, s) { |
||
42 | return a.filter(function(e, i) { |
||
43 | return i % s === 0; |
||
44 | }); |
||
45 | } |
||
46 | }); |
||
47 | |||
48 | /*************************************************************************/ |
||
49 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
50 | /*************************************************************************/ |
||
51 | if (!Array.prototype.chunk) { |
||
52 | Array.prototype.chunk = function(n) { |
||
53 | if (!this.length) { |
||
54 | return []; |
||
55 | } |
||
56 | return [this.slice(0, n)] |
||
57 | .concat(this.slice(n).chunk(n)); |
||
58 | }; |
||
59 | } |
||
60 | $.extend({ |
||
61 | chunk: function(a, n) { |
||
62 | if (!a.length) { |
||
63 | return []; |
||
64 | } |
||
65 | return [a.slice(0, n)] |
||
66 | .concat(a.slice(n).chunk(n)); |
||
67 | } |
||
68 | }); |
||
69 | |||
5 | office | 70 | /*************************************************************************/ |
71 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
72 | /*************************************************************************/ |
||
73 | /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ |
||
74 | /*************************************************************************/ |
||
75 | if (!Array.prototype.equals) { |
||
76 | // attach the .equals method to Array's prototype to call it on any array |
||
77 | Array.prototype.equals = function(a) { |
||
78 | // if the other array is a falsy value, return |
||
79 | if (!a) { |
||
80 | return false; |
||
81 | } |
||
82 | |||
83 | // compare lengths - can save a lot of time |
||
84 | if (this.length !== a.length) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | for (var i = 0, l = this.length; i < l; i++) { |
||
89 | // Check if we have nested arrays |
||
90 | if (this[i] instanceof Array && a[i] instanceof Array) { |
||
91 | // recurse into the nested arrays |
||
92 | if (!this[i].equals(a[i])) { |
||
93 | return false; |
||
94 | } |
||
95 | } else if (this[i] !== a[i]) { |
||
96 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
||
97 | return false; |
||
98 | } |
||
99 | } |
||
100 | return true; |
||
101 | }; |
||
102 | } |
||
103 | |||
104 | $.extend({ |
||
105 | equals: function(a) { |
||
106 | // if the other array is a falsy value, return |
||
107 | if (!a) { |
||
108 | return false; |
||
109 | } |
||
110 | |||
111 | // compare lengths - can save a lot of time |
||
112 | if (this.length !== a.length) { |
||
113 | return false; |
||
114 | } |
||
115 | |||
116 | for (var i = 0, l = this.length; i < l; i++) { |
||
117 | // Check if we have nested arrays |
||
118 | if (this[i] instanceof Array && a[i] instanceof Array) { |
||
119 | // recurse into the nested arrays |
||
120 | if (!this[i].equals(a[i])) { |
||
121 | return false; |
||
122 | } |
||
123 | } else if (this[i] !== a[i]) { |
||
124 | // Warning - two different object instances will never be equal: {x:20} != {x:20} |
||
125 | return false; |
||
126 | } |
||
127 | } |
||
128 | return true; |
||
129 | } |
||
130 | }); |
||
131 | |||
7 | office | 132 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
133 | /*************************************************************************/ |
||
3 | office | 134 | function wasCSVToArray(csv) { |
135 | var l = []; |
||
136 | var s = []; |
||
137 | var m = ""; |
||
138 | |||
139 | do { |
||
140 | var a = csv.charAt(0); |
||
141 | csv = csv.slice(1, csv.length); |
||
142 | if(a === ",") { |
||
143 | if(s[s.length-1] !== '"') { |
||
144 | l.push(m); |
||
145 | m = ""; |
||
146 | continue; |
||
147 | } |
||
148 | m += a; |
||
149 | continue; |
||
150 | } |
||
151 | if(a === '"' && csv.charAt(0) === a) { |
||
152 | m += a; |
||
153 | csv = csv.slice(1, csv.length); |
||
154 | continue; |
||
155 | } |
||
156 | if(a === '"') { |
||
157 | if(s[s.length-1] !== a) { |
||
158 | s.push(a); |
||
159 | continue; |
||
160 | } |
||
161 | s.pop(); |
||
162 | continue; |
||
163 | } |
||
164 | m += a; |
||
165 | } while(csv !== ""); |
||
166 | |||
167 | l.push(m); |
||
168 | |||
169 | return l; |
||
170 | } |
||
171 | |||
7 | office | 172 | /*************************************************************************/ |
173 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
174 | /*************************************************************************/ |
||
3 | office | 175 | function wasArrayToCSV(a) { |
176 | var csv = []; |
||
177 | for(var i=0; i<a.length; ++i) { |
||
178 | var cell = a[i].toString().replace('"', '""'); |
||
179 | if(/"\s,\r\n/.test(cell)) { |
||
180 | csv[i] = '"' + cell + '"'; |
||
181 | continue; |
||
182 | } |
||
183 | csv[i] = cell; |
||
184 | } |
||
185 | return csv.join(); |
||
186 | } |
||
7 | office | 187 | |
12 | office | 188 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
189 | /*************************************************************************/ |
||
190 | function wasKeyValueObjectify(a) { |
||
191 | var o = {}; |
||
192 | a.reduce(function(a, c, i) { |
||
193 | i = Math.floor(i / 2); |
||
194 | if (!a[i]) { |
||
195 | a[i] = []; |
||
196 | } |
||
197 | a[i].push(c); |
||
198 | return a; |
||
199 | }, []).forEach(function(c, i, a) { |
||
200 | o[c[0]] = c[1]; |
||
201 | }, o); |
||
202 | return o; |
||
203 | } |
||
204 | |||
24 | office | 205 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
206 | /*************************************************************************/ |
||
207 | /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ |
||
208 | /*************************************************************************/ |
||
209 | function wasSwitch(q, d, ...c) { |
||
210 | if(c.length % 2 !== 0) { |
||
211 | throw "Pairs of predicates expected for cases"; |
||
212 | } |
||
213 | |||
214 | (Array.isArray(this) ? this : [ this ]).forEach((s) => { |
||
215 | var m = false; |
||
216 | for(var i = 0; i < c.length; i += 2) { |
||
217 | if(!c[i](s)) { |
||
218 | continue; |
||
219 | } |
||
220 | if(!c[i + 1](s)) { |
||
221 | continue; |
||
222 | } |
||
223 | m = true; |
||
224 | } |
||
225 | |||
226 | if(!m) { |
||
227 | d(s); |
||
228 | } |
||
229 | }); |
||
230 | } |
||
231 | |||
232 | // Vanilla ES6 JavaScript |
||
233 | if (!Array.prototype.switch) { |
||
234 | Array.prototype.switch = wasSwitch; |
||
235 | } |
||
236 | // jQuery |
||
237 | if(typeof jQuery === 'function') { |
||
238 | $.extend({ |
||
239 | switch: wasSwitch |
||
240 | }); |
||
241 | } |
||
242 | // Node.JS |
||
243 | module.exports = { |
||
244 | switch: wasSwitch |
||
245 | }; |
||
246 | |||
7 | office | 247 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
248 | /*************************************************************************/ |
||
249 | function wasMapValueToRange(value, xMin, xMax, yMin, yMax) { |
||
250 | return yMin + ( |
||
251 | ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) |
||
252 | ); |
||
253 | } |
||
254 | |||
255 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
256 | /*************************************************************************/ |
||
8 | office | 257 | function wasHexToRGB(hex) { |
7 | office | 258 | var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; |
259 | hex = hex.replace( |
||
8 | office | 260 | shortRegEx, |
7 | office | 261 | function(m, r, g, b) { |
262 | return r + r + g + g + b + b; |
||
263 | } |
||
264 | ); |
||
8 | office | 265 | |
7 | office | 266 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
267 | return result ? { |
||
268 | r: parseInt(result[1], 16), |
||
269 | g: parseInt(result[2], 16), |
||
270 | b: parseInt(result[3], 16) |
||
271 | } : null; |
||
8 | office | 272 | } |
273 | |||
274 | /*************************************************************************/ |
||
275 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
276 | /*************************************************************************/ |
||
277 | function wasRGBToHex(r, g, b) { |
||
278 | return "#" + ( |
||
279 | (1 << 24) +><> |
||
280 | << 24) +< 24) + (r << 16) +><>><> |
||
281 | << 24) +< 24) +<< 16) +< 16) + (g << 8) +><>><>><> |
||
282 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + b><>><>><> |
||
283 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + ).toString(16).slice(1);><>><>><> |
||
284 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}><>><>><> |