was.js – Blame information for rev 34

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