-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathproxy.js
129 lines (87 loc) · 2.28 KB
/
proxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Ejemplo base */
class subject{
hello(){
return 'hello';
}
goodbye(){
return 'Goodbye people!';
}
}
class Proxy(subject){
constructor(subject){
this.subject = subject;
}
hello(){
return this.subject.hello()+' world!';
}
goodbye(){
return this.subject.goodbye();
}
}
let tmpsubj = new subject();
let proxy = new Proxy(tmpsubj);
console.log(proxy.hello());
console.log(proxy.goodbye());
/* Ejemplo Data Validation */
class calculator{
sumCalc(a,b){
return a+b;
}
difCalc(a,b){
return a-b;
}
}
class validatorCalc{
constructor(subject){
this.subject = subject;
}
sumar(numA,numB){
if ((typeof numA=="number")&&(typeof numB=="number"))
return this.subject.sumCalc(numA,numB);
else
throw "Parametros no numericos";
}
restar(numA,numB){
if ((typeof numA=="number")&&(typeof numB=="number"))
return this.subject.difCalc(numA,numB);
else
throw "Parametros no numericos";
}
}
let calculator = new calculator();
let validator = new validatorCalc(calculator);
console.log(validator.sumar(2,5));
console.log(validator.restar(80,5));
/* Ejemplo log */
function createLoggingWritable(writableOrig) {
var proto = Object.getPrototypeOf(writableOrig);
function LoggingWritable(subject){
this.writableOrig = writableOrig;
}
LoggingWritable.prototype = Object.create(proto);
LoggingWritable.prototype.write = function(chunk, encoding, callback) {
if(!callback && typeof encoding === 'function') {
callback = encoding;
encoding = undefined;
}
console.log('Writing ', chunk);
return this.writableOrig.write(chunk, encoding, function() {
console.log('Finished writing ', chunk);
callback && callback();
});
};
LoggingWritable.prototype.on = function() {
return this.writableOrig.on.apply(this.writableOrig, arguments);
};
LoggingWritable.prototype.end = function() {
return this.writableOrig.end.apply(this.writableOrig, arguments);
}
return new LoggingWritable(this.writableOrig);
}
var fs = require('fs');
var writable = fs.createWriteStream('test.txt');
var writableProxy = createProxy(writable);
writableProxy.write('First chunk');
writableProxy.write('Second chunk');
writable.write('This is not logged');
writableProxy.end();