sindresorhus/eslint-plugin-unicorn
Rule proposal: Prefer explicit array check over `Array#flat()`.
Chiusa
#1719 aperta il 8 feb 2022
help wantednew rule
Metriche repository
- Star
- (5022 stelle)
- Metriche merge PR
- (Merge medio 4h 30m) (26 PR mergiate in 30 g)
Descrizione
Description
When a function accepts a single or multiple things. Though [foo].flat() is shorter and nicer, but it's not very clear to me.
I prefer Array.isArray(foo) ? foo : [foo]. If the array need to be cloned Array.isArray(foo) ? [...foo] : [foo].
But if the variable name is something like nameOrNames, [nameOrNames].flat() looks clear to me, maybe we can add an exception.
Fail
items = [items].flat();
Pass
items = Array.isArray(items) ? items : [items];
items = Array.isArray(items) ? [...items] : [items];
items = [...(Array.isArray(items) ? items : [items])];
const names = [nameOrNames].flat();