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