was.js – Diff between revs 21 and 22
?pathlinks?
Rev 21 | Rev 22 | |||
---|---|---|---|---|
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 |
- | ||
7 | if (!Array.prototype.switch) { |
- | ||
8 | Array.prototype.switch = function(d, ...c) { |
- | ||
9 | if(c.length % 2 !== 0) { |
- | ||
10 | throw "Pairs of predicates expected for cases"; |
- | ||
11 | } |
- | ||
12 | |
- | ||
13 | (Array.isArray(this) ? this : [ this ]).forEach((s) => { |
- | ||
14 | var m = false; |
- | ||
15 | for(var i = 0; i < c.length; i += 2) { |
- | ||
16 | if(!c[i](s)) { |
- | ||
17 | continue; |
- | ||
18 | } |
- | ||
19 | if(!c[i + 1](s)) { |
- | ||
20 | continue; |
- | ||
21 | } |
- | ||
22 | m = true; |
- | ||
23 | } |
- | ||
24 | |
- | ||
25 | if(!m) { |
- | ||
26 | d(s); |
- | ||
27 | } |
- | ||
28 | }); |
- | ||
29 | }; |
- | ||
30 | } |
- | ||
31 | // jQuery |
- | ||
32 | $.extend({ |
- | ||
33 | switch: function(q, d, ...c) { |
6 | function wasSwitch(q, d, ...c) { |
|
34 | if(c.length % 2 !== 0) { |
- | ||
35 | throw "Pairs of predicates expected for cases"; |
- | ||
36 | } |
- | ||
37 | |
- | ||
38 | (Array.isArray(q) ? q : [ q ]).forEach((s) => { |
- | ||
39 | var m = false; |
- | ||
40 | for(var i = 0; i < c.length; i += 2) { |
- | ||
41 | if(!c[i](s)) { |
- | ||
42 | continue; |
- | ||
43 | } |
- | ||
44 | if(!c[i + 1](s)) { |
- | ||
45 | continue; |
- | ||
46 | } |
- | ||
47 | m = true; |
- | ||
48 | } |
- | ||
49 | |
- | ||
50 | if(!m) { |
- | ||
51 | d(s); |
- | ||
52 | } |
- | ||
53 | }); |
- | ||
54 | } |
- | ||
55 | }); |
- | ||
56 | // Node.JS |
- | ||
57 | module.exports.switch = function(q, d, ...c) { |
- | ||
58 | if(c.length % 2 !== 0) { |
7 | if(c.length % 2 !== 0) { |
|
59 | throw "Pairs of predicates expected for cases"; |
8 | throw "Pairs of predicates expected for cases"; |
|
60 | } |
9 | } |
|
Line 61... | Line 10... | |||
61 | |
10 | |
|
62 | (Array.isArray(q) ? q : [ q ]).forEach((s) => { |
11 | (Array.isArray(this) ? this : [ this ]).forEach((s) => { |
|
63 | var m = false; |
12 | var m = false; |
|
64 | for(var i = 0; i < c.length; i += 2) { |
13 | for(var i = 0; i < c.length; i += 2) { |
|
65 | if(!c[i](s)) { |
14 | if(!c[i](s)) { |
|
66 | continue; |
15 | continue; |
|
Line 73... | Line 22... | |||
73 | |
22 | |
|
74 | if(!m) { |
23 | if(!m) { |
|
75 | d(s); |
24 | d(s); |
|
76 | } |
25 | } |
|
- | 26 | }); |
||
- | 27 | } |
||
- | 28 | |
||
- | 29 | // Vanilla ES6 JavaScript |
||
- | 30 | if (!Array.prototype.switch) { |
||
- | 31 | Array.prototype.switch = wasSwitch; |
||
- | 32 | } |
||
- | 33 | // jQuery |
||
- | 34 | $.extend({ |
||
- | 35 | switch: wasSwitch |
||
- | 36 | }); |
||
- | 37 | // Node.JS |
||
- | 38 | module.exports = { |
||
77 | }); |
39 | switch: wasSwitch |