sindresorhus/eslint-plugin-unicorn

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

Chiusa

#1773 aperta il 30 mar 2022

 (1 commento) (1 reazione) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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, '*')

Guida contributor