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

Commit aaae3cc

Browse files
tameraydinpkozlowski-opensource
authored andcommitted
feat(limitTo): extend the filter to take a beginning index argument
Extend the limitTo filter to take an optional argument for beginning index. It provides a slice-alike functionality to manipulate the input. Closes #5355 Closes #10899
1 parent 49b54b0 commit aaae3cc

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/ng/filter/limitTo.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* If the number is negative, `limit` number of items from the end of the source array/string
1818
* are copied. The `limit` will be trimmed if it exceeds `array.length`. If `limit` is undefined,
1919
* the input will be returned unchanged.
20+
* @param {string|number} begin Index at which to begin limitation. As a negative index, `begin`
21+
* indicates an offset from the end of `input`. Defaults to `0`.
2022
* @returns {Array|string} A new sub-array or substring of length `limit` or less if input array
2123
* had less than `limit` elements.
2224
*
@@ -88,7 +90,7 @@
8890
</example>
8991
*/
9092
function limitToFilter() {
91-
return function(input, limit) {
93+
return function(input, limit, begin) {
9294
if (Math.abs(Number(limit)) === Infinity) {
9395
limit = Number(limit);
9496
} else {
@@ -99,6 +101,17 @@ function limitToFilter() {
99101
if (isNumber(input)) input = input.toString();
100102
if (!isArray(input) && !isString(input)) return input;
101103

102-
return limit >= 0 ? input.slice(0, limit) : input.slice(limit);
104+
begin = (!begin || isNaN(begin)) ? 0 : toInt(begin);
105+
begin = (begin < 0 && begin >= -input.length) ? input.length + begin : begin;
106+
107+
if (limit >= 0) {
108+
return input.slice(begin, begin + limit);
109+
} else {
110+
if (begin === 0) {
111+
return input.slice(limit, input.length);
112+
} else {
113+
return input.slice(Math.max(0, begin + limit), begin);
114+
}
115+
}
103116
};
104117
}

test/ng/filter/limitToSpec.js

+66
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ describe('Filter: limitTo', function() {
2323
expect(limitTo(number, '3')).toEqual("100");
2424
});
2525

26+
it('should return the first X items beginning from index Y when X and Y are positive', function() {
27+
expect(limitTo(items, 3, '3')).toEqual(['d', 'e', 'f']);
28+
expect(limitTo(items, '3', 3)).toEqual(['d', 'e', 'f']);
29+
expect(limitTo(str, 3, 3)).toEqual("wxy");
30+
expect(limitTo(str, '3', '3')).toEqual("wxy");
31+
});
32+
33+
it('should return the first X items beginning from index Y when X is positive and Y is negative', function() {
34+
expect(limitTo(items, 3, '-3')).toEqual(['f', 'g', 'h']);
35+
expect(limitTo(items, '3', -3)).toEqual(['f', 'g', 'h']);
36+
expect(limitTo(str, 3, -3)).toEqual("xyz");
37+
expect(limitTo(str, '3', '-3')).toEqual("xyz");
38+
});
2639

2740
it('should return the last X items when X is negative', function() {
2841
expect(limitTo(items, -3)).toEqual(['f', 'g', 'h']);
@@ -33,6 +46,19 @@ describe('Filter: limitTo', function() {
3346
expect(limitTo(number, '-3')).toEqual("045");
3447
});
3548

49+
it('should return the last X items until index Y when X and Y are negative', function() {
50+
expect(limitTo(items, -3, '-3')).toEqual(['c', 'd', 'e']);
51+
expect(limitTo(items, '-3', -3)).toEqual(['c', 'd', 'e']);
52+
expect(limitTo(str, -3, -3)).toEqual("uvw");
53+
expect(limitTo(str, '-3', '-3')).toEqual("uvw");
54+
});
55+
56+
it('should return the last X items until index Y when X is negative and Y is positive', function() {
57+
expect(limitTo(items, -3, '4')).toEqual(['b', 'c', 'd']);
58+
expect(limitTo(items, '-3', 4)).toEqual(['b', 'c', 'd']);
59+
expect(limitTo(str, -3, 4)).toEqual("uvw");
60+
expect(limitTo(str, '-3', '4')).toEqual("uvw");
61+
});
3662

3763
it('should return an empty array when X = 0', function() {
3864
expect(limitTo(items, 0)).toEqual([]);
@@ -60,6 +86,18 @@ describe('Filter: limitTo', function() {
6086
expect(limitTo(str, undefined)).toEqual(str);
6187
});
6288

89+
it('should take 0 as beginning index value when Y cannot be parsed', function() {
90+
expect(limitTo(items, 3, 'bogus')).toEqual(limitTo(items, 3, 0));
91+
expect(limitTo(items, -3, 'null')).toEqual(limitTo(items, -3));
92+
expect(limitTo(items, '3', 'undefined')).toEqual(limitTo(items, '3', 0));
93+
expect(limitTo(items, '-3', null)).toEqual(limitTo(items, '-3'));
94+
expect(limitTo(items, 3, undefined)).toEqual(limitTo(items, 3, 0));
95+
expect(limitTo(str, 3, 'bogus')).toEqual(limitTo(str, 3));
96+
expect(limitTo(str, -3, 'null')).toEqual(limitTo(str, -3, 0));
97+
expect(limitTo(str, '3', 'undefined')).toEqual(limitTo(str, '3'));
98+
expect(limitTo(str, '-3', null)).toEqual(limitTo(str, '-3', 0));
99+
expect(limitTo(str, 3, undefined)).toEqual(limitTo(str, 3));
100+
});
63101

64102
it('should return input if not String or Array or Number', function() {
65103
expect(limitTo(null, 1)).toEqual(null);
@@ -99,4 +137,32 @@ describe('Filter: limitTo', function() {
99137
expect(limitTo(str, -Infinity)).toEqual(str);
100138
expect(limitTo(str, '-Infinity')).toEqual(str);
101139
});
140+
141+
it('should return an empty array if Y exceeds input length', function() {
142+
expect(limitTo(items, '3', 12)).toEqual([]);
143+
expect(limitTo(items, 4, '-12')).toEqual([]);
144+
expect(limitTo(items, -3, '12')).toEqual([]);
145+
expect(limitTo(items, '-4', -12)).toEqual([]);
146+
});
147+
148+
it('should return an empty string if Y exceeds input length', function() {
149+
expect(limitTo(str, '3', 12)).toEqual("");
150+
expect(limitTo(str, 4, '-12')).toEqual("");
151+
expect(limitTo(str, -3, '12')).toEqual("");
152+
expect(limitTo(str, '-4', -12)).toEqual("");
153+
});
154+
155+
it('should return the entire string beginning from Y if X is positive and X+Y exceeds input length', function() {
156+
expect(limitTo(items, 7, 3)).toEqual(['d', 'e', 'f', 'g', 'h']);
157+
expect(limitTo(items, 7, -3)).toEqual(['f', 'g', 'h']);
158+
expect(limitTo(str, 6, 3)).toEqual("wxyz");
159+
expect(limitTo(str, 6, -3)).toEqual("xyz");
160+
});
161+
162+
it('should return the entire string until index Y if X is negative and X+Y exceeds input length', function() {
163+
expect(limitTo(items, -7, 3)).toEqual(['a', 'b', 'c']);
164+
expect(limitTo(items, -7, -3)).toEqual(['a', 'b', 'c', 'd', 'e']);
165+
expect(limitTo(str, -6, 3)).toEqual("tuv");
166+
expect(limitTo(str, -6, -3)).toEqual("tuvw");
167+
});
102168
});

0 commit comments

Comments
 (0)