unicorn/prefer-math-trunc
eslint-plugin-unicorn
Suggestion
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-trunc": "error",
},
},
];console.log(foo | 0);console.log(~~foo);console.log(foo << 0);console.log(Math.trunc(foo));foo = Math.trunc(foo);const foo = 37.4;
console.log(foo | 3);Examples from the ESLint documentation.
unicorn/prefer-math-trunc
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-math-trunc": "error",
},
},
];unicorn/prefer-math-trunc
unicorn plugin · pedantic
{
"rules": {
"unicorn/prefer-math-trunc": "error"
}
}nursery/useMathTrunc
nursery · since 2.6.3
{
"linter": {
"rules": {
"nursery": {
"useMathTrunc": "error"
}
}
}
}Prefer Math.min() and Math.max() over ternaries for simple comparisons.
Prefer modern Math APIs over legacy patterns.
Prefer Date.now() to get the number of milliseconds since the Unix Epoch.
Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object.
Prefer BigInt literals over the constructor.
Disallow bitwise operators where a logical operator was likely intended.