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