useFlatMap
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/complexity/useFlatMap¥Diagnostic Category:
lint/complexity/useFlatMap -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/prefer-array-flat-map相同¥Same as
unicorn/prefer-array-flat-map -
与
map_flatten相同¥Same as
map_flatten
-
¥How to configure
{ "linter": { "rules": { "complexity": { "useFlatMap": "error" } } }}¥Description
提倡在 map().flat() 一起使用时使用 .flatMap()。
¥Promotes the use of .flatMap() when map().flat() are used together.
¥Examples
¥Invalid
const array = ["split", "the text", "into words"];array.map(sentence => sentence.split(' ')).flat();code-block.js:2:1 lint/complexity/useFlatMap FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The call chain .map().flat() can be replaced with a single .flatMap() call.
1 │ const array = [“split”, “the text”, “into words”];
> 2 │ array.map(sentence => sentence.split(’ ‘)).flat();
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ Safe fix: Replace the chain with .flatMap().
1 1 │ const array = [“split”, “the text”, “into words”];
2 │ - array.map(sentence·=>·sentence.split(’·‘)).flat();
2 │ + array.flatMap(sentence·=>·sentence.split(’·’));
3 3 │
const array = ["split", "the text", "into words"];array.map(sentence => sentence.split(' ')).flat(1);code-block.js:2:1 lint/complexity/useFlatMap FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The call chain .map().flat() can be replaced with a single .flatMap() call.
1 │ const array = [“split”, “the text”, “into words”];
> 2 │ array.map(sentence => sentence.split(’ ‘)).flat(1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │
ℹ Safe fix: Replace the chain with .flatMap().
1 1 │ const array = [“split”, “the text”, “into words”];
2 │ - array.map(sentence·=>·sentence.split(’·‘)).flat(1);
2 │ + array.flatMap(sentence·=>·sentence.split(’·’));
3 3 │
¥Valid
const array = ["split", "the text", "into words"];array.map(sentence => sentence.split(' ')).flat(2);¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号