Skip to content

Commit 57d5e0d

Browse files
committed
[guide] Spread for convert array-like objects, Array.from for mapping airbnb#1084
1 parent 9dee847 commit 57d5e0d

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ before_install:
99
- 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ]; then npm install -g [email protected] ; elif [ "${TRAVIS_NODE_VERSION}" != "0.9" ]; then case "$(npm --version)" in 1.*) npm install -g [email protected] ;; 2.*) npm install -g npm@2 ;; esac ; fi'
1010
- 'if [ "${TRAVIS_NODE_VERSION%${TRAVIS_NODE_VERSION#[0-9]}}" = "0" ] || [ "${TRAVIS_NODE_VERSION:0:4}" = "iojs" ]; then npm install -g [email protected] ; elif [ "${TRAVIS_NODE_VERSION}" != "0.6" ] && [ "${TRAVIS_NODE_VERSION}" != "0.9" ]; then npm install -g npm; fi'
1111
install:
12-
- 'cd "packages/${PACKAGE}"'
12+
- 'if [ -n "${PACKAGE-}" ]; then cd "packages/${PACKAGE}"; fi'
1313
- 'if [ "${TRAVIS_NODE_VERSION}" = "0.6" ]; then nvm install 0.8 && npm install -g [email protected] && npm install -g [email protected] && npm install -g npm@2 && npm install && nvm use "${TRAVIS_NODE_VERSION}"; else npm install; fi;'
1414
- 'if [ -n "${ESLINT}" ]; then npm install --no-save "eslint@${ESLINT}"; fi'
1515
script:
16-
- 'if [ -n "${PREPUBLISH-}" ]; then npm run pretravis && npm run prepublish && npm run posttravis; else npm run travis; fi'
16+
- 'if [ -n "${PREPUBLISH-}" ]; then npm run pretravis && npm run prepublish && npm run posttravis; elif [ -n "${LINT-}" ]; then npm run lint; else npm run travis; fi'
1717
sudo: false
1818
env:
1919
matrix:
@@ -26,6 +26,8 @@ matrix:
2626
env: PREPUBLISH=true ESLINT=4 PACKAGE=eslint-config-airbnb
2727
- node_js: "node"
2828
env: PREPUBLISH=true ESLINT=4 PACKAGE=eslint-config-airbnb-base
29+
- node_js: "node"
30+
env: LINT=true
2931
allow_failures:
3032
- node_js: "7"
3133
- node_js: "5"

README.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,31 @@ Other Style Guides
364364
```
365365

366366
<a name="arrays--from"></a><a name="4.4"></a>
367-
- [4.4](#arrays--from) To convert an array-like object to an array, use [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
367+
- [4.4](#arrays--from) To convert an array-like object to an array, use spreads `...` instead of [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from).
368368

369369
```javascript
370370
const foo = document.querySelectorAll('.foo');
371+
372+
// good
371373
const nodes = Array.from(foo);
374+
375+
// best
376+
const nodes = [...foo];
377+
```
378+
379+
<a name="arrays--mapping"></a>
380+
- [4.5](#arrays--mapping) Use [Array.from](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/from) instead of spread `...` for mapping over iterables, because it avoids creating an intermediate array.
381+
382+
```javascript
383+
// bad
384+
const bar = [...foo].map(mapper);
385+
386+
// good
387+
const bar = Array.from(foo, mapper);
372388
```
373389

374390
<a name="arrays--callback-return"></a><a name="4.5"></a>
375-
- [4.5](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return)
391+
- [4.6](#arrays--callback-return) Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following [8.2](#arrows--implicit-return). eslint: [`array-callback-return`](http://eslint.org/docs/rules/array-callback-return)
376392

377393
```javascript
378394
// good
@@ -420,8 +436,8 @@ Other Style Guides
420436
});
421437
```
422438
423-
<a name="arrays--bracket-newline"></a>
424-
- [4.6](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
439+
<a name="arrays--bracket-newline"></a>
440+
- [4.7](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines
425441
426442
```javascript
427443
// bad
@@ -2511,7 +2527,7 @@ Other Style Guides
25112527
25122528
## Commas
25132529
2514-
<a name="commas--leading-trailing"></a><a name="19.1"></a>
2530+
<a name="commas--leading-trailing"></a><a name="19.1"></a>
25152531
- [20.1](#commas--leading-trailing) Leading commas: **Nope.** eslint: [`comma-style`](http://eslint.org/docs/rules/comma-style.html) jscs: [`requireCommaBeforeLineBreak`](http://jscs.info/rule/requireCommaBeforeLineBreak)
25162532
25172533
```javascript
@@ -3169,7 +3185,7 @@ Other Style Guides
31693185
eslint: [`no-restricted-globals`](http://eslint.org/docs/rules/no-restricted-globals)
31703186
31713187
> Why? The global `isNaN` coerces non-numbers to numbers, returning true for anything that coerces to NaN.
3172-
If this behavior is desired, make it explicit.
3188+
> If this behavior is desired, make it explicit.
31733189
31743190
```javascript
31753191
// bad
@@ -3186,7 +3202,7 @@ Other Style Guides
31863202
eslint: [`no-restricted-globals`](http://eslint.org/docs/rules/no-restricted-globals)
31873203
31883204
> Why? The global `isFinite` coerces non-numbers to numbers, returning true for anything that coerces to a finite number.
3189-
If this behavior is desired, make it explicit.
3205+
> If this behavior is desired, make it explicit.
31903206
31913207
```javascript
31923208
// bad

packages/eslint-config-airbnb-base/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
12.0.0 / 2017-09-02
22
==================
33
- [deps] [breaking] require `eslint` v4
4-
- enable `function-paren-newline`, `for-direction`, `getter-return`, `no-compare-neg-zero`, `semi-style`, `object-curly-newline`, `no-buffer-constructor`, `no-restricted-globals`, `switch-colon-spacing`, `template-tag-spacing`, `prefer-promise-reject-errors`, `prefer-restructuring`
4+
- enable `function-paren-newline`, `for-direction`, `getter-return`, `no-compare-neg-zero`, `semi-style`, `object-curly-newline`, `no-buffer-constructor`, `no-restricted-globals`, `switch-colon-spacing`, `template-tag-spacing`, `prefer-promise-reject-errors`, `prefer-destructuring`
55
- improve `indent`, `no-multi-spaces`, `no-trailing-spaces`, `no-underscore-dangle`
66
- [breaking] move `comma-dangle` to Stylistic Issues (#1514)
77
- [breaking] Rules prohibiting global isNaN, isFinite (#1477)

0 commit comments

Comments
 (0)