unicorn/text-encoding-identifier-case

Enforce one spelling for the encoding names utf8 and ascii

fixable

"UTF-8", "utf-8", "Utf8" and "utf8" all name the same encoding in most APIs. With one spelling, a search finds every use. The rule wants "utf8" and "ascii".

In three places it wants "utf-8", the name the web standards use: new TextDecoder("utf-8"), <meta charset="utf-8" /> and <form acceptCharset="utf-8">.

The rule reports any string that's exactly one of these names, wherever it appears, and JSX text such as <span>UTF-8</span>. Template literals aren't checked.

--fix rewrites the string.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/text-encoding-identifier-case" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/text-encoding-identifier-case" = ["error", {
  withDash = false,
}]

Examples#

example.js3 problems
const text = await fs.readFile(file, "UTF-8");
const bytes = Buffer.from(name, "ASCII");
const decoder = new TextDecoder("utf8");
example.jsx2 problems
const head = <meta charset="utf8" />;
const label = <span>UTF-8</span>;
example.jsNo problems
const text = await fs.readFile(file, "utf8");
const bytes = Buffer.from(name, "ascii");
const decoder = new TextDecoder("utf-8");
example.jsxNo problems
const head = <meta charset="utf-8" />;
const label = <span>{`UTF-8`}</span>;

Options#

OptionDefaultWhat it does
withDashfalseRequire "utf-8" everywhere instead of "utf8".
example.jswith { "withDash": true }1 problem
const text = await fs.readFile(file, "utf8");
example.jswith { "withDash": true }No problems
const text = await fs.readFile(file, "utf-8");