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

Commit 12f0a49

Browse files
committed
feat(limitTo): add support for array-likes
- Add support for array-like objects
1 parent cfc8b41 commit 12f0a49

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

src/ng/filter/limitTo.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,25 @@ function limitToFilter() {
108108
if (isNaN(limit)) return input;
109109

110110
if (isNumber(input)) input = input.toString();
111-
if (!isArray(input) && !isString(input)) return input;
111+
if (!isArrayLike(input)) return input;
112112

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

116116
if (limit >= 0) {
117-
return input.slice(begin, begin + limit);
117+
return sliceFn(input, begin, begin + limit);
118118
} else {
119119
if (begin === 0) {
120-
return input.slice(limit, input.length);
120+
return sliceFn(input, limit, input.length);
121121
} else {
122-
return input.slice(Math.max(0, begin + limit), begin);
122+
return sliceFn(input, Math.max(0, begin + limit), begin);
123123
}
124124
}
125125
};
126126
}
127+
128+
function sliceFn(input, idx, length) {
129+
if (isString(input)) return input.slice(idx, length);
130+
131+
return slice.call(input, idx, length);
132+
}

test/ng/filter/limitToSpec.js

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,33 @@ describe('Filter: limitTo', function() {
44
var items;
55
var str;
66
var number;
7+
var arrayLike;
78
var limitTo;
89

9-
beforeEach(inject(function($filter) {
10+
beforeEach(inject(function($document, $filter) {
1011
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
1112
str = "tuvwxyz";
1213
number = 100.045;
14+
arrayLike = {
15+
0: 'a',
16+
1: 'b',
17+
2: 'c',
18+
3: 'd',
19+
4: 'e',
20+
5: 'f',
21+
6: 'g',
22+
7: 'h',
23+
get length() {
24+
return Object.keys(this).length - 1;
25+
}
26+
};
1327
limitTo = $filter('limitTo');
1428
}));
1529

30+
afterEach(function() {
31+
32+
});
33+
1634

1735
it('should return the first X items when X is positive', function() {
1836
expect(limitTo(items, 3)).toEqual(['a', 'b', 'c']);
@@ -21,20 +39,26 @@ describe('Filter: limitTo', function() {
2139
expect(limitTo(str, '3')).toEqual("tuv");
2240
expect(limitTo(number, 3)).toEqual("100");
2341
expect(limitTo(number, '3')).toEqual("100");
42+
expect(limitTo(arrayLike, 3)).toEqual(['a', 'b', 'c']);
43+
expect(limitTo(arrayLike, '3')).toEqual(['a', 'b', 'c']);
2444
});
2545

2646
it('should return the first X items beginning from index Y when X and Y are positive', function() {
2747
expect(limitTo(items, 3, '3')).toEqual(['d', 'e', 'f']);
2848
expect(limitTo(items, '3', 3)).toEqual(['d', 'e', 'f']);
2949
expect(limitTo(str, 3, 3)).toEqual("wxy");
3050
expect(limitTo(str, '3', '3')).toEqual("wxy");
51+
expect(limitTo(arrayLike, 3, 3)).toEqual(['d', 'e', 'f']);
52+
expect(limitTo(arrayLike, '3', '3')).toEqual(['d', 'e', 'f']);
3153
});
3254

3355
it('should return the first X items beginning from index Y when X is positive and Y is negative', function() {
3456
expect(limitTo(items, 3, '-3')).toEqual(['f', 'g', 'h']);
3557
expect(limitTo(items, '3', -3)).toEqual(['f', 'g', 'h']);
3658
expect(limitTo(str, 3, -3)).toEqual("xyz");
3759
expect(limitTo(str, '3', '-3')).toEqual("xyz");
60+
expect(limitTo(arrayLike, 3, '-3')).toEqual(['f', 'g', 'h']);
61+
expect(limitTo(arrayLike, '3', -3)).toEqual(['f', 'g', 'h']);
3862
});
3963

4064
it('should return the last X items when X is negative', function() {
@@ -44,38 +68,52 @@ describe('Filter: limitTo', function() {
4468
expect(limitTo(str, '-3')).toEqual("xyz");
4569
expect(limitTo(number, -3)).toEqual("045");
4670
expect(limitTo(number, '-3')).toEqual("045");
71+
expect(limitTo(arrayLike, -3)).toEqual(['f', 'g', 'h']);
72+
expect(limitTo(arrayLike, '-3')).toEqual(['f', 'g', 'h']);
4773
});
4874

4975
it('should return the last X items until index Y when X and Y are negative', function() {
5076
expect(limitTo(items, -3, '-3')).toEqual(['c', 'd', 'e']);
5177
expect(limitTo(items, '-3', -3)).toEqual(['c', 'd', 'e']);
5278
expect(limitTo(str, -3, -3)).toEqual("uvw");
5379
expect(limitTo(str, '-3', '-3')).toEqual("uvw");
80+
expect(limitTo(arrayLike, -3, '-3')).toEqual(['c', 'd', 'e']);
81+
expect(limitTo(arrayLike, '-3', -3)).toEqual(['c', 'd', 'e']);
5482
});
5583

5684
it('should return the last X items until index Y when X is negative and Y is positive', function() {
5785
expect(limitTo(items, -3, '4')).toEqual(['b', 'c', 'd']);
5886
expect(limitTo(items, '-3', 4)).toEqual(['b', 'c', 'd']);
5987
expect(limitTo(str, -3, 4)).toEqual("uvw");
6088
expect(limitTo(str, '-3', '4')).toEqual("uvw");
89+
expect(limitTo(arrayLike, -3, '4')).toEqual(['b', 'c', 'd']);
90+
expect(limitTo(arrayLike, '-3', 4)).toEqual(['b', 'c', 'd']);
6191
});
6292

6393
it('should return an empty array when X = 0', function() {
6494
expect(limitTo(items, 0)).toEqual([]);
6595
expect(limitTo(items, '0')).toEqual([]);
96+
expect(limitTo(arrayLike, 0)).toEqual([]);
97+
expect(limitTo(arrayLike, '0')).toEqual([]);
6698
});
6799

68100
it('should return entire array when X cannot be parsed', function() {
69-
expect(limitTo(items, 'bogus')).toEqual(items);
70-
expect(limitTo(items, 'null')).toEqual(items);
71-
expect(limitTo(items, 'undefined')).toEqual(items);
72-
expect(limitTo(items, null)).toEqual(items);
73-
expect(limitTo(items, undefined)).toEqual(items);
101+
expect(limitTo(items, 'bogus')).toBe(items);
102+
expect(limitTo(items, 'null')).toBe(items);
103+
expect(limitTo(items, 'undefined')).toBe(items);
104+
expect(limitTo(items, null)).toBe(items);
105+
expect(limitTo(items, undefined)).toBe(items);
106+
expect(limitTo(arrayLike, 'bogus')).toBe(arrayLike);
107+
expect(limitTo(arrayLike, 'null')).toBe(arrayLike);
108+
expect(limitTo(arrayLike, 'undefined')).toBe(arrayLike);
109+
expect(limitTo(arrayLike, null)).toBe(arrayLike);
110+
expect(limitTo(arrayLike, undefined)).toBe(arrayLike);
74111
});
75112

76113
it('should return an empty string when X = 0', function() {
77114
expect(limitTo(str, 0)).toEqual("");
78115
expect(limitTo(str, '0')).toEqual("");
116+
expect(limitTo({}, 1)).toEqual({});
79117
});
80118

81119
it('should return entire string when X cannot be parsed', function() {
@@ -97,12 +135,16 @@ describe('Filter: limitTo', function() {
97135
expect(limitTo(str, '3', 'undefined')).toEqual(limitTo(str, '3'));
98136
expect(limitTo(str, '-3', null)).toEqual(limitTo(str, '-3', 0));
99137
expect(limitTo(str, 3, undefined)).toEqual(limitTo(str, 3));
138+
expect(limitTo(arrayLike, 3, 'bogus')).toEqual(limitTo(arrayLike, 3, 0));
139+
expect(limitTo(arrayLike, -3, 'null')).toEqual(limitTo(arrayLike, -3));
140+
expect(limitTo(arrayLike, '3', 'undefined')).toEqual(limitTo(arrayLike, '3', 0));
141+
expect(limitTo(arrayLike, '-3', null)).toEqual(limitTo(arrayLike, '-3'));
142+
expect(limitTo(arrayLike, 3, undefined)).toEqual(limitTo(arrayLike, 3, 0));
100143
});
101144

102-
it('should return input if not String or Array or Number', function() {
145+
it('should return input if not array-like or Number', function() {
103146
expect(limitTo(null, 1)).toEqual(null);
104147
expect(limitTo(undefined, 1)).toEqual(undefined);
105-
expect(limitTo({}, 1)).toEqual({});
106148
});
107149

108150

@@ -111,8 +153,13 @@ describe('Filter: limitTo', function() {
111153
expect(limitTo(items, '9')).toEqual(items);
112154
expect(limitTo(items, -9)).toEqual(items);
113155
expect(limitTo(items, '-9')).toEqual(items);
156+
expect(limitTo(arrayLike, 9)).toEqual(items);
157+
expect(limitTo(arrayLike, '9')).toEqual(items);
158+
expect(limitTo(arrayLike, -9)).toEqual(items);
159+
expect(limitTo(arrayLike, '-9')).toEqual(items);
114160

115161
expect(limitTo(items, 9)).not.toBe(items);
162+
expect(limitTo(arrayLike, 9)).not.toBe(items);
116163
});
117164

118165
it('should return the entire string if X exceeds input length', function() {
@@ -129,6 +176,10 @@ describe('Filter: limitTo', function() {
129176
expect(limitTo(items, 'Infinity')).toEqual(items);
130177
expect(limitTo(items, -Infinity)).toEqual(items);
131178
expect(limitTo(items, '-Infinity')).toEqual(items);
179+
expect(limitTo(arrayLike, Infinity)).toEqual(items);
180+
expect(limitTo(arrayLike, 'Infinity')).toEqual(items);
181+
expect(limitTo(arrayLike, -Infinity)).toEqual(items);
182+
expect(limitTo(arrayLike, '-Infinity')).toEqual(items);
132183
});
133184

134185
it('should return the entire string when limited by Infinity', function() {
@@ -141,6 +192,8 @@ describe('Filter: limitTo', function() {
141192
it('should return an empty array if Y exceeds input length', function() {
142193
expect(limitTo(items, '3', 12)).toEqual([]);
143194
expect(limitTo(items, -3, '12')).toEqual([]);
195+
expect(limitTo(arrayLike, '3', 12)).toEqual([]);
196+
expect(limitTo(arrayLike, -3, '12')).toEqual([]);
144197
});
145198

146199
it('should return an empty string if Y exceeds input length', function() {
@@ -153,19 +206,25 @@ describe('Filter: limitTo', function() {
153206
expect(limitTo(items, '-4', -12)).toEqual(['e', 'f', 'g', 'h']);
154207
expect(limitTo(str, 4, '-12')).toEqual("tuvw");
155208
expect(limitTo(str, '-4', -12)).toEqual("wxyz");
209+
expect(limitTo(arrayLike, 4, '-12')).toEqual(['a', 'b', 'c', 'd']);
210+
expect(limitTo(arrayLike, '-4', -12)).toEqual(['e', 'f', 'g', 'h']);
156211
});
157212

158213
it('should return the entire string beginning from Y if X is positive and X+Y exceeds input length', function() {
159214
expect(limitTo(items, 7, 3)).toEqual(['d', 'e', 'f', 'g', 'h']);
160215
expect(limitTo(items, 7, -3)).toEqual(['f', 'g', 'h']);
161216
expect(limitTo(str, 6, 3)).toEqual("wxyz");
162217
expect(limitTo(str, 6, -3)).toEqual("xyz");
218+
expect(limitTo(arrayLike, 7, 3)).toEqual(['d', 'e', 'f', 'g', 'h']);
219+
expect(limitTo(arrayLike, 7, -3)).toEqual(['f', 'g', 'h']);
163220
});
164221

165222
it('should return the entire string until index Y if X is negative and X+Y exceeds input length', function() {
166223
expect(limitTo(items, -7, 3)).toEqual(['a', 'b', 'c']);
167224
expect(limitTo(items, -7, -3)).toEqual(['a', 'b', 'c', 'd', 'e']);
168225
expect(limitTo(str, -6, 3)).toEqual("tuv");
169226
expect(limitTo(str, -6, -3)).toEqual("tuvw");
227+
expect(limitTo(arrayLike, -7, 3)).toEqual(['a', 'b', 'c']);
228+
expect(limitTo(arrayLike, -7, -3)).toEqual(['a', 'b', 'c', 'd', 'e']);
170229
});
171230
});

0 commit comments

Comments
 (0)