was.js – Diff between revs 17 and 19
?pathlinks?
Rev 17 | Rev 19 | |||
---|---|---|---|---|
Line 1... | Line 1... | |||
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 | } |
|
Line 25... | Line 26... | |||
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 | } |
|
Line 55... | Line 51... | |||
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 | } |