Skip to content

Commit c014264

Browse files
committed
Improve examples
1 parent 5982063 commit c014264

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

JavaScript/1-wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ const func = (par1, par2) => {
1919
return [par1, par2];
2020
};
2121

22-
const cloned = wrap(func);
23-
cloned('Uno', 'Due');
22+
const wrapped = wrap(func);
23+
wrapped('Uno', 'Due');

JavaScript/2-before-after.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ const after = (...args) => {
2121
return args;
2222
};
2323

24-
const cloned = wrap(before, after, func);
25-
cloned('Uno', 'Due');
24+
const wrapped = wrap(before, after, func);
25+
wrapped('Uno', 'Due');
26+
27+
console.dir({
28+
func: func.length,
29+
wrapped: wrapped.length
30+
});
31+

JavaScript/7-once.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
// Wrapper will prevent calls > n
44

5-
const once = (fn) => {
6-
let finished = false;
7-
return (...args) => {
8-
if (finished) return;
9-
const res = fn(...args);
10-
finished = true;
11-
return res;
12-
};
5+
const once = fn => (...args) => {
6+
if (!fn) return;
7+
const res = fn(...args);
8+
fn = null;
9+
return res;
1310
};
1411

1512
// Usage

JavaScript/8-limit.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ const limit = (count, fn) => {
66
let counter = 0;
77
return (...args) => {
88
if (counter === count) return;
9-
const res = fn(...args);
109
counter++;
11-
return res;
10+
return fn(...args);
1211
};
1312
};
1413

JavaScript/9-cancelable.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ const cancelable = (fn) => {
44
const wrapper = (...args) => {
55
if (fn) return fn(...args);
66
};
7-
wrapper.cancel = () => {
8-
fn = null;
9-
return wrapper;
10-
};
7+
wrapper.cancel = () => fn = null;
118
return wrapper;
129
};
1310

@@ -18,7 +15,7 @@ const fn = (par) => {
1815
};
1916

2017
const f = cancelable(fn);
21-
f('first');
2218

19+
f('first');
2320
f.cancel();
2421
f('second');

0 commit comments

Comments
 (0)