was.js – Diff between revs 17 and 19

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