sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-string-pad-start-end`

Closed

#1,773 opened on Mar 30, 2022

 (1 comment) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

Description

More clear intention, prevent mistake like this

bar = '12345';
'0'.repeat(4 - bar.length) + bar;
VM433:2 Uncaught RangeError: Invalid count value
    at String.repeat (<anonymous>)

bar = '12345';
bar.padStart(4, '0');
'12345'

Fail

const foo = ' '.repeat(10 - bar.length) + bar;
const foo = '*'.repeat(10 - bar.length) + bar;
const foo = bar + ' '.repeat(10 - bar.length);
const foo = bar + '*'.repeat(10 - bar.length);
const foo = ('*'.repeat(10) + bar).slice(-10);
const foo = (bar + '*'.repeat(10)).slice(0, 10);

Pass

const foo = bar.padStart(10)
const foo = bar.padStart(10, '*')
const foo = bar.padEnd(10)
const foo = bar.padEnd(10, '*')

Contributor guide