was.js – Diff between revs 29 and 30
?pathlinks?
Rev 29 | Rev 30 | |||
---|---|---|---|---|
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 | // Vanilla ES6 JavaScript |
|
7 | function wasSwitch(q, d, ...c) { |
7 | function wasSwitch(q, d, ...c) { |
|
8 | if(c.length % 2 !== 0) { |
8 | if(c.length % 2 !== 0) { |
|
9 | throw "Pairs of predicates expected for cases"; |
9 | throw "Pairs of predicates expected for cases"; |
|
10 | } |
10 | } |
|
11 | |
11 | |
|
12 | (Array.isArray(q) ? q : [ q ]).forEach((s) => { |
12 | (Array.isArray(q) ? q : [ q ]).forEach((s) => { |
|
13 | var m = false; |
13 | var m = false; |
|
14 | for(var i = 0; i < c.length; i += 2) { |
14 | for(var i = 0; i < c.length; i += 2) { |
|
15 | if(!c[i](s)) { |
15 | if(!c[i](s)) { |
|
16 | continue; |
16 | continue; |
|
17 | } |
17 | } |
|
18 | if(!c[i + 1](s)) { |
18 | if(!c[i + 1](s)) { |
|
19 | continue; |
19 | continue; |
|
20 | } |
20 | } |
|
21 | m = true; |
21 | m = true; |
|
22 | } |
22 | } |
|
23 | |
23 | |
|
24 | if(!m) { |
24 | if(!m) { |
|
25 | d(s); |
25 | d(s); |
|
26 | } |
26 | } |
|
27 | }); |
27 | }); |
|
28 | } |
28 | } |
|
29 | if (!Array.prototype.switch) { |
29 | if (!Array.prototype.switch) { |
|
30 | Array.prototype.switch = function(d, ...c) { |
30 | Array.prototype.switch = function(d, ...c) { |
|
31 | wasSwitch(this, d, ...c) |
31 | wasSwitch(this, d, ...c); |
|
32 | }; |
32 | }; |
|
33 | } |
33 | } |
|
34 | // jQuery |
34 | // jQuery |
|
35 | if(typeof jQuery === 'function') { |
35 | if(typeof jQuery === 'function') { |
|
36 | $.extend({ |
36 | $.extend({ |
|
37 | switch: wasSwitch |
37 | switch: wasSwitch |
|
38 | }); |
38 | }); |
|
39 | } |
39 | } |
|
40 | |
40 | |
|
41 | /*************************************************************************/ |
41 | /*************************************************************************/ |
|
42 | /* Node.JS package export. */ |
42 | /* Node.JS package export. */ |
|
43 | /*************************************************************************/ |
43 | /*************************************************************************/ |
|
44 | module.exports.lambda = { |
44 | module.exports.lambda = { |
|
45 | switch: wasSwitch |
45 | switch: wasSwitch |
|
46 | }; |
46 | }; |
|
47 | |
47 | |