Skip to content

Latest commit

 

History

History
23 lines (17 loc) · 1 KB

prefer-negative-index.md

File metadata and controls

23 lines (17 loc) · 1 KB

Prefer negative index over .length - index for {String,Array,TypedArray}#slice() and Array#splice()

Prefer negative index over calculating from .length for String#slice(), Array#slice, TypedArray#slice and Array#splice()

This rule is fixable.

Fail

foo.slice(foo.length - 2, foo.length - 1);
foo.splice(foo.length - 1, 1);
Array.prototype.slice.call(foo, foo.length - 2, foo.length - 1);
Array.prototype.slice.apply(foo, [foo.length - 2, foo.length - 1]);

Pass

foo.slice(-2, -1);
foo.splice(-1, 1);
Array.prototype.slice.call(foo, -2, -1);
Array.prototype.slice.apply(foo, [-2, -1]);