was.js – Blame information for rev 51

Subversion Repositories:
Rev:
Rev Author Line No. Line
51 office 1 /*! was.js - v1.0.8 - 2020-12-08
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 /*************************************************************************/
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  
26 office 46 // Vanilla JavaScript
30 office 47 function wasChunk(a, n) {
51 office 48 return Array.from(new Array(Math.ceil(a.length/n)), (_,i)=>a.slice(i*n,i*n+n));
26 office 49 }
3 office 50 if (!Array.prototype.chunk) {
30 office 51 Array.prototype.chunk = function(a, n) {
52 return wasChunk(this, n);
53 };
3 office 54 }
26 office 55 // jQuery
56 if(typeof jQuery === 'function') {
57 $.extend({
58 chunk: wasChunk
59 });
60 }
3 office 61  
5 office 62 /*************************************************************************/
63 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
64 /*************************************************************************/
65 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
66 /*************************************************************************/
26 office 67 // Vanilla JavaScript
30 office 68 function wasEquals(a, b) {
26 office 69 // if the other array is a falsy value, return
30 office 70 if (!b) {
26 office 71 return false;
72 }
5 office 73  
26 office 74 // compare lengths - can save a lot of time
30 office 75 if (a.length !== b.length) {
26 office 76 return false;
77 }
5 office 78  
30 office 79 for (var i = 0, l = a.length; i < l; i++) {
26 office 80 // Check if we have nested arrays
30 office 81 if (a[i] instanceof Array && b[i] instanceof Array) {
26 office 82 // recurse into the nested arrays
30 office 83 if (!a[i].equals(b[i])) {
5 office 84 return false;
85 }
30 office 86 } else if (a[i] !== b[i]) {
26 office 87 // Warning - two different object instances will never be equal: {x:20} != {x:20}
88 return false;
5 office 89 }
26 office 90 }
91 return true;
5 office 92 }
26 office 93 if (!Array.prototype.equals) {
94 // attach the .equals method to Array's prototype to call it on any array
30 office 95 Array.prototype.equals = function(a, b) {
96 return wasEquals(this, b);
97 };
26 office 98 }
99 // jQuery
100 if(typeof jQuery === 'function') {
101 $.extend({
102 equals: wasEquals
103 });
104 }
5 office 105  
26 office 106 /*************************************************************************/
107 /* Node.JS package export. */
108 /*************************************************************************/
41 office 109 if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
34 office 110 module.exports.collections = {
111 product: wasProduct,
112 stride: wasStride,
113 chunk: wasChunk,
114 equals: wasEquals
115 };
116 }
47 office 117  
7 office 118 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
119 /*************************************************************************/
3 office 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  
7 office 158 /*************************************************************************/
159 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
160 /*************************************************************************/
3 office 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 }
47 office 173  
26 office 174 /* Node.JS package export. */
175 /*************************************************************************/
41 office 176 if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
45 office 177 /* global wasCSVToArray, wasArrayToCSV, wasKeyValueToObject, wasKeyValueGet, wasKeyValueSet */
34 office 178 module.exports.formats = {
41 office 179 csv: {
180 CSVToArray: wasCSVToArray,
181 ArrayToCSV: wasArrayToCSV
182 },
183 kvp: {
45 office 184 KeyValueToObject: wasKeyValueToObject,
185 KeyValueGet: wasKeyValueGet,
186 KeyValueSet: wasKeyValueSet
41 office 187 }
34 office 188 };
189 }
47 office 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  
45 office 208 /*************************************************************************/
24 office 209 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
210 /*************************************************************************/
45 office 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 }, '');
47 office 228 }
45 office 229 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
230 /*************************************************************************/
24 office 231 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
232 /*************************************************************************/
47 office 233 function wasSwitch(q, d, ...c) {
234 if(c.length % 2 !== 0) {
24 office 235 throw "Pairs of predicates expected for cases";
236 }
47 office 237  
238 (Array.isArray(q) ? q : [ q ]).forEach((s) => {
24 office 239 var m = false;
47 office 240 for(var i = 0; i < c.length; i += 2) {
241 if(!c[i](s)) {
24 office 242 continue;
243 }
47 office 244 if(!c[i + 1](s)) {
24 office 245 continue;
246 }
247 m = true;
248 }
47 office 249  
24 office 250 if(!m) {
47 office 251 d(s);
24 office 252 }
253 });
254 }
255 if (!Array.prototype.switch) {
35 office 256 Array.prototype.switch = function() {
257 wasSwitch(this, arguments[0], arguments.slice(1));
30 office 258 };
24 office 259 }
260 // jQuery
261 if(typeof jQuery === 'function') {
262 $.extend({
263 switch: wasSwitch
264 });
265 }
26 office 266  
267 /*************************************************************************/
268 /* Node.JS package export. */
269 /*************************************************************************/
41 office 270 if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
34 office 271 module.exports.lambda = {
272 switch: wasSwitch
273 };
274 }
47 office 275  
7 office 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  
26 office 284 /*************************************************************************/
285 /* Node.JS package export. */
286 /*************************************************************************/
41 office 287 if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
34 office 288 module.exports.mathematics = {
289 MapValueToRange: wasMapValueToRange
290 };
291 }
47 office 292  
7 office 293 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
294 /*************************************************************************/
8 office 295 function wasHexToRGB(hex) {
7 office 296 var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
297 hex = hex.replace(
8 office 298 shortRegEx,
7 office 299 function(m, r, g, b) {
300 return r + r + g + g + b + b;
301 }
302 );
8 office 303  
7 office 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;
8 office 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) +}
26 office 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) +/*************************************************************************/
41 office 327 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
34 office 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) +}