Skip to content

Commit 5febc5d

Browse files
committed
Merge branch 'dev' into feature/patterns
2 parents 7109c7c + 9c7db3a commit 5febc5d

26 files changed

Lines changed: 1643 additions & 1591 deletions

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v11.10.1

.travis.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
language: node_js
22

3-
node_js:
4-
- '11'
5-
63
branches:
74
only:
85
- master
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
const CHAIN_OF_RESPONSIBILITY = {
2+
id: 'chain_of_responsibility',
3+
name: 'Chain of Responsibility',
4+
type: 'behavioral',
5+
hint: 'delegates commands to a chain of processing objects',
6+
codeES5: `function ShoppingCart() {
7+
this.products = [];
8+
9+
this.addProduct = function(p) {
10+
this.products.push(p);
11+
};
12+
}
13+
14+
function Discount() {
15+
this.calc = function(products) {
16+
var ndiscount = new NumberDiscount();
17+
var pdiscount = new PriceDiscount();
18+
var none = new NoneDiscount();
19+
20+
ndiscount.setNext(pdiscount);
21+
pdiscount.setNext(none);
22+
23+
return ndiscount.exec(products);
24+
};
25+
}
26+
27+
function NumberDiscount() {
28+
this.next = null;
29+
this.setNext = function(fn) {
30+
this.next = fn;
31+
};
32+
33+
this.exec = function(products) {
34+
var result = 0;
35+
if (products.length > 3) result = 0.05;
36+
37+
return result + this.next.exec(products);
38+
};
39+
}
40+
41+
function PriceDiscount() {
42+
this.next = null;
43+
this.setNext = function(fn) {
44+
this.next = fn;
45+
};
46+
this.exec = function(products) {
47+
var result = 0;
48+
var total = products.reduce(function(a, b) {
49+
return a + b;
50+
});
51+
52+
if (total >= 500) result = 0.1;
53+
54+
return result + this.next.exec(products);
55+
};
56+
}
57+
58+
function NoneDiscount() {
59+
this.exec = function() {
60+
return 0;
61+
};
62+
}
63+
64+
module.exports = [ShoppingCart, Discount];`,
65+
codeES6: `class ShoppingCart {
66+
constructor() {
67+
this.products = [];
68+
}
69+
70+
addProduct(p) {
71+
this.products.push(p);
72+
}
73+
}
74+
75+
class Discount {
76+
calc(products) {
77+
let ndiscount = new NumberDiscount();
78+
let pdiscount = new PriceDiscount();
79+
let none = new NoneDiscount();
80+
ndiscount.setNext(pdiscount);
81+
pdiscount.setNext(none);
82+
83+
return ndiscount.exec(products);
84+
}
85+
}
86+
87+
class NumberDiscount {
88+
constructor() {
89+
this.next = null;
90+
}
91+
92+
setNext(fn) {
93+
this.next = fn;
94+
}
95+
96+
exec(products) {
97+
let result = 0;
98+
if (products.length > 3) result = 0.05;
99+
100+
return result + this.next.exec(products);
101+
}
102+
}
103+
104+
class PriceDiscount {
105+
constructor() {
106+
this.next = null;
107+
}
108+
109+
setNext(fn) {
110+
this.next = fn;
111+
}
112+
113+
exec(products) {
114+
let result = 0;
115+
let total = products.reduce((a, b) => a + b);
116+
117+
if (total >= 500) result = 0.1;
118+
119+
return result + this.next.exec(products);
120+
}
121+
}
122+
123+
class NoneDiscount {
124+
exec() {
125+
return 0;
126+
}
127+
}
128+
129+
export { ShoppingCart, Discount };`
130+
};
131+
132+
export default CHAIN_OF_RESPONSIBILITY;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
const COMMAND = {
2+
id: 'command',
3+
name: 'Command',
4+
type: 'behavioral',
5+
hint: 'creates objects which encapsulate actions and parameters',
6+
codeES5: `function Cockpit(instruction) {
7+
this.instruction = instruction;
8+
}
9+
10+
Cockpit.prototype.execute = function() {
11+
this.instruction.execute();
12+
};
13+
14+
function Turbine() {
15+
this.speed = 0;
16+
this.state = false;
17+
}
18+
19+
Turbine.prototype.on = function() {
20+
this.state = true;
21+
this.speed = 100;
22+
};
23+
24+
Turbine.prototype.off = function() {
25+
this.speed = 0;
26+
this.state = false;
27+
};
28+
29+
Turbine.prototype.speedDown = function() {
30+
if (!this.state) return;
31+
32+
this.speed -= 100;
33+
};
34+
35+
Turbine.prototype.speedUp = function() {
36+
if (!this.state) return;
37+
38+
this.speed += 100;
39+
};
40+
41+
function OnInstruction(turbine) {
42+
this.turbine = turbine;
43+
}
44+
45+
OnInstruction.prototype.execute = function() {
46+
this.turbine.on();
47+
};
48+
49+
function OffInstruction(turbine) {
50+
this.turbine = turbine;
51+
}
52+
53+
OffInstruction.prototype.execute = function() {
54+
this.turbine.off();
55+
};
56+
57+
function SpeedUpInstruction(turbine) {
58+
this.turbine = turbine;
59+
}
60+
61+
SpeedUpInstruction.prototype.execute = function() {
62+
this.turbine.speedUp();
63+
};
64+
65+
function SpeedDownInstruction(turbine) {
66+
this.turbine = turbine;
67+
}
68+
69+
SpeedDownInstruction.prototype.execute = function() {
70+
this.turbine.speedDown();
71+
};
72+
73+
module.exports = [Cockpit, Turbine, OnInstruction, OffInstruction, SpeedUpInstruction, SpeedDownInstruction];`,
74+
codeES6: `class Cockpit {
75+
constructor(instruction) {
76+
this.instruction = instruction;
77+
}
78+
79+
execute() {
80+
this.instruction.execute();
81+
}
82+
}
83+
84+
class Turbine {
85+
constructor() {
86+
this.state = false;
87+
}
88+
89+
on() {
90+
this.state = true;
91+
}
92+
93+
off() {
94+
this.state = false;
95+
}
96+
}
97+
98+
class OnInstruction {
99+
constructor(turbine) {
100+
this.turbine = turbine;
101+
}
102+
103+
execute() {
104+
this.turbine.on();
105+
}
106+
}
107+
108+
class OffInstruction {
109+
constructor(turbine) {
110+
this.turbine = turbine;
111+
}
112+
113+
execute() {
114+
this.turbine.off();
115+
}
116+
}
117+
118+
export { Cockpit, Turbine, OnInstruction, OffInstruction };`
119+
};
120+
121+
export default COMMAND;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const INTERPRETER = {
2+
id: 'interpteter',
3+
name: 'Interpreter',
4+
type: 'behavioral',
5+
hint: 'implements a specialized language',
6+
codeES5: `function Sum(left, right) {
7+
this.left = left;
8+
this.right = right;
9+
}
10+
11+
Sum.prototype.pattern = function() {
12+
return this.left.pattern() + this.right.pattern();
13+
};
14+
15+
function Min(left, right) {
16+
this.left = left;
17+
this.right = right;
18+
}
19+
20+
Min.prototype.pattern = function() {
21+
return this.left.pattern() - this.right.pattern();
22+
};
23+
24+
function Num(val) {
25+
this.val = val;
26+
}
27+
28+
Num.prototype.pattern = function() {
29+
return this.val;
30+
};
31+
32+
module.exports = [Num, Min, Sum];`,
33+
codeES6: `class Sum {
34+
constructor(left, right) {
35+
this.left = left;
36+
this.right = right;
37+
}
38+
39+
pattern() {
40+
return this.left.pattern() + this.right.pattern();
41+
}
42+
}
43+
44+
class Min {
45+
constructor(left, right) {
46+
this.left = left;
47+
this.right = right;
48+
}
49+
50+
pattern() {
51+
return this.left.pattern() - this.right.pattern();
52+
}
53+
}
54+
55+
class Num {
56+
constructor(val) {
57+
this.val = val;
58+
}
59+
60+
pattern() {
61+
return this.val;
62+
}
63+
}
64+
65+
export { Num, Min, Sum };`
66+
};
67+
68+
export default INTERPRETER;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const ITERATOR = {
2+
id: 'iterator',
3+
name: 'Iterator',
4+
type: 'behavioral',
5+
hint:
6+
'accesses the elements of an object sequentially without exposing its underlying representation',
7+
codeES5: `function Pattern(el) {
8+
this.index = 0;
9+
this.elements = el;
10+
}
11+
12+
Pattern.prototype = {
13+
next: function() {
14+
return this.elements[this.index++];
15+
},
16+
hasNext: function() {
17+
return this.index < this.elements.length;
18+
}
19+
};
20+
21+
module.exports = Pattern;`,
22+
codeES6: `class Pattern {
23+
constructor(el) {
24+
this.index = 0;
25+
this.elements = el;
26+
}
27+
28+
next() {
29+
return this.elements[this.index++];
30+
}
31+
32+
hasNext() {
33+
return this.index < this.elements.length;
34+
}
35+
}
36+
37+
export default Pattern;`
38+
};
39+
40+
export default ITERATOR;

0 commit comments

Comments
 (0)