From a3f426803bdb5990593a5dd0133122f8c553e49d Mon Sep 17 00:00:00 2001 From: Rezvan Mahdavi Hezaveh <31081679+rmahdav@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:06:09 -0700 Subject: [PATCH 1/5] Add iterator helpers blog (#747) --- src/_img/avatars/rezvan-mahdavi-hezaveh.avif | Bin 0 -> 989 bytes src/_img/avatars/rezvan-mahdavi-hezaveh.jpg | Bin 0 -> 3015 bytes .../avatars/rezvan-mahdavi-hezaveh@2x.avif | Bin 0 -> 1803 bytes .../avatars/rezvan-mahdavi-hezaveh@2x.jpg | Bin 0 -> 5988 bytes src/features/iterator-helpers.md | 189 ++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 src/_img/avatars/rezvan-mahdavi-hezaveh.avif create mode 100644 src/_img/avatars/rezvan-mahdavi-hezaveh.jpg create mode 100644 src/_img/avatars/rezvan-mahdavi-hezaveh@2x.avif create mode 100644 src/_img/avatars/rezvan-mahdavi-hezaveh@2x.jpg create mode 100644 src/features/iterator-helpers.md diff --git a/src/_img/avatars/rezvan-mahdavi-hezaveh.avif b/src/_img/avatars/rezvan-mahdavi-hezaveh.avif new file mode 100644 index 0000000000000000000000000000000000000000..ffbfc877ba1bf0a8e829cf8c855346f85007cb42 GIT binary patch literal 989 zcmZQzU{FXasVqn=%S>Yc0uY^>nP!-qnV9D5Xy^nK`jnemk_eIm0*#E6oFWL5fuSHX zxdg@r(K(q(Fk|=%GD~v7a*RMyE;A=T8N_p8U|1git>_kDj1l6kgWkIB%WE2n+TEv0-(cK7@3+`fUY{7o03=} z#K6TVA-Vp3LL(!ylhI~Yp#bLdHXj{sFw8g~zxaf}f2ZWSf{XlKd;gp`%k@Mu+;v_K zV`=TGn%D2HOh0~OpTG6}%8M5NlPq~nJ3s1}FVl{CCS?#`G;!rOE2-~yx|K|F7N7Cc z(b{xqXNy8}hjhHzPDfrY_XQcV4=<9I^i%wITWp%@(I2}+>Px2o4AMHaB~WICxYm5} z3oifizA_kSKl!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A( literal 0 HcmV?d00001 diff --git a/src/features/iterator-helpers.md b/src/features/iterator-helpers.md new file mode 100644 index 000000000..5227bc26f --- /dev/null +++ b/src/features/iterator-helpers.md @@ -0,0 +1,189 @@ +--- +title: 'Iterator helpers' +author: 'Rezvan Mahdavi Hezaveh' +avatars: + - 'rezvan-mahdavi-hezaveh' +date: 2024-03-27 +tags: + - ECMAScript +description: 'Interfaces that help with general usage and consumption of iterators.' +tweet: '' +--- + +*Iterator helpers* are a collection of new methods on Iterator prototype that help in general use of iterators. Since these helper methods are on the iterator prototype, any object that has `Iterator.prototype` on its prototype chain (e.g. array iterators) will get the methods. In the following subsections, we explain iterator helpers. All the provided examples are working in a blog archive page that includes list of blog posts, illustrating how iterator helpers are helpful for finding and manupulating posts. You can try them on [V8 blog page](https://v8.dev/blog)! + + +## .map(mapperFn) + +`map` takes a mapper function as an argument. This helper returns an iterator of values with the mapper function applied to the original iterator values. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Get the list of posts, return a list of their text content (titles) and log them. +for (const post of posts.values().map((x) => x.textContent)) { + console.log(post); +} +``` + +## .filter(filtererFn) + +`filter` takes a filter function as an argument. This helper returns an iterator of values from the original iterator for which the filter function returned a truthy value. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Filter blog posts that includes `V8` in their text content (titles) and log them. +for (const post of posts.values().filter((x) => x.textContent.includes('V8'))) { + console.log(post); +} +``` + +## .take(limit) + +`take` takes a an integer as an argument. This helper returns an iterator of values from the original iterator, up to `limit` values. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Select 10 recent blog posts and log them. +for (const post of posts.values().take(10)) { + console.log(post); +} +``` + +## .drop(limit) + +`drop` takes a an integer as an argument. This helper returns an iterator of values from the original iterator, starting with the value after the `limit` values. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Drop 10 recent blog posts and log the rest of them. +for (const post of posts.values().drop(10)) { + console.log(post); +} +``` + +## .flatMap(mapperFn) + +`flatMap` takes a mapper function as an argument. This helper returns an iterator of the values of the iterators produced by applying the mapper function to the original iterator values. That is, the iterators returned by the mapper function are flattened into the iterator returned by this helper. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Get list of tags of the blog posts and log them. Each post can have more than one tag. +for (const tag of posts.values().flatMap((x) => x.querySelectorAll('.tag').values())) { + console.log(tag.textContent); +} +``` + +## .reduce(reducer [, initialValue ]) + +`reduce` takes a reducer function and an optional initial value. This helper returns one value as a result of applying the reducer function to every value of the iterator while keeping track of the last result of applying the reducer. The initial value is used as the starting point for the reducer function when it processes the first value of the iterator. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Get list of tags for all posts. +const tagLists = posts.values().flatMap((x) => x.querySelectorAll('.tag').values()); + +// Get text context for each tag in the list. +const tags = tagLists.map((x) => x.textContent); + +// Counts posts with security tag. +const count = tags.reduce((sum , value) => sum + (value === 'security' ? 1 : 0), 0); +console.log(count); +``` + +## .toArray() + +`toArray` returns an array form iterator values. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Create an array from the list of 10 recent blog posts. +const arr = posts.values().take(10).toArray(); +``` + +## .forEach(fn) + +`forEach` takes a function as an argument and is applied on each element of the iterator. This helper is called for its side effect and returns `undefined`. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Get the dates that at least one blog post is published and log them. +const dates = new Set(); +const forEach = posts.values().forEach((x) => dates.add(x.querySelector('time'))); +console.log(dates); +``` + +## .some(fn) + +`some` takes a predicate function as an argument. This helper returns `true` if any iterator element returns true when the function is applied to it. The iterator is consumed after `some` is called. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Find out if text content (title) of any blog post includes the `Iteartors` keyword. +posts.values().some((x) => x.textContent.includes('Iterators')); +``` + +## .every(fn) + +`every` takes a predicate function as an argument. This helper returns `true` if every iterator element returns true when the function is applied to it. The iterator is consumed after `every` is called. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Find out if text content (title) of all blog post includes the `V8` keyword. +posts.values().every((x) => x.textContent.includes('V8')); +``` + +## .find(fn) + +`find` takes a predicate function as an argument. This helper returns the first value of the iterator for which the function returns a truthy value, or `undefined` if no value of the iterator does. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// Log the text content (title) of the recent blog post includes `V8` keyword. +console.log(posts.values().find((x) => x.textContent.includes('V8')).textContent); +``` + +## Iterator.from(object) + +`from` is a static method and takes an object as an argument. If the `object` is already an instance of Iterator, the helper returns it directly. If the `object` has `Symbol.iterator`, which means it is an iterable, its `Symbol.iterator` method is called to get the iterator and the helper returns it. Otherwise, a new `Iterator` object (that inherit from `Iterator.prototype` and has `next()` and `return()` methods) is created that wraps the `object` and is returned by this helper. + +```javascript +// Select the list of blog posts from a blog archive page. +const posts = document.querySelectorAll('li:not(header li)'); + +// First create an iterator from the posts. Then, log the text content (title) of the recent blog post that includes the `V8` keyword. +console.log(Iterator.from(posts).find((x) => x.textContent.includes('V8')).textContent); +``` + +## Availability + +Iterator helpers are shipped in V8 v12.2. + +## Import attribute support + + From 1e5660cfa9f2cc199751b045ead0e322b0314127 Mon Sep 17 00:00:00 2001 From: Rezvan Mahdavi Hezaveh <31081679+rmahdav@users.noreply.github.com> Date: Wed, 27 Mar 2024 15:31:16 -0700 Subject: [PATCH 2/5] Update examples comments (#749) --- src/features/iterator-helpers.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/features/iterator-helpers.md b/src/features/iterator-helpers.md index 5227bc26f..a4d508913 100644 --- a/src/features/iterator-helpers.md +++ b/src/features/iterator-helpers.md @@ -77,7 +77,8 @@ for (const post of posts.values().drop(10)) { // Select the list of blog posts from a blog archive page. const posts = document.querySelectorAll('li:not(header li)'); -// Get list of tags of the blog posts and log them. Each post can have more than one tag. +// Get list of tags of the blog posts and log them. Each post can have more than +// one tag. for (const tag of posts.values().flatMap((x) => x.querySelectorAll('.tag').values())) { console.log(tag.textContent); } @@ -136,7 +137,8 @@ console.log(dates); // Select the list of blog posts from a blog archive page. const posts = document.querySelectorAll('li:not(header li)'); -// Find out if text content (title) of any blog post includes the `Iteartors` keyword. +// Find out if text content (title) of any blog post includes the `Iteartors` +// keyword. posts.values().some((x) => x.textContent.includes('Iterators')); ``` @@ -172,7 +174,8 @@ console.log(posts.values().find((x) => x.textContent.includes('V8')).textContent // Select the list of blog posts from a blog archive page. const posts = document.querySelectorAll('li:not(header li)'); -// First create an iterator from the posts. Then, log the text content (title) of the recent blog post that includes the `V8` keyword. +// First create an iterator from the posts. Then, log the text content (title) of +// the recent blog post that includes the `V8` keyword. console.log(Iterator.from(posts).find((x) => x.textContent.includes('V8')).textContent); ``` From cf0bc7ffce4f7baa6d8e3c5d7ebba75dad1b104b Mon Sep 17 00:00:00 2001 From: Rezvan Mahdavi Hezaveh <31081679+rmahdav@users.noreply.github.com> Date: Thu, 28 Mar 2024 17:02:38 -0700 Subject: [PATCH 3/5] fix typo (#752) --- src/features/iterator-helpers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/iterator-helpers.md b/src/features/iterator-helpers.md index a4d508913..f06f24929 100644 --- a/src/features/iterator-helpers.md +++ b/src/features/iterator-helpers.md @@ -137,7 +137,7 @@ console.log(dates); // Select the list of blog posts from a blog archive page. const posts = document.querySelectorAll('li:not(header li)'); -// Find out if text content (title) of any blog post includes the `Iteartors` +// Find out if text content (title) of any blog post includes the `Iterators` // keyword. posts.values().some((x) => x.textContent.includes('Iterators')); ``` From 98d98992a76bed54fa2dfb6819e6c8ca6c76d8c7 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sat, 30 Mar 2024 04:10:14 +0700 Subject: [PATCH 4/5] Actualize `core-js` data for `Iterator` helpers (#751) --- src/features/iterator-helpers.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/features/iterator-helpers.md b/src/features/iterator-helpers.md index f06f24929..fc98a4b94 100644 --- a/src/features/iterator-helpers.md +++ b/src/features/iterator-helpers.md @@ -186,7 +186,7 @@ Iterator helpers are shipped in V8 v12.2. ## Import attribute support + firefox="no https://bugzilla.mozilla.org/show_bug.cgi?id=1568906" + safari="no https://bugs.webkit.org/show_bug.cgi?id=248650" + nodejs="no" + babel="yes https://github.com/zloirock/core-js#iterator-helpers"> From eaf223a610a52a1b70c55a80b35ecce4d69ddec9 Mon Sep 17 00:00:00 2001 From: Ville Lahdenvuo Date: Tue, 2 Apr 2024 13:16:15 +0200 Subject: [PATCH 5/5] Fix incorrect heading in iterator-helpers.md (#750) --- src/features/iterator-helpers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/features/iterator-helpers.md b/src/features/iterator-helpers.md index fc98a4b94..69e1ce709 100644 --- a/src/features/iterator-helpers.md +++ b/src/features/iterator-helpers.md @@ -183,7 +183,7 @@ console.log(Iterator.from(posts).find((x) => x.textContent.includes('V8')).textC Iterator helpers are shipped in V8 v12.2. -## Import attribute support +## Iterator helpers support