Open gurgunday opened 1 month ago
string.split('/').at(numberVar)
Could also be:
string.split('/', numberVar).at(numberVar)
Yeah
Just a small correction in case anyone starts implementing the rule -- since the second parameter is the length of the returned array instead of the number of splits to do:
string.split('/').at(numberVar)
// would be:
string.split('/', numberVar + 1).at(numberVar)
The rule could potentially also warn for that:
s.split('/', 1).at(2);
// Error: This will always be undefined
Description
Whenever we use
String.prototype.split
with literals like so:We benefit by limiting the search space, this allows split to exit early once it attains the number of splits and to not scan the whole string:
Fail
Pass
Proposed rule name
specify-split-limit or prefer-split-limit
Additional Info
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split#limit