-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterators-Generators.js
More file actions
97 lines (75 loc) · 1.88 KB
/
Iterators-Generators.js
File metadata and controls
97 lines (75 loc) · 1.88 KB
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
//: 11.1 => Don't use iterators. Prefer JavaScript higher-order function instead of loops like for-in or for-of. eslint: no-iterator, no-restricted-syntax
/* Why? This inforces out immutable rule. Dealing with pure function that return values is easier to reason about than side effects. */
/* Use map() / every() / filter() / find() / findIndex() / reduce() / some() to iterate over array, and Object.keys() / Object.values() / Object.entries() to preduce array sp you can iterate over object. */
const numbers = [1, 2, 3, 4, 5, 6];
// bad
let sum = 0;
for (let num of numbers) {
sum += num;
}
sum === 21;
// good
let sum2 = 0;
numbers.forEach((num) => {
sum2 += num;
});
sum2 === 21;
// best (use the functional force)
const sum3 = numbers.reduce((total, num) => total + num, 0);
sum3 === 21;
// bad
const increasedByOne = [];
for (let i; i < numbers.length; i++) {
increasedByOne.push(numbers[i] + 1);
}
// good
const increasedByOne2 = [];
numbers.forEach((num) => {
increasedByOne2.push(num + 1);
});
// best
const increasedByOne3 = numbers.map((num) => num + 1);
//: 11.2 => Don't use generators for now
/* Why? They don't transpile well to ES5 */
//: 11.3 => /* If you must use generators, or if disregard our advice, make sure their function signature is spaced properly. eslint: generators-star-spacing */
/* Why? function and * are part of the same conceptupal keyword- * is not a modifier for function, function* is a unique construct, different from function. */
// bad
function* foo() {
// ...
}
// bad
const bar = function* () {
// ...
};
// bad
const baz = function* () {
// ...
};
// bad
const quux = function* () {
// ...
};
// bad
function* foo() {
// ...
}
// bad
function* foo() {
// ...
}
// very bad
function* foo() {
// ...
}
// very bad
const wat = function* () {
// ...
};
// good
function* foo() {
// ...
}
// good
const foo = function* () {
// ...
};