was.js – Diff between revs 47 and 48

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