Skip to content

Latest commit

 

History

History
71 lines (45 loc) · 1.62 KB

text-encoder.md

File metadata and controls

71 lines (45 loc) · 1.62 KB

Enforce either TextEncoder or require("util").TextEncoder (n/prefer-global/text-encoder)

The TextEncoder class of util module is defined as a global variable.

console.log(TextEncoder === require("util").TextEncoder) //→ true

It will be readable if we use either TextEncoder consistently.

📖 Rule Details

This rule enforces which TextEncoder we should use.

Options

This rule has a string option.

{
    "n/prefer-global/text-encoder": ["error", "always" | "never"]
}
  • "always" (default) ... enforces to use the global variable TextEncoder rather than require("util").TextEncoder.
  • "never" ... enforces to use require("util").TextEncoder rather than the global variable TextEncoder.

always

Examples of 👎 incorrect code for this rule:

/*eslint n/prefer-global/text-encoder: [error]*/

const { TextEncoder } = require("util")
const u = new TextEncoder(s)

Examples of 👍 correct code for this rule:

/*eslint n/prefer-global/text-encoder: [error]*/

const u = new TextEncoder(s)

never

Examples of 👎 incorrect code for the "never" option:

/*eslint n/prefer-global/text-encoder: [error, never]*/

const u = new TextEncoder(s)

Examples of 👍 correct code for the "never" option:

/*eslint n/prefer-global/text-encoder: [error, never]*/

const { TextEncoder } = require("util")
const u = new TextEncoder(s)

🔎 Implementation