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