@@ -4,15 +4,33 @@ describe('Filter: limitTo', function() {
4
4
var items ;
5
5
var str ;
6
6
var number ;
7
+ var arrayLike ;
7
8
var limitTo ;
8
9
9
- beforeEach ( inject ( function ( $filter ) {
10
+ beforeEach ( inject ( function ( $document , $ filter) {
10
11
items = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' ] ;
11
12
str = "tuvwxyz" ;
12
13
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
+ } ;
13
27
limitTo = $filter ( 'limitTo' ) ;
14
28
} ) ) ;
15
29
30
+ afterEach ( function ( ) {
31
+
32
+ } ) ;
33
+
16
34
17
35
it ( 'should return the first X items when X is positive' , function ( ) {
18
36
expect ( limitTo ( items , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
@@ -21,20 +39,26 @@ describe('Filter: limitTo', function() {
21
39
expect ( limitTo ( str , '3' ) ) . toEqual ( "tuv" ) ;
22
40
expect ( limitTo ( number , 3 ) ) . toEqual ( "100" ) ;
23
41
expect ( limitTo ( number , '3' ) ) . toEqual ( "100" ) ;
42
+ expect ( limitTo ( arrayLike , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
43
+ expect ( limitTo ( arrayLike , '3' ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
24
44
} ) ;
25
45
26
46
it ( 'should return the first X items beginning from index Y when X and Y are positive' , function ( ) {
27
47
expect ( limitTo ( items , 3 , '3' ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
28
48
expect ( limitTo ( items , '3' , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
29
49
expect ( limitTo ( str , 3 , 3 ) ) . toEqual ( "wxy" ) ;
30
50
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' ] ) ;
31
53
} ) ;
32
54
33
55
it ( 'should return the first X items beginning from index Y when X is positive and Y is negative' , function ( ) {
34
56
expect ( limitTo ( items , 3 , '-3' ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
35
57
expect ( limitTo ( items , '3' , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
36
58
expect ( limitTo ( str , 3 , - 3 ) ) . toEqual ( "xyz" ) ;
37
59
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' ] ) ;
38
62
} ) ;
39
63
40
64
it ( 'should return the last X items when X is negative' , function ( ) {
@@ -44,38 +68,52 @@ describe('Filter: limitTo', function() {
44
68
expect ( limitTo ( str , '-3' ) ) . toEqual ( "xyz" ) ;
45
69
expect ( limitTo ( number , - 3 ) ) . toEqual ( "045" ) ;
46
70
expect ( limitTo ( number , '-3' ) ) . toEqual ( "045" ) ;
71
+ expect ( limitTo ( arrayLike , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
72
+ expect ( limitTo ( arrayLike , '-3' ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
47
73
} ) ;
48
74
49
75
it ( 'should return the last X items until index Y when X and Y are negative' , function ( ) {
50
76
expect ( limitTo ( items , - 3 , '-3' ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
51
77
expect ( limitTo ( items , '-3' , - 3 ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
52
78
expect ( limitTo ( str , - 3 , - 3 ) ) . toEqual ( "uvw" ) ;
53
79
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' ] ) ;
54
82
} ) ;
55
83
56
84
it ( 'should return the last X items until index Y when X is negative and Y is positive' , function ( ) {
57
85
expect ( limitTo ( items , - 3 , '4' ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
58
86
expect ( limitTo ( items , '-3' , 4 ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
59
87
expect ( limitTo ( str , - 3 , 4 ) ) . toEqual ( "uvw" ) ;
60
88
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' ] ) ;
61
91
} ) ;
62
92
63
93
it ( 'should return an empty array when X = 0' , function ( ) {
64
94
expect ( limitTo ( items , 0 ) ) . toEqual ( [ ] ) ;
65
95
expect ( limitTo ( items , '0' ) ) . toEqual ( [ ] ) ;
96
+ expect ( limitTo ( arrayLike , 0 ) ) . toEqual ( [ ] ) ;
97
+ expect ( limitTo ( arrayLike , '0' ) ) . toEqual ( [ ] ) ;
66
98
} ) ;
67
99
68
100
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 ) ;
74
111
} ) ;
75
112
76
113
it ( 'should return an empty string when X = 0' , function ( ) {
77
114
expect ( limitTo ( str , 0 ) ) . toEqual ( "" ) ;
78
115
expect ( limitTo ( str , '0' ) ) . toEqual ( "" ) ;
116
+ expect ( limitTo ( { } , 1 ) ) . toEqual ( { } ) ;
79
117
} ) ;
80
118
81
119
it ( 'should return entire string when X cannot be parsed' , function ( ) {
@@ -97,12 +135,16 @@ describe('Filter: limitTo', function() {
97
135
expect ( limitTo ( str , '3' , 'undefined' ) ) . toEqual ( limitTo ( str , '3' ) ) ;
98
136
expect ( limitTo ( str , '-3' , null ) ) . toEqual ( limitTo ( str , '-3' , 0 ) ) ;
99
137
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 ) ) ;
100
143
} ) ;
101
144
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 ( ) {
103
146
expect ( limitTo ( null , 1 ) ) . toEqual ( null ) ;
104
147
expect ( limitTo ( undefined , 1 ) ) . toEqual ( undefined ) ;
105
- expect ( limitTo ( { } , 1 ) ) . toEqual ( { } ) ;
106
148
} ) ;
107
149
108
150
@@ -111,8 +153,13 @@ describe('Filter: limitTo', function() {
111
153
expect ( limitTo ( items , '9' ) ) . toEqual ( items ) ;
112
154
expect ( limitTo ( items , - 9 ) ) . toEqual ( items ) ;
113
155
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 ) ;
114
160
115
161
expect ( limitTo ( items , 9 ) ) . not . toBe ( items ) ;
162
+ expect ( limitTo ( arrayLike , 9 ) ) . not . toBe ( items ) ;
116
163
} ) ;
117
164
118
165
it ( 'should return the entire string if X exceeds input length' , function ( ) {
@@ -129,6 +176,10 @@ describe('Filter: limitTo', function() {
129
176
expect ( limitTo ( items , 'Infinity' ) ) . toEqual ( items ) ;
130
177
expect ( limitTo ( items , - Infinity ) ) . toEqual ( items ) ;
131
178
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 ) ;
132
183
} ) ;
133
184
134
185
it ( 'should return the entire string when limited by Infinity' , function ( ) {
@@ -141,6 +192,8 @@ describe('Filter: limitTo', function() {
141
192
it ( 'should return an empty array if Y exceeds input length' , function ( ) {
142
193
expect ( limitTo ( items , '3' , 12 ) ) . toEqual ( [ ] ) ;
143
194
expect ( limitTo ( items , - 3 , '12' ) ) . toEqual ( [ ] ) ;
195
+ expect ( limitTo ( arrayLike , '3' , 12 ) ) . toEqual ( [ ] ) ;
196
+ expect ( limitTo ( arrayLike , - 3 , '12' ) ) . toEqual ( [ ] ) ;
144
197
} ) ;
145
198
146
199
it ( 'should return an empty string if Y exceeds input length' , function ( ) {
@@ -153,19 +206,25 @@ describe('Filter: limitTo', function() {
153
206
expect ( limitTo ( items , '-4' , - 12 ) ) . toEqual ( [ 'e' , 'f' , 'g' , 'h' ] ) ;
154
207
expect ( limitTo ( str , 4 , '-12' ) ) . toEqual ( "tuvw" ) ;
155
208
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' ] ) ;
156
211
} ) ;
157
212
158
213
it ( 'should return the entire string beginning from Y if X is positive and X+Y exceeds input length' , function ( ) {
159
214
expect ( limitTo ( items , 7 , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' , 'g' , 'h' ] ) ;
160
215
expect ( limitTo ( items , 7 , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
161
216
expect ( limitTo ( str , 6 , 3 ) ) . toEqual ( "wxyz" ) ;
162
217
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' ] ) ;
163
220
} ) ;
164
221
165
222
it ( 'should return the entire string until index Y if X is negative and X+Y exceeds input length' , function ( ) {
166
223
expect ( limitTo ( items , - 7 , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
167
224
expect ( limitTo ( items , - 7 , - 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' , 'e' ] ) ;
168
225
expect ( limitTo ( str , - 6 , 3 ) ) . toEqual ( "tuv" ) ;
169
226
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' ] ) ;
170
229
} ) ;
171
230
} ) ;
0 commit comments