was.js – Blame information for rev 41
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
41 | office | 1 | /*! was.js - v1.0.4 - 2019-08-03 |
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 | /*************************************************************************/ |
||
41 | office | 116 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 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 | /* Node.JS package export. */ |
182 | /*************************************************************************/ |
||
41 | office | 183 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
184 | /* global wasCSVToArray, wasArrayToCSV, wasKeyValueToObject */ |
||
34 | office | 185 | module.exports.formats = { |
41 | office | 186 | csv: { |
187 | CSVToArray: wasCSVToArray, |
||
188 | ArrayToCSV: wasArrayToCSV |
||
189 | }, |
||
190 | kvp: { |
||
191 | KeyValueToObject: wasKeyValueToObject |
||
192 | } |
||
34 | office | 193 | }; |
194 | } |
||
195 | |||
12 | office | 196 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
197 | /*************************************************************************/ |
||
26 | office | 198 | function wasKeyValueToObject(a) { |
12 | office | 199 | var o = {}; |
200 | a.reduce(function(a, c, i) { |
||
201 | i = Math.floor(i / 2); |
||
202 | if (!a[i]) { |
||
203 | a[i] = []; |
||
204 | } |
||
205 | a[i].push(c); |
||
206 | return a; |
||
207 | }, []).forEach(function(c, i, a) { |
||
208 | o[c[0]] = c[1]; |
||
209 | }, o); |
||
210 | return o; |
||
211 | } |
||
212 | |||
24 | office | 213 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
214 | /*************************************************************************/ |
||
215 | /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ |
||
216 | /*************************************************************************/ |
||
35 | office | 217 | function wasSwitch() { |
40 | office | 218 | if(arguments.length % 2 !== 0) { |
24 | office | 219 | throw "Pairs of predicates expected for cases"; |
220 | } |
||
221 | |||
40 | office | 222 | (Array.isArray(arguments[0]) ? arguments[0] : [ arguments[0] ]).forEach(function(s) { |
24 | office | 223 | var m = false; |
35 | office | 224 | for(var i = 2; i < arguments.length; i += 2) { |
225 | if(!arguments[i](s)) { |
||
24 | office | 226 | continue; |
227 | } |
||
35 | office | 228 | if(!arguments[i + 1](s)) { |
24 | office | 229 | continue; |
230 | } |
||
231 | m = true; |
||
232 | } |
||
233 | |||
234 | if(!m) { |
||
35 | office | 235 | arguments[1](s); |
24 | office | 236 | } |
237 | }); |
||
238 | } |
||
239 | if (!Array.prototype.switch) { |
||
35 | office | 240 | Array.prototype.switch = function() { |
241 | wasSwitch(this, arguments[0], arguments.slice(1)); |
||
30 | office | 242 | }; |
24 | office | 243 | } |
244 | // jQuery |
||
245 | if(typeof jQuery === 'function') { |
||
246 | $.extend({ |
||
247 | switch: wasSwitch |
||
248 | }); |
||
249 | } |
||
26 | office | 250 | |
251 | /*************************************************************************/ |
||
252 | /* Node.JS package export. */ |
||
253 | /*************************************************************************/ |
||
41 | office | 254 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 255 | module.exports.lambda = { |
256 | switch: wasSwitch |
||
257 | }; |
||
258 | } |
||
24 | office | 259 | |
7 | office | 260 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
261 | /*************************************************************************/ |
||
262 | function wasMapValueToRange(value, xMin, xMax, yMin, yMax) { |
||
263 | return yMin + ( |
||
264 | ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) |
||
265 | ); |
||
266 | } |
||
267 | |||
26 | office | 268 | /*************************************************************************/ |
269 | /* Node.JS package export. */ |
||
270 | /*************************************************************************/ |
||
41 | office | 271 | if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') { |
34 | office | 272 | module.exports.mathematics = { |
273 | MapValueToRange: wasMapValueToRange |
||
274 | }; |
||
275 | } |
||
26 | office | 276 | |
7 | office | 277 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
278 | /*************************************************************************/ |
||
8 | office | 279 | function wasHexToRGB(hex) { |
7 | office | 280 | var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; |
281 | hex = hex.replace( |
||
8 | office | 282 | shortRegEx, |
7 | office | 283 | function(m, r, g, b) { |
284 | return r + r + g + g + b + b; |
||
285 | } |
||
286 | ); |
||
8 | office | 287 | |
7 | office | 288 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
289 | return result ? { |
||
290 | r: parseInt(result[1], 16), |
||
291 | g: parseInt(result[2], 16), |
||
292 | b: parseInt(result[3], 16) |
||
293 | } : null; |
||
8 | office | 294 | } |
295 | |||
296 | /*************************************************************************/ |
||
297 | /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
298 | /*************************************************************************/ |
||
299 | function wasRGBToHex(r, g, b) { |
||
300 | return "#" + ( |
||
301 | (1 << 24) +><> |
||
302 | << 24) +< 24) + (r << 16) +><>><> |
||
303 | << 24) +< 24) +<< 16) +< 16) + (g << 8) +><>><>><> |
||
304 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + b><>><>><> |
||
305 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + ).toString(16).slice(1);><>><>><> |
||
306 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}><>><>><> |
||
26 | office | 307 | |
308 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/><>><>><> |
||
309 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/* Node.JS package export. */><>><>><> |
||
310 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +/*************************************************************************/><>><>><> |
||
41 | office | 311 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +if(typeof module !== 'undefined' && typeof module.exports !== 'undefined') {><>><>><> |
34 | office | 312 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + module.exports.physics = {><>><>><> |
313 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + HexToRGB: wasHexToRGB,><>><>><> |
||
314 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + RGBToHex: wasRGBToHex><>><>><> |
||
315 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) + };><>><>><> |
||
316 | << 24) +< 24) +<< 16) +< 16) +<< 8) +< 8) +}><>><>><> |