@@ -4,15 +4,35 @@ describe('Filter: limitTo', function() {
44 var items ;
55 var str ;
66 var number ;
7+ var arrayLike ;
78 var limitTo ;
89
910 beforeEach ( inject ( function ( $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+ function isInObject ( obj ) {
31+ return function ( key ) {
32+ return ! ! obj [ key ] ;
33+ } ;
34+ }
35+
1636
1737 it ( 'should return the first X items when X is positive' , function ( ) {
1838 expect ( limitTo ( items , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
@@ -21,20 +41,26 @@ describe('Filter: limitTo', function() {
2141 expect ( limitTo ( str , '3' ) ) . toEqual ( "tuv" ) ;
2242 expect ( limitTo ( number , 3 ) ) . toEqual ( "100" ) ;
2343 expect ( limitTo ( number , '3' ) ) . toEqual ( "100" ) ;
44+ expect ( limitTo ( arrayLike , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
45+ expect ( limitTo ( arrayLike , '3' ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
2446 } ) ;
2547
2648 it ( 'should return the first X items beginning from index Y when X and Y are positive' , function ( ) {
2749 expect ( limitTo ( items , 3 , '3' ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
2850 expect ( limitTo ( items , '3' , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
2951 expect ( limitTo ( str , 3 , 3 ) ) . toEqual ( "wxy" ) ;
3052 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' ] ) ;
3155 } ) ;
3256
3357 it ( 'should return the first X items beginning from index Y when X is positive and Y is negative' , function ( ) {
3458 expect ( limitTo ( items , 3 , '-3' ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
3559 expect ( limitTo ( items , '3' , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
3660 expect ( limitTo ( str , 3 , - 3 ) ) . toEqual ( "xyz" ) ;
3761 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' ] ) ;
3864 } ) ;
3965
4066 it ( 'should return the last X items when X is negative' , function ( ) {
@@ -44,25 +70,33 @@ describe('Filter: limitTo', function() {
4470 expect ( limitTo ( str , '-3' ) ) . toEqual ( "xyz" ) ;
4571 expect ( limitTo ( number , - 3 ) ) . toEqual ( "045" ) ;
4672 expect ( limitTo ( number , '-3' ) ) . toEqual ( "045" ) ;
73+ expect ( limitTo ( arrayLike , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
74+ expect ( limitTo ( arrayLike , '-3' ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
4775 } ) ;
4876
4977 it ( 'should return the last X items until index Y when X and Y are negative' , function ( ) {
5078 expect ( limitTo ( items , - 3 , '-3' ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
5179 expect ( limitTo ( items , '-3' , - 3 ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
5280 expect ( limitTo ( str , - 3 , - 3 ) ) . toEqual ( "uvw" ) ;
5381 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' ] ) ;
5484 } ) ;
5585
5686 it ( 'should return the last X items until index Y when X is negative and Y is positive' , function ( ) {
5787 expect ( limitTo ( items , - 3 , '4' ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
5888 expect ( limitTo ( items , '-3' , 4 ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
5989 expect ( limitTo ( str , - 3 , 4 ) ) . toEqual ( "uvw" ) ;
6090 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' ] ) ;
6193 } ) ;
6294
6395 it ( 'should return an empty array when X = 0' , function ( ) {
6496 expect ( limitTo ( items , 0 ) ) . toEqual ( [ ] ) ;
6597 expect ( limitTo ( items , '0' ) ) . toEqual ( [ ] ) ;
98+ expect ( limitTo ( arrayLike , 0 ) ) . toEqual ( [ ] ) ;
99+ expect ( limitTo ( arrayLike , '0' ) ) . toEqual ( [ ] ) ;
66100 } ) ;
67101
68102 it ( 'should return entire array when X cannot be parsed' , function ( ) {
@@ -71,6 +105,11 @@ describe('Filter: limitTo', function() {
71105 expect ( limitTo ( items , 'undefined' ) ) . toEqual ( items ) ;
72106 expect ( limitTo ( items , null ) ) . toEqual ( items ) ;
73107 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 ) ;
74113 } ) ;
75114
76115 it ( 'should return an empty string when X = 0' , function ( ) {
@@ -97,12 +136,16 @@ describe('Filter: limitTo', function() {
97136 expect ( limitTo ( str , '3' , 'undefined' ) ) . toEqual ( limitTo ( str , '3' ) ) ;
98137 expect ( limitTo ( str , '-3' , null ) ) . toEqual ( limitTo ( str , '-3' , 0 ) ) ;
99138 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 ) ) ;
100144 } ) ;
101145
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 ( ) {
103147 expect ( limitTo ( null , 1 ) ) . toEqual ( null ) ;
104148 expect ( limitTo ( undefined , 1 ) ) . toEqual ( undefined ) ;
105- expect ( limitTo ( { } , 1 ) ) . toEqual ( { } ) ;
106149 } ) ;
107150
108151
@@ -111,8 +154,13 @@ describe('Filter: limitTo', function() {
111154 expect ( limitTo ( items , '9' ) ) . toEqual ( items ) ;
112155 expect ( limitTo ( items , - 9 ) ) . toEqual ( items ) ;
113156 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 ) ;
114161
115- expect ( limitTo ( items , 9 ) ) . not . toBe ( items ) ;
162+ expect ( limitTo ( arrayLike , 9 ) ) . not . toBe ( items ) ;
163+ expect ( limitTo ( arrayLike , 9 ) ) . not . toBe ( items ) ;
116164 } ) ;
117165
118166 it ( 'should return the entire string if X exceeds input length' , function ( ) {
@@ -129,6 +177,10 @@ describe('Filter: limitTo', function() {
129177 expect ( limitTo ( items , 'Infinity' ) ) . toEqual ( items ) ;
130178 expect ( limitTo ( items , - Infinity ) ) . toEqual ( items ) ;
131179 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 ) ;
132184 } ) ;
133185
134186 it ( 'should return the entire string when limited by Infinity' , function ( ) {
@@ -141,6 +193,8 @@ describe('Filter: limitTo', function() {
141193 it ( 'should return an empty array if Y exceeds input length' , function ( ) {
142194 expect ( limitTo ( items , '3' , 12 ) ) . toEqual ( [ ] ) ;
143195 expect ( limitTo ( items , - 3 , '12' ) ) . toEqual ( [ ] ) ;
196+ expect ( limitTo ( arrayLike , '3' , 12 ) ) . toEqual ( [ ] ) ;
197+ expect ( limitTo ( arrayLike , - 3 , '12' ) ) . toEqual ( [ ] ) ;
144198 } ) ;
145199
146200 it ( 'should return an empty string if Y exceeds input length' , function ( ) {
@@ -153,19 +207,25 @@ describe('Filter: limitTo', function() {
153207 expect ( limitTo ( items , '-4' , - 12 ) ) . toEqual ( [ 'e' , 'f' , 'g' , 'h' ] ) ;
154208 expect ( limitTo ( str , 4 , '-12' ) ) . toEqual ( "tuvw" ) ;
155209 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' ] ) ;
156212 } ) ;
157213
158214 it ( 'should return the entire string beginning from Y if X is positive and X+Y exceeds input length' , function ( ) {
159215 expect ( limitTo ( items , 7 , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' , 'g' , 'h' ] ) ;
160216 expect ( limitTo ( items , 7 , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
161217 expect ( limitTo ( str , 6 , 3 ) ) . toEqual ( "wxyz" ) ;
162218 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' ] ) ;
163221 } ) ;
164222
165223 it ( 'should return the entire string until index Y if X is negative and X+Y exceeds input length' , function ( ) {
166224 expect ( limitTo ( items , - 7 , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
167225 expect ( limitTo ( items , - 7 , - 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' , 'e' ] ) ;
168226 expect ( limitTo ( str , - 6 , 3 ) ) . toEqual ( "tuv" ) ;
169227 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' ] ) ;
170230 } ) ;
171231} ) ;
0 commit comments