Skip to content

Latest commit

 

History

History
70 lines (45 loc) · 1.6 KB

text-encoder.md

File metadata and controls

70 lines (45 loc) · 1.6 KB

node/prefer-global/text-encoder

enforce either TextEncoder or require("util").TextEncoder

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.

{
    "node/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 node/prefer-global/text-encoder: [error]*/

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

Examples of 👍 correct code for this rule:

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

const u = new TextEncoder(s)

never

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

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

const u = new TextEncoder(s)

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

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

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

🔎 Implementation