From e16b03b841b20e510a203cf7d213d4d75d9d21df Mon Sep 17 00:00:00 2001 From: Nikita Lebedev Date: Sat, 9 Sep 2017 06:07:29 +0500 Subject: [PATCH] [guide] require array spread operator or `Array.from` Fixes #1084. --- README.md | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e03261eb..c9c412e8 100644 --- a/README.md +++ b/README.md @@ -364,15 +364,31 @@ Other Style Guides ``` - - [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). + - [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). ```javascript const foo = document.querySelectorAll('.foo'); + + // good const nodes = Array.from(foo); + + // best + const nodes = [...foo]; + ``` + + + - [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. + + ```javascript + // bad + const bar = [...foo].map(bar); + + // good + const bar = Array.from(foo, bar); ``` - - [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) + - [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) ```javascript // good @@ -420,8 +436,8 @@ Other Style Guides }); ``` - - - [4.6](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines + + - [4.7](#arrays--bracket-newline) Use line breaks after open and before close array brackets if an array has multiple lines ```javascript // bad @@ -2511,7 +2527,7 @@ Other Style Guides ## Commas - + - [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) ```javascript