was.js – Diff between revs 49 and 51

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