was.js – Blame information for rev 30

Subversion Repositories:
Rev:
Rev Author Line No. Line
24 office 1 /*! was.js - v1.0.0 - 2017-12-13
3 office 2 * http://grimore.org
3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */
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 /*************************************************************************/
28 office 116 module.exports.collections = {
26 office 117 product: wasProduct,
118 stride: wasStride,
119 chunk: wasChunk,
120 equals: wasEquals
121 };
5 office 122  
7 office 123 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
124 /*************************************************************************/
3 office 125 function wasCSVToArray(csv) {
126 var l = [];
127 var s = [];
128 var m = "";
129  
130 do {
131 var a = csv.charAt(0);
132 csv = csv.slice(1, csv.length);
133 if(a === ",") {
134 if(s[s.length-1] !== '"') {
135 l.push(m);
136 m = "";
137 continue;
138 }
139 m += a;
140 continue;
141 }
142 if(a === '"' && csv.charAt(0) === a) {
143 m += a;
144 csv = csv.slice(1, csv.length);
145 continue;
146 }
147 if(a === '"') {
148 if(s[s.length-1] !== a) {
149 s.push(a);
150 continue;
151 }
152 s.pop();
153 continue;
154 }
155 m += a;
156 } while(csv !== "");
157  
158 l.push(m);
159  
160 return l;
161 }
162  
7 office 163 /*************************************************************************/
164 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
165 /*************************************************************************/
3 office 166 function wasArrayToCSV(a) {
167 var csv = [];
168 for(var i=0; i<a.length; ++i) {
169 var cell = a[i].toString().replace('"', '""');
170 if(/"\s,\r\n/.test(cell)) {
171 csv[i] = '"' + cell + '"';
172 continue;
173 }
174 csv[i] = cell;
175 }
176 return csv.join();
177 }
7 office 178  
26 office 179 /*************************************************************************/
180 /* Node.JS package export. */
181 /*************************************************************************/
28 office 182 module.exports.formats = {
26 office 183 CSVToArray: wasCSVToArray,
184 ArrayToCSV: wasArrayToCSV
185 };
12 office 186 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
187 /*************************************************************************/
26 office 188 function wasKeyValueToObject(a) {
12 office 189 var o = {};
190 a.reduce(function(a, c, i) {
191 i = Math.floor(i / 2);
192 if (!a[i]) {
193 a[i] = [];
194 }
195 a[i].push(c);
196 return a;
197 }, []).forEach(function(c, i, a) {
198 o[c[0]] = c[1];
199 }, o);
200 return o;
201 }
202  
26 office 203 /*************************************************************************/
204 /* Node.JS package export. */
205 /*************************************************************************/
28 office 206 module.exports.formats = {
26 office 207 KeyValueToObject: wasKeyValueToObject
208 };
24 office 209 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
210 /*************************************************************************/
211 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
212 /*************************************************************************/
26 office 213 // Vanilla ES6 JavaScript
24 office 214 function wasSwitch(q, d, ...c) {
215 if(c.length % 2 !== 0) {
216 throw "Pairs of predicates expected for cases";
217 }
218  
30 office 219 (Array.isArray(q) ? q : [ q ]).forEach((s) => {
24 office 220 var m = false;
221 for(var i = 0; i < c.length; i += 2) {
222 if(!c[i](s)) {
223 continue;
224 }
225 if(!c[i + 1](s)) {
226 continue;
227 }
228 m = true;
229 }
230  
231 if(!m) {
232 d(s);
233 }
234 });
235 }
236 if (!Array.prototype.switch) {
30 office 237 Array.prototype.switch = function(d, ...c) {
238 wasSwitch(this, d, ...c);
239 };
24 office 240 }
241 // jQuery
242 if(typeof jQuery === 'function') {
243 $.extend({
244 switch: wasSwitch
245 });
246 }
26 office 247  
248 /*************************************************************************/
249 /* Node.JS package export. */
250 /*************************************************************************/
28 office 251 module.exports.lambda = {
24 office 252 switch: wasSwitch
253 };
254  
7 office 255 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
256 /*************************************************************************/
257 function wasMapValueToRange(value, xMin, xMax, yMin, yMax) {
258 return yMin + (
259 ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
260 );
261 }
262  
26 office 263 /*************************************************************************/
264 /* Node.JS package export. */
265 /*************************************************************************/
28 office 266 module.exports.mathematics = {
26 office 267 MapValueToRange: wasMapValueToRange
268 };
269  
7 office 270 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
271 /*************************************************************************/
8 office 272 function wasHexToRGB(hex) {
7 office 273 var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
274 hex = hex.replace(
8 office 275 shortRegEx,
7 office 276 function(m, r, g, b) {
277 return r + r + g + g + b + b;
278 }
279 );
8 office 280  
7 office 281 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
282 return result ? {
283 r: parseInt(result[1], 16),
284 g: parseInt(result[2], 16),
285 b: parseInt(result[3], 16)
286 } : null;
8 office 287 }
288  
289 /*************************************************************************/
290 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
291 /*************************************************************************/
292 function wasRGBToHex(r, g, b) {
293 return "#" + (
294 (1 << 24) +
295 << 24) +< 24) + (r << 16) +
296 << 24) +< 24) +<< 16) +< 16) + (g << 8) +
297 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + b
298 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + ).toString(16).slice(1);
299 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}
26 office 300  
301 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/
302 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/* Node.JS package export. */
303 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/
28 office 304 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +module.exports.physics = {
26 office 305 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + HexToRGB: wasHexToRGB,
306 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + RGBToHex: wasRGBToHex
307 << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +};