was.js – Diff between revs 21 and 22

Subversion Repositories:
Rev:
Only display areas with differencesRegard whitespace
Rev 21 Rev 22
1 /*************************************************************************/ 1 /*************************************************************************/
2 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ 2 /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */
3 /*************************************************************************/ 3 /*************************************************************************/
4 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ 4 /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */
5 /*************************************************************************/ 5 /*************************************************************************/
6 // Vanilla ES6 JavaScript -  
7 if (!Array.prototype.switch) { -  
8 Array.prototype.switch = function(d, ...c) { 6 function wasSwitch(q, d, ...c) {
9 if(c.length % 2 !== 0) { 7 if(c.length % 2 !== 0) {
10 throw "Pairs of predicates expected for cases"; 8 throw "Pairs of predicates expected for cases";
11 } 9 }
12 10
13 (Array.isArray(this) ? this : [ this ]).forEach((s) => { 11 (Array.isArray(this) ? this : [ this ]).forEach((s) => {
14 var m = false; 12 var m = false;
15 for(var i = 0; i < c.length; i += 2) { 13 for(var i = 0; i < c.length; i += 2) {
16 if(!c[i](s)) { 14 if(!c[i](s)) {
17 continue; 15 continue;
18 } 16 }
19 if(!c[i + 1](s)) { 17 if(!c[i + 1](s)) {
20 continue; 18 continue;
21 } 19 }
22 m = true; 20 m = true;
23 } 21 }
24 22
25 if(!m) { 23 if(!m) {
26 d(s); 24 d(s);
27 } 25 }
28 }); 26 });
29 }; -  
30 } -  
31 // jQuery -  
32 $.extend({ -  
33 switch: function(q, d, ...c) { -  
34 if(c.length % 2 !== 0) { -  
35 throw "Pairs of predicates expected for cases"; -  
36 } 27 }
37 -  
38 (Array.isArray(q) ? q : [ q ]).forEach((s) => { 28  
39 var m = false; -  
40 for(var i = 0; i < c.length; i += 2) { 29 // Vanilla ES6 JavaScript
41 if(!c[i](s)) { -  
42 continue; -  
43 } 30 if (!Array.prototype.switch) {
44 if(!c[i + 1](s)) { -  
45 continue; -  
46 } -  
47 m = true; -  
48 } -  
49 -  
50 if(!m) { -  
51 d(s); -  
52 } -  
53 }); 31 Array.prototype.switch = wasSwitch;
-   32 }
-   33 // jQuery
-   34 $.extend({
54 } 35 switch: wasSwitch
55 }); 36 });
56 // Node.JS 37 // Node.JS
57 module.exports.switch = function(q, d, ...c) { -  
58 if(c.length % 2 !== 0) { -  
59 throw "Pairs of predicates expected for cases"; -  
60 } -  
61 -  
62 (Array.isArray(q) ? q : [ q ]).forEach((s) => { -  
63 var m = false; -  
64 for(var i = 0; i < c.length; i += 2) { -  
65 if(!c[i](s)) { -  
66 continue; -  
67 } -  
68 if(!c[i + 1](s)) { -  
69 continue; -  
70 } 38 module.exports = {
71 m = true; -  
72 } -  
73 -  
74 if(!m) { -  
75 d(s); -  
76 } -  
77 }); 39 switch: wasSwitch
78 }; 40 };
79   41