was.js – Diff between revs 31 and 33

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