was.js – Blame information for rev 45
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
45 | office | 1 | /*! was.js - v1.0.5 - 2019-12-01
|
2 | * http://grimore.org
|
||
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 | /*************************************************************************/ |
||
41 | office | 116 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 117 | module.exports.collections = { |
118 | product: wasProduct, |
||
119 | stride: wasStride, |
||
120 | chunk: wasChunk, |
||
121 | equals: wasEquals |
||
122 | }; |
||
123 | } |
||
45 | 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 | } |
||
45 | office | 180 | |
26 | office | 181 | /* Node.JS package export. */ |
182 | /*************************************************************************/ |
||
41 | office | 183 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
45 | office | 184 | /* global wasCSVToArray, wasArrayToCSV, wasKeyValueToObject, wasKeyValueGet, wasKeyValueSet */ |
34 | office | 185 | module.exports.formats = { |
41 | office | 186 | csv: { |
187 | CSVToArray: wasCSVToArray, |
||
188 | ArrayToCSV: wasArrayToCSV |
||
189 | }, |
||
190 | kvp: { |
||
45 | office | 191 | KeyValueToObject: wasKeyValueToObject, |
192 | KeyValueGet: wasKeyValueGet, |
||
193 | KeyValueSet: wasKeyValueSet |
||
41 | office | 194 | } |
34 | office | 195 | }; |
196 | } |
||
45 | office | 197 | |
12 | office | 198 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
199 | /*************************************************************************/ |
||
26 | office | 200 | function wasKeyValueToObject(a) { |
12 | office | 201 | var o = {}; |
202 | a.reduce(function(a, c, i) { |
||
203 | i = Math.floor(i / 2); |
||
204 | if (!a[i]) { |
||
205 | a[i] = []; |
||
206 | } |
||
207 | a[i].push(c); |
||
208 | return a; |
||
209 | }, []).forEach(function(c, i, a) { |
||
210 | o[c[0]] = c[1]; |
||
211 | }, o); |
||
212 | return o; |
||
213 | } |
||
214 | |||
45 | office | 215 | /*************************************************************************/ |
24 | office | 216 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
217 | /*************************************************************************/ |
||
45 | office | 218 | function wasKeyValueSet(k, v, data) { |
219 | return data.split('&') |
||
220 | .map(c => c.split('=')[0] === k ? `${k}=${v}` : c) |
||
221 | .concat(`${k}=${v}`) |
||
222 | .filter((e,i,s) => s.indexOf(e) === i) |
||
223 | .join('&'); |
||
224 | } |
||
225 | |||
226 | |||
227 | /*************************************************************************/ |
||
228 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
229 | /*************************************************************************/ |
||
230 | function wasKeyValueGet(k, data) { |
||
231 | return data.split('&').reduce((a, c) => { |
||
232 | var s = c.split('='); |
||
233 | return s[0] === k ? s[1] : a; |
||
234 | }, ''); |
||
235 | }
|
||
236 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
237 | /*************************************************************************/ |
||
24 | office | 238 | /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ |
239 | /*************************************************************************/ |
||
35 | office | 240 | function wasSwitch() { |
40 | office | 241 | if(arguments.length % 2 !== 0) { |
24 | office | 242 | throw "Pairs of predicates expected for cases"; |
243 | } |
||
244 | |||
40 | office | 245 | (Array.isArray(arguments[0]) ? arguments[0] : [ arguments[0] ]).forEach(function(s) { |
24 | office | 246 | var m = false; |
35 | office | 247 | for(var i = 2; i < arguments.length; i += 2) { |
248 | if(!arguments[i](s)) { |
||
24 | office | 249 | continue; |
250 | } |
||
35 | office | 251 | if(!arguments[i + 1](s)) { |
24 | office | 252 | continue; |
253 | } |
||
254 | m = true; |
||
255 | } |
||
256 | |||
257 | if(!m) { |
||
35 | office | 258 | arguments[1](s); |
24 | office | 259 | } |
260 | }); |
||
261 | } |
||
262 | if (!Array.prototype.switch) { |
||
35 | office | 263 | Array.prototype.switch = function() { |
264 | wasSwitch(this, arguments[0], arguments.slice(1)); |
||
30 | office | 265 | }; |
24 | office | 266 | } |
267 | // jQuery |
||
268 | if(typeof jQuery === 'function') { |
||
269 | $.extend({ |
||
270 | switch: wasSwitch |
||
271 | }); |
||
272 | } |
||
26 | office | 273 | |
274 | /*************************************************************************/ |
||
275 | /* Node.JS package export. */ |
||
276 | /*************************************************************************/ |
||
41 | office | 277 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 278 | module.exports.lambda = { |
279 | switch: wasSwitch |
||
280 | }; |
||
281 | } |
||
45 | office | 282 | |
7 | office | 283 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
284 | /*************************************************************************/ |
||
285 | function wasMapValueToRange(value, xMin, xMax, yMin, yMax) { |
||
286 | return yMin + ( |
||
287 | ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) |
||
288 | ); |
||
289 | } |
||
290 | |||
26 | office | 291 | /*************************************************************************/ |
292 | /* Node.JS package export. */ |
||
293 | /*************************************************************************/ |
||
41 | office | 294 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 295 | module.exports.mathematics = { |
296 | MapValueToRange: wasMapValueToRange |
||
297 | }; |
||
298 | } |
||
45 | office | 299 | |
7 | office | 300 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
301 | /*************************************************************************/ |
||
8 | office | 302 | function wasHexToRGB(hex) { |
7 | office | 303 | var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; |
304 | hex = hex.replace( |
||
8 | office | 305 | shortRegEx, |
7 | office | 306 | function(m, r, g, b) { |
307 | return r + r + g + g + b + b; |
||
308 | } |
||
309 | ); |
||
8 | office | 310 | |
7 | office | 311 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
312 | return result ? { |
||
313 | r: parseInt(result[1], 16), |
||
314 | g: parseInt(result[2], 16), |
||
315 | b: parseInt(result[3], 16) |
||
316 | } : null; |
||
8 | office | 317 | } |
318 | |||
319 | /*************************************************************************/ |
||
320 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
321 | /*************************************************************************/ |
||
322 | function wasRGBToHex(r, g, b) { |
||
323 | return "#" + ( |
||
324 | (1 << 24) +><> |
||
325 | << 24) +< 24) + (r << 16) +><>><> |
||
326 | << 24) +< 24) +<< 16) +< 16) + (g << 8) +><>><>><> |
||
327 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + b><>><>><> |
||
328 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + ).toString(16).slice(1);><>><>><> |
||
329 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}><>><>><> |
||
26 | office | 330 | |
331 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/><>><>><> |
||
332 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/* Node.JS package export. */><>><>><> |
||
333 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/><>><>><> |
||
41 | office | 334 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {><>><>><> |
34 | office | 335 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + module.exports.physics = {><>><>><> |
336 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + HexToRGB: wasHexToRGB,><>><>><> |
||
337 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + RGBToHex: wasRGBToHex><>><>><> |
||
338 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + };><>><>><> |
||
339 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}><>><>><> |