Frontend Weekly 2024-10-18
New ECMAScript Stage 4 Proposals
2024 年 10 月 12 日の TC39 ミーティングで、Promise.try
、Import Attributes
、RegExp Modifiers
、Sync Iterator helpers
、JSON Modules
が Stage 4 になった。
https://ecmascript-daily.github.io/ecmascript/2024/10/12/ecmascript-proposal-update
Sync Iterator helpers
これによりイテレータを操作するためのヘルパー関数が多く追加される。
https://github.com/tc39/proposal-iterator-helpers
次のように Array 相当の記述が可能になる。
Iterators | Array |
---|---|
map(fn) | map(fn) |
filter(fn) | filter(fn) |
take(n) | slice(0, n) |
drop(n) | slice(n) |
flatMap(fn) | flatMap(fn) |
reduce(fn, init) | reduce(fn, init) |
toArray() | |
forEach(fn) | forEach(fn) |
some(fn) | some(fn) |
every(fn) | every(fn) |
find(fn) | find(fn) |
from(iter) |
Promise.try
Promise.try は、渡された関数を実行し、その結果を Promise でラップして返す。
Primise.try(func());
Promise.try を利用することで任意の関数を Promise のセマンティクスで扱うことができる。 以下のコードに似た動作をする。
new Promise((resolve, reject) => { try { resolve(func()); } catch (error) { reject(error); }});
Import Attributes 及び JSON Modules
Import Attributes は JSON や CSS、WASM など追加のモジュールを読み込めるようにすることを目的にしている。
import json from "./foo.json" with { type: "json" };
合わせて、JSON モジュールのプロポーザルも Stage 4 になった。
https://github.com/tc39/proposal-json-modules
既に JSON の読み込みについては、Firefox を除くモダンブラウザだけでなく Node.js や Deno、Bun などでも利用可能。
RegExp Modifiers
RegExp Modifiers を利用すると、次のように正規表現のフラグを一部変更することが出来る。
const re1 = /^[a-z](?-i:[a-z])$/i;re1.test("ab"); // truere1.test("Ab"); // truere1.test("aB"); // false
const re2 = /^(?i:[a-z])[a-z]$/;re2.test("ab"); // truere2.test("Ab"); // truere2.test("aB"); // false
CSS nesting improves with CSSNestedDeclarations
CSS Nesting における問題を解決するために、Chrome 130 から導入される CSSNestedDeclarations について解説する記事。
https://web.dev/blog/css-nesting-cssnesteddeclarations?hl=en
Chrome 130 から宣言のホイスティングが行われなくなるため、宣言がネストしたルールセットの下にあるような記述をしていた場合、壊れる可能性がある。
/* This does not work in Chrome 130 */#mypopover:popover-open { @starting-style { opacity: 0; scale: 0.5; }
opacity: 1; scale: 1;}
.io ドメイン消滅の可能性について
イギリスの領土返還により、.io
ドメインが消滅するかもしれないという話が出ている。
記事では、ユーゴスラビアに割り当てられていた国別コードトップレベルドメイン(ccTLD)である.yu
や、
ソビエト連邦の ccTLD であった.su
を例に挙げ、.io
ドメインが消滅する可能性について考察している。