was.js – Diff between revs 24 and 26

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 24 Rev 26
Line 1... Line 1...
1 /*! was.js - v1.0.0 - 2017-12-13 1 /*! was.js - v1.0.0 - 2017-12-13
2 * http://grimore.org 2 * http://grimore.org
3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; Licensed GPL-3.0 */ 3 * Copyright (c) 2017 Wizardry and Steamworks <office@grimore.org>; 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 if (!Array.prototype.product) { 6 function wasProduct(b) {
7 Array.prototype.product = function(b) { -  
8 var a = this; 7 var a = this;
9 return $.map( -  
10 new Array(Math.max(this.length, a.length)), 8 var m = Math.max(this.length, a.length);
11 function(e, i) { -  
12 var o = {}; -  
13 o[a[i]] = b[i]; -  
14 return o; -  
15 }); 9 var o = {};
16 }; -  
17 } -  
18 $.extend({ -  
19 product: function(a, b) { -  
20 return $.map( -  
21 new Array(Math.max(this.length, a.length)), -  
22 function(e, i) { 10 for(var i = 0; i < m; ++i) {
23 var o = {}; -  
24 o[a[i]] = b[i]; 11 o[a[i]] = b[i];
25 return o; -  
26 }); -  
27 } 12 }
-   13 return o;
28 }); 14 }
-   15 if (!Array.prototype.product) {
-   16 Array.prototype.product = wasProduct;
-   17 }
-   18 // jQuery
-   19 if(typeof jQuery === 'function') {
-   20 $.extend({
-   21 product: wasProduct
-   22 });
-   23 }
Line 29... Line 24...
29   24  
30 /*************************************************************************/ 25 /*************************************************************************/
31 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 26 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
-   27 /*************************************************************************/
-   28 function wasStride(s) {
-   29 return this.filter(function(e, i) {
-   30 return i % s === 0;
-   31 });
32 /*************************************************************************/ 32 }
33 if (!Array.prototype.stride) { 33 if (!Array.prototype.stride) {
34 Array.prototype.stride = function(s) { -  
35 return this.filter(function(e, i) { -  
36 return i % s === 0; -  
37 }); -  
38 }; 34 Array.prototype.stride = wasStride;
39 } 35 }
40 $.extend({ 36 // jQuery
41 stride: function(a, s) { 37 if(typeof jQuery === 'function') {
42 return a.filter(function(e, i) { 38 $.extend({
43 return i % s === 0; -  
44 }); 39 stride: wasStride
45 } 40 });
Line 46... Line 41...
46 }); 41 }
47   42  
48 /*************************************************************************/ 43 /*************************************************************************/
49 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ -  
50 /*************************************************************************/ -  
51 if (!Array.prototype.chunk) { -  
52 Array.prototype.chunk = function(n) { 44 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
53 if (!this.length) { -  
54 return []; -  
55 } -  
56 return [this.slice(0, n)] -  
57 .concat(this.slice(n).chunk(n)); -  
58 }; -  
59 } 45 /*************************************************************************/
60 $.extend({ 46 // Vanilla JavaScript
61 chunk: function(a, n) { 47 function wasChunk(n) {
62 if (!a.length) { -  
63 return []; -  
64 } -  
65 return [a.slice(0, n)] 48 if (!this.length) {
-   49 return [];
-   50 }
66 .concat(a.slice(n).chunk(n)); 51 return [this.slice(0, n)]
-   52 .concat(this.slice(n).wasChunk(n));
-   53 }
-   54 if (!Array.prototype.chunk) {
-   55 Array.prototype.chunk = wasChunk;
-   56 }
-   57 // jQuery
-   58 if(typeof jQuery === 'function') {
-   59 $.extend({
-   60 chunk: wasChunk
Line 67... Line 61...
67 } 61 });
68 }); 62 }
69   63  
70 /*************************************************************************/ 64 /*************************************************************************/
71 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 65 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
72 /*************************************************************************/ 66 /*************************************************************************/
73 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/ -  
74 /*************************************************************************/ 67 /*stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript*/
75 if (!Array.prototype.equals) { 68 /*************************************************************************/
76 // attach the .equals method to Array's prototype to call it on any array 69 // Vanilla JavaScript
77 Array.prototype.equals = function(a) { 70 function wasEquals(a) {
78 // if the other array is a falsy value, return 71 // if the other array is a falsy value, return
Line 79... Line 72...
79 if (!a) { 72 if (!a) {
80 return false; 73 return false;
81 } 74 }
82   75  
Line 83... Line 76...
83 // compare lengths - can save a lot of time 76 // compare lengths - can save a lot of time
84 if (this.length !== a.length) { 77 if (this.length !== a.length) {
85 return false; 78 return false;
86 } 79 }
87   80  
88 for (var i = 0, l = this.length; i < l; i++) { -  
89 // Check if we have nested arrays -  
90 if (this[i] instanceof Array && a[i] instanceof Array) { -  
91 // recurse into the nested arrays -  
92 if (!this[i].equals(a[i])) { 81 for (var i = 0, l = this.length; i < l; i++) {
93 return false; 82 // Check if we have nested arrays
94 } -  
95 } else if (this[i] !== a[i]) { -  
96 // Warning - two different object instances will never be equal: {x:20} != {x:20} -  
97 return false; -  
98 } -  
99 } -  
100 return true; -  
101 }; -  
102 } 83 if (this[i] instanceof Array && a[i] instanceof Array) {
103   -  
104 $.extend({ -  
105 equals: function(a) { -  
106 // if the other array is a falsy value, return 84 // recurse into the nested arrays
107 if (!a) { -  
108 return false; 85 if (!this[i].equals(a[i])) {
109 } 86 return false;
110   -  
111 // compare lengths - can save a lot of time -  
112 if (this.length !== a.length) { -  
113 return false; -  
114 } -  
115   -  
116 for (var i = 0, l = this.length; i < l; i++) { -  
117 // Check if we have nested arrays -  
118 if (this[i] instanceof Array && a[i] instanceof Array) { -  
119 // recurse into the nested arrays -  
120 if (!this[i].equals(a[i])) { -  
121 return false; -  
122 } -  
123 } else if (this[i] !== a[i]) { -  
124 // Warning - two different object instances will never be equal: {x:20} != {x:20} 87 }
-   88 } else if (this[i] !== a[i]) {
-   89 // Warning - two different object instances will never be equal: {x:20} != {x:20}
-   90 return false;
-   91 }
-   92 }
-   93 return true;
-   94 }
-   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 }
-   105  
-   106 /*************************************************************************/
-   107 /* Node.JS package export. */
-   108 /*************************************************************************/
125 return false; 109 module.exports.collections.arrays = {
Line 126... Line 110...
126 } 110 product: wasProduct,
127 } 111 stride: wasStride,
128 return true; 112 chunk: wasChunk,
129 } 113 equals: wasEquals
Line 183... Line 167...
183 csv[i] = cell; 167 csv[i] = cell;
184 } 168 }
185 return csv.join(); 169 return csv.join();
186 } 170 }
Line -... Line 171...
-   171  
-   172 /*************************************************************************/
-   173 /* Node.JS package export. */
-   174 /*************************************************************************/
-   175 module.exports.formats.csv = {
-   176 CSVToArray: wasCSVToArray,
-   177 ArrayToCSV: wasArrayToCSV
187   178 };
188 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 179 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
189 /*************************************************************************/ 180 /*************************************************************************/
190 function wasKeyValueObjectify(a) { 181 function wasKeyValueToObject(a) {
191 var o = {}; 182 var o = {};
192 a.reduce(function(a, c, i) { 183 a.reduce(function(a, c, i) {
193 i = Math.floor(i / 2); 184 i = Math.floor(i / 2);
194 if (!a[i]) { 185 if (!a[i]) {
Line 200... Line 191...
200 o[c[0]] = c[1]; 191 o[c[0]] = c[1];
201 }, o); 192 }, o);
202 return o; 193 return o;
203 } 194 }
Line -... Line 195...
-   195  
-   196 /*************************************************************************/
-   197 /* Node.JS package export. */
-   198 /*************************************************************************/
-   199 module.exports.formats.kvp = {
-   200 KeyValueToObject: wasKeyValueToObject
204   201 };
205 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 202 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
206 /*************************************************************************/ 203 /*************************************************************************/
207 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ 204 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
-   205 /*************************************************************************/
208 /*************************************************************************/ 206 // Vanilla ES6 JavaScript
209 function wasSwitch(q, d, ...c) { 207 function wasSwitch(q, d, ...c) {
210 if(c.length % 2 !== 0) { 208 if(c.length % 2 !== 0) {
211 throw "Pairs of predicates expected for cases"; 209 throw "Pairs of predicates expected for cases";
Line 226... Line 224...
226 if(!m) { 224 if(!m) {
227 d(s); 225 d(s);
228 } 226 }
229 }); 227 });
230 } 228 }
231   -  
232 // Vanilla ES6 JavaScript -  
233 if (!Array.prototype.switch) { 229 if (!Array.prototype.switch) {
234 Array.prototype.switch = wasSwitch; 230 Array.prototype.switch = wasSwitch;
235 } 231 }
236 // jQuery 232 // jQuery
237 if(typeof jQuery === 'function') { 233 if(typeof jQuery === 'function') {
238 $.extend({ 234 $.extend({
239 switch: wasSwitch 235 switch: wasSwitch
240 }); 236 });
241 } 237 }
242 // Node.JS 238  
-   239 /*************************************************************************/
-   240 /* Node.JS package export. */
-   241 /*************************************************************************/
243 module.exports = { 242 module.exports.lambda.aggregators = {
244 switch: wasSwitch 243 switch: wasSwitch
245 }; 244 };
Line 246... Line 245...
246   245  
247 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ 246 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
Line 250... Line 249...
250 return yMin + ( 249 return yMin + (
251 ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin ) 250 ( yMax - yMin ) * ( value - xMin ) / ( xMax - xMin )
252 ); 251 );
253 } 252 }
Line -... Line 253...
-   253  
-   254 /*************************************************************************/
-   255 /* Node.JS package export. */
-   256 /*************************************************************************/
-   257 module.exports.formats.kvp = {
-   258 MapValueToRange: wasMapValueToRange
-   259 };
254   260  
255 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */ 261 /* Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 */
256 /*************************************************************************/ 262 /*************************************************************************/
257 function wasHexToRGB(hex) { 263 function wasHexToRGB(hex) {
258 var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; 264 var shortRegEx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
Line 280... Line 286...
280   286  
281   287  
282   288  
283   289  
284   290  
-   291  
-   292  
-   293  
-   294  
-   295  
-   296  
-   297  
-   298