unicorn/numeric-separators-style

Enforce how digits are grouped with _ in numbers

fixable

1000000 and 10000000 are hard to tell apart. 1_000_000 is easy to read, but only if the groups are all the same size. 10_00_000 is worse than no separators.

By default, decimal numbers of five digits or more are grouped by three, hexadecimal numbers by two, and binary and octal numbers by four. Groups are counted from the right.

--fix rewrites the number.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/numeric-separators-style" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/numeric-separators-style" = ["error", {
  onlyIfContainsSeparator = false,
  number = { minimumDigits = 5, groupLength = 3 },
  hexadecimal = { minimumDigits = 0, groupLength = 2 },
  binary = { minimumDigits = 0, groupLength = 4 },
  octal = { minimumDigits = 0, groupLength = 4 },
}]

Examples#

example.js4 problems
const population = 1000000;
const price = 12_34_567;
const color = 0xFF8800;
const flags = 0b10101010;
example.jsNo problems
const population = 1_000_000;
const price = 1_234_567;
const color = 0xFF_88_00;
const flags = 0b1010_1010;
const year = 2026;

Options#

OptionDefaultWhat it does
onlyIfContainsSeparatorfalseOnly check numbers that already have a _.
number{ "minimumDigits": 5, "groupLength": 3 }Settings for decimal numbers.
hexadecimal{ "minimumDigits": 0, "groupLength": 2 }Settings for 0x numbers.
binary{ "minimumDigits": 0, "groupLength": 4 }Settings for 0b numbers.
octal{ "minimumDigits": 0, "groupLength": 4 }Settings for 0o numbers.

Each of the four kinds takes these settings:

SettingWhat it does
groupLengthHow many digits are in a group.
minimumDigitsNumbers with fewer digits have no separators.
onlyIfContainsSeparatorOverrides the top-level option for this kind of number.
fractionGroupLengthOnly for number. How many digits are in a group after the decimal point, counted from the left. By default the fraction has no separators.

With onlyIfContainsSeparator, a number without separators is left alone, and one with separators must have them in the right places:

example.jswith { "onlyIfContainsSeparator": true }No problems
const population = 1000000;
example.jswith { "onlyIfContainsSeparator": true }1 problem
const population = 10_00000;

To leave hexadecimal numbers such as colors alone unless they have a separator:

example.jswith { "hexadecimal": { "onlyIfContainsSeparator": true } }No problems
const color = 0xFF8800;

With minimumDigits, shorter numbers must not have separators:

example.jswith { "number": { "minimumDigits": 7 } }No problems
const timeout = 30000;
const population = 1_000_000;
example.jswith { "number": { "minimumDigits": 7 } }1 problem
const timeout = 30_000;

With fractionGroupLength:

example.jswith { "number": { "fractionGroupLength": 3 } }1 problem
const pi = 3.14159265;
example.jswith { "number": { "fractionGroupLength": 3 } }No problems
const pi = 3.141_592_65;