unicorn/prefer-math-min-max
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-min-max": "error",
},
},
];Prefer Math.min() and Math.max() over ternaries for simple comparisons.
height > 50 ? 50 : height;height >= 50 ? 50 : height;height < 50 ? height : 50;Math.min(height, 50);Math.max(height, 50);Examples from the ESLint documentation.
unicorn/prefer-math-min-max
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-min-max": "error",
},
},
];unicorn/prefer-math-min-max
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-math-min-max": "error"
}
}complexity/useMathMinMax
complexity · since 2.4.14
{
"linter": {
"rules": {
"complexity": {
"useMathMinMax": "error"
}
}
}
}Prefer Date.now() to get the number of milliseconds since the Unix Epoch.
Prefer Math.trunc() for truncating numbers.
Prefer BigInt literals over the constructor.
Prefer modern Math APIs over legacy patterns.
Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item.
Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object.