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