was.js – Blame information for rev 21
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
14 | office | 1 | /*************************************************************************/ |
2 | /* Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 */ |
||
3 | /*************************************************************************/ |
||
4 | /* fuss/lambda_calculus/functional_programming/aggregators @ grimore.org */ |
||
5 | /*************************************************************************/ |
||
19 | office | 6 | // Vanilla ES6 JavaScript |
14 | office | 7 | if (!Array.prototype.switch) { |
15 | office | 8 | Array.prototype.switch = function(d, ...c) { |
17 | office | 9 | if(c.length % 2 !== 0) { |
14 | office | 10 | throw "Pairs of predicates expected for cases"; |
17 | office | 11 | } |
14 | office | 12 | |
15 | office | 13 | (Array.isArray(this) ? this : [ this ]).forEach((s) => { |
14 | office | 14 | var m = false; |
17 | office | 15 | for(var i = 0; i < c.length; i += 2) { |
16 | if(!c[i](s)) { |
||
14 | office | 17 | continue; |
17 | office | 18 | } |
19 | if(!c[i + 1](s)) { |
||
14 | office | 20 | continue; |
17 | office | 21 | } |
14 | office | 22 | m = true; |
23 | } |
||
24 | |||
17 | office | 25 | if(!m) { |
26 | d(s); |
||
27 | } |
||
14 | office | 28 | }); |
29 | }; |
||
30 | } |
||
19 | office | 31 | // jQuery |
14 | office | 32 | $.extend({ |
15 | office | 33 | switch: function(q, d, ...c) { |
17 | office | 34 | if(c.length % 2 !== 0) { |
14 | office | 35 | throw "Pairs of predicates expected for cases"; |
17 | office | 36 | } |
14 | office | 37 | |
15 | office | 38 | (Array.isArray(q) ? q : [ q ]).forEach((s) => { |
14 | office | 39 | var m = false; |
17 | office | 40 | for(var i = 0; i < c.length; i += 2) { |
41 | if(!c[i](s)) { |
||
14 | office | 42 | continue; |
17 | office | 43 | } |
44 | if(!c[i + 1](s)) { |
||
14 | office | 45 | continue; |
17 | office | 46 | } |
14 | office | 47 | m = true; |
48 | } |
||
49 | |||
17 | office | 50 | if(!m) { |
51 | d(s); |
||
52 | } |
||
14 | office | 53 | }); |
54 | } |
||
55 | }); |
||
19 | office | 56 | // Node.JS |
21 | office | 57 | module.exports.switch = function(q, d, ...c) { |
19 | office | 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 | }); |
||
20 | office | 78 | }; |