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