Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit ecf9304

Browse files
gkalpakpetebacondarwin
authored andcommitted
fix(limitTo): start at 0 if begin is negative and exceeds input length
Previously, specifying a negative `begin` whose abs value exceeds the input's length, would behave unexpectedly (depending on the value of `limit` relative to `begin`). E.g.: ``` limitToFilter('12345', 3, -7) === '1' // but limitToFilter('12345', 10, -7) === '123' ``` This commit fixes the unexpected behaviour, by setting `begin` to 0 in the aforementioned cases. Thus, the previous examples become: ``` limitToFilter('12345', 3, -7) === limitToFilter('12345', 3, 0) === '123' // and limitToFilter('12345', 10, -7) === limitToFilter('12345', 10, 0) === '12345' ``` Fixes #12775 Closes #12781
1 parent b9ab887 commit ecf9304

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/ng/filter/limitTo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function limitToFilter() {
111111
if (!isArray(input) && !isString(input)) return input;
112112

113113
begin = (!begin || isNaN(begin)) ? 0 : toInt(begin);
114-
begin = (begin < 0 && begin >= -input.length) ? input.length + begin : begin;
114+
begin = (begin < 0) ? Math.max(0, input.length + begin) : begin;
115115

116116
if (limit >= 0) {
117117
return input.slice(begin, begin + limit);

test/ng/filter/limitToSpec.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,19 @@ describe('Filter: limitTo', function() {
140140

141141
it('should return an empty array if Y exceeds input length', function() {
142142
expect(limitTo(items, '3', 12)).toEqual([]);
143-
expect(limitTo(items, 4, '-12')).toEqual([]);
144143
expect(limitTo(items, -3, '12')).toEqual([]);
145-
expect(limitTo(items, '-4', -12)).toEqual([]);
146144
});
147145

148146
it('should return an empty string if Y exceeds input length', function() {
149147
expect(limitTo(str, '3', 12)).toEqual("");
150-
expect(limitTo(str, 4, '-12')).toEqual("");
151148
expect(limitTo(str, -3, '12')).toEqual("");
152-
expect(limitTo(str, '-4', -12)).toEqual("");
149+
});
150+
151+
it('should start at 0 if Y is negative and exceeds input length', function() {
152+
expect(limitTo(items, 4, '-12')).toEqual(['a', 'b', 'c', 'd']);
153+
expect(limitTo(items, '-4', -12)).toEqual(['e', 'f', 'g', 'h']);
154+
expect(limitTo(str, 4, '-12')).toEqual("tuvw");
155+
expect(limitTo(str, '-4', -12)).toEqual("wxyz");
153156
});
154157

155158
it('should return the entire string beginning from Y if X is positive and X+Y exceeds input length', function() {

0 commit comments

Comments
 (0)