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