Skip to content

Commit df493a4

Browse files
committed
Improve examples
1 parent 69b806f commit df493a4

File tree

12 files changed

+85
-75
lines changed

12 files changed

+85
-75
lines changed

JavaScript/1-wrap.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
'use strict';
22

3-
const wrap = fn => {
4-
console.log('Wrap function:', fn.name);
3+
// const wrap = f => (...args) => f(...args);
4+
5+
const wrap = f => {
6+
console.log('Wrap function:', f.name);
57
return (...args) => {
6-
console.log('Called wrapper for:', fn.name);
8+
console.log('Called wrapper for:', f.name);
79
console.dir({ args });
8-
const result = fn(...args);
9-
console.log('Ended wrapper for:', fn.name);
10+
const result = f(...args);
11+
console.log('Ended wrapper for:', f.name);
1012
console.dir({ result });
1113
return result;
1214
};

JavaScript/2-before-after.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const wrap = (before, after, fn) => (...args) => after(fn(...before(...args)));
3+
const wrap = (before, after, f) => (...args) => after(f(...before(...args)));
44

55
// Usage
66

@@ -24,5 +24,5 @@ wrapped('Uno', 'Due');
2424

2525
console.dir({
2626
func: func.length,
27-
wrapped: wrapped.length
27+
wrapped: wrapped.length,
2828
});

JavaScript/3-callback.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
'use strict';
22

3-
// const wrap = (before, after, fn) =>
4-
// (...args) => after(fn(...before(...args)));
3+
// const wrap = (before, after, f) => (...args) => after(f(...before(...args)));
54

6-
// const wrapAsync = (before, after, beforeCb, afterCb, fn) =>
5+
// const wrapAsync = (before, after, beforeCb, afterCb, f) =>
76
// (...args) => {
8-
// const callback = arr[arr.length -1];
7+
// const callback = arrs[arrs.length -1];
98
// if (typeof callback === 'function') {
109
// args[args.length - 1] = (...pars) =>
1110
// afterCb(callback(...beforeCb(...pars)));
1211
// }
13-
// return after(fn(...before(...args)));
12+
// return after(f(...before(...args)));
1413
// };
1514

16-
const wrapFunction = fn => {
17-
console.log('Wrap function:', fn.name);
15+
const wrapFunction = f => {
16+
console.log('Wrap function:', f.name);
1817
return (...args) => {
19-
console.log('Called wrapper for:', fn.name);
18+
console.log('Called wrapper for:', f.name);
2019
console.dir({ args });
2120
if (args.length > 0) {
2221
const callback = args[args.length - 1];
2322
if (typeof callback === 'function') {
2423
args[args.length - 1] = (...args) => {
25-
console.log('Callback:', fn.name);
24+
console.log('Callback:', f.name);
2625
return callback(...args);
2726
};
2827
}
2928
}
30-
console.log('Call:', fn.name);
29+
console.log('Call:', f.name);
3130
console.dir(args);
32-
const result = fn(...args);
33-
console.log('Ended wrapper for:', fn.name);
31+
const result = f(...args);
32+
console.log('Ended wrapper for:', f.name);
3433
console.dir({ result });
3534
return result;
3635
};

JavaScript/4-wrap-api.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3-
const wrapFunction = fn => {
4-
console.log('Wrap function:', fn.name);
3+
const wrapFunction = f => {
4+
console.log('Wrap function:', f.name);
55
return (...args) => {
6-
console.log('Called wrapper for:', fn.name);
6+
console.log('Called wrapper for:', f.name);
77
console.dir({ args });
8-
const result = fn(...args);
9-
console.log('Ended wrapper for:', fn.name);
8+
const result = f(...args);
9+
console.log('Ended wrapper for:', f.name);
1010
console.dir({ result });
1111
return result;
1212
};
@@ -24,11 +24,20 @@ const cloneInterface = anInterface => {
2424
// Usage
2525

2626
const interfaceName = {
27-
methodName(par1, par2) {
27+
methodSync(par1, par2) {
2828
console.dir({ method: { par1, par2 } });
2929
return [par1, par2];
30+
},
31+
methodAsync(par1, par2, callback) {
32+
console.dir({ method: { par1, par2 } });
33+
callback(null, { field: 'value' });
3034
}
3135
};
3236

3337
const cloned = cloneInterface(interfaceName);
34-
cloned.methodName('Uno', 'Due');
38+
cloned.methodSync('Uno', 'Due');
39+
40+
cloned.methodAsync('Tre', 'Quattro', (err, res) => {
41+
if (err) throw err;
42+
console.dir({ res });
43+
});

JavaScript/5-timeout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Wrapper will prevent call after timeout
44

5-
const timeout = (msec, fn) => {
5+
const timeout = (msec, f) => {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timedout');
88
timer = null;
@@ -11,7 +11,7 @@ const timeout = (msec, fn) => {
1111
if (timer) {
1212
clearTimeout(timer);
1313
timer = null;
14-
return fn(...args);
14+
return f(...args);
1515
}
1616
};
1717
};

JavaScript/6-timeout-async.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Wrapper will prevent call after timeout
44

5-
const timeout = (msec, fn) => {
5+
const timeout = (msec, f) => {
66
let timer = setTimeout(() => {
77
if (timer) console.log('Function timedout');
88
timer = null;
@@ -11,7 +11,7 @@ const timeout = (msec, fn) => {
1111
if (timer) {
1212
clearTimeout(timer);
1313
timer = null;
14-
return fn(...args);
14+
return f(...args);
1515
}
1616
};
1717
};

JavaScript/7-once.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
// Wrapper will prevent calls > n
44

5-
const once = fn => (...args) => {
6-
if (!fn) return;
7-
const res = fn(...args);
8-
fn = null;
5+
const once = f => (...args) => {
6+
if (!f) return;
7+
const res = f(...args);
8+
f = null;
99
return res;
1010
};
1111

@@ -15,7 +15,7 @@ const fn = par => {
1515
console.log('Function called, par:', par);
1616
};
1717

18-
const f = once(fn);
18+
const fn2 = once(fn);
1919

20-
f('first');
21-
f('second');
20+
fn2('first');
21+
fn2('second');

JavaScript/8-limit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
// Wrapper will prevent calls > n
44

5-
const limit = (count, fn) => {
5+
const limit = (count, f) => {
66
let counter = 0;
77
return (...args) => {
88
if (counter === count) return;
99
counter++;
10-
return fn(...args);
10+
return f(...args);
1111
};
1212
};
1313

JavaScript/9-cancelable.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
const cancelable = fn => {
3+
const cancelable = f => {
44
const wrapper = (...args) => {
5-
if (fn) return fn(...args);
5+
if (f) return f(...args);
66
};
7-
wrapper.cancel = () => fn = null;
7+
wrapper.cancel = () => f = null;
88
return wrapper;
99
};
1010

@@ -14,8 +14,8 @@ const fn = par => {
1414
console.log('Function called, par:', par);
1515
};
1616

17-
const f = cancelable(fn);
17+
const f2 = cancelable(fn);
1818

19-
f('first');
20-
f.cancel();
21-
f('second');
19+
f2('first');
20+
f2.cancel();
21+
f2('second');

JavaScript/a-wrap.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
'use strict';
22

3-
const wrap = fn => {
3+
const wrap = f => {
44
let limit = 0;
55
let counter = 0;
66

77
const wrapper = (...args) => {
88
if (limit && counter === limit) wrapper.cancel();
9-
if (fn) {
10-
const res = fn(...args);
9+
if (f) {
10+
const res = f(...args);
1111
counter++;
1212
return res;
1313
}
1414
};
1515

1616
wrapper.cancel = () => {
17-
fn = null;
17+
f = null;
1818
return wrapper;
1919
};
2020

@@ -39,12 +39,12 @@ const fn = par => {
3939
console.log('Function called, par:', par);
4040
};
4141

42-
const f = wrap(fn).timeout(200).limit(3);
43-
f('1st');
42+
const fn2 = wrap(fn).timeout(200).limit(3);
43+
fn2('1st');
4444

4545
setTimeout(() => {
46-
f('2nd');
47-
f('3rd');
48-
f.cancel();
49-
f('4th');
46+
fn2('2nd');
47+
fn2('3rd');
48+
fn2.cancel();
49+
fn2('4th');
5050
}, 150);

0 commit comments

Comments
 (0)