メインコンテンツへスキップ

All Posts

News bits

New ECMAScript Stage 4 Proposals

2024 年 10 月 12 日の TC39 ミーティングで、Promise.tryImport AttributesRegExp ModifiersSync Iterator helpersJSON 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 相当の記述が可能になる。

IteratorsArray
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 でラップして返す。

js
Primise.try(func());

Promise.try を利用することで任意の関数を Promise のセマンティクスで扱うことができる。 以下のコードに似た動作をする。

js
new Promise((resolve, reject) => {
  try {
    resolve(func());
  } catch (error) {
    reject(error);
  }
});

#Import Attributes 及び JSON Modules

Import Attributes は JSON や CSS、WASM など追加のモジュールを読み込めるようにすることを目的にしている。

js
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 を利用すると、次のように正規表現のフラグを一部変更することが出来る。

js
const re1 = /^[a-z](?-i:[a-z])$/i;
re1.test("ab"); // true
re1.test("Ab"); // true
re1.test("aB"); // false

const re2 = /^(?i:[a-z])[a-z]$/;
re2.test("ab"); // true
re2.test("Ab"); // true
re2.test("aB"); // false

https://github.com/tc39/proposal-regexp-modifiers

著者について

Hi there. I'm hrdtbs, a frontend expert and technical consultant. I started my career in the creative industry over 13 years ago, learning on the job as a 3DCG modeler and game engineer in the indie scene.

In 2015 I began working as a freelance web designer and engineer. I handled everything from design and development to operation and advertising, delivering comprehensive solutions for various clients.

In 2016 I joined Wemotion as CTO, where I built the engineering team from the ground up and led the development of core web and mobile applications for three years.

In 2019 I joined matsuri technologies as a Frontend Expert, and in 2020 I also began serving as a technical manager supporting streamers and content creators.

I'm so grateful to be working in this field, doing something that brings me so much joy. Thanks for stopping by.