@@ -23,6 +23,19 @@ describe('Filter: limitTo', function() {
23
23
expect ( limitTo ( number , '3' ) ) . toEqual ( "100" ) ;
24
24
} ) ;
25
25
26
+ it ( 'should return the first X items beginning from index Y when X and Y are positive' , function ( ) {
27
+ expect ( limitTo ( items , 3 , '3' ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
28
+ expect ( limitTo ( items , '3' , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' ] ) ;
29
+ expect ( limitTo ( str , 3 , 3 ) ) . toEqual ( "wxy" ) ;
30
+ expect ( limitTo ( str , '3' , '3' ) ) . toEqual ( "wxy" ) ;
31
+ } ) ;
32
+
33
+ it ( 'should return the first X items beginning from index Y when X is positive and Y is negative' , function ( ) {
34
+ expect ( limitTo ( items , 3 , '-3' ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
35
+ expect ( limitTo ( items , '3' , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
36
+ expect ( limitTo ( str , 3 , - 3 ) ) . toEqual ( "xyz" ) ;
37
+ expect ( limitTo ( str , '3' , '-3' ) ) . toEqual ( "xyz" ) ;
38
+ } ) ;
26
39
27
40
it ( 'should return the last X items when X is negative' , function ( ) {
28
41
expect ( limitTo ( items , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
@@ -33,6 +46,19 @@ describe('Filter: limitTo', function() {
33
46
expect ( limitTo ( number , '-3' ) ) . toEqual ( "045" ) ;
34
47
} ) ;
35
48
49
+ it ( 'should return the last X items until index Y when X and Y are negative' , function ( ) {
50
+ expect ( limitTo ( items , - 3 , '-3' ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
51
+ expect ( limitTo ( items , '-3' , - 3 ) ) . toEqual ( [ 'c' , 'd' , 'e' ] ) ;
52
+ expect ( limitTo ( str , - 3 , - 3 ) ) . toEqual ( "uvw" ) ;
53
+ expect ( limitTo ( str , '-3' , '-3' ) ) . toEqual ( "uvw" ) ;
54
+ } ) ;
55
+
56
+ it ( 'should return the last X items until index Y when X is negative and Y is positive' , function ( ) {
57
+ expect ( limitTo ( items , - 3 , '4' ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
58
+ expect ( limitTo ( items , '-3' , 4 ) ) . toEqual ( [ 'b' , 'c' , 'd' ] ) ;
59
+ expect ( limitTo ( str , - 3 , 4 ) ) . toEqual ( "uvw" ) ;
60
+ expect ( limitTo ( str , '-3' , '4' ) ) . toEqual ( "uvw" ) ;
61
+ } ) ;
36
62
37
63
it ( 'should return an empty array when X = 0' , function ( ) {
38
64
expect ( limitTo ( items , 0 ) ) . toEqual ( [ ] ) ;
@@ -60,6 +86,18 @@ describe('Filter: limitTo', function() {
60
86
expect ( limitTo ( str , undefined ) ) . toEqual ( str ) ;
61
87
} ) ;
62
88
89
+ it ( 'should take 0 as beginning index value when Y cannot be parsed' , function ( ) {
90
+ expect ( limitTo ( items , 3 , 'bogus' ) ) . toEqual ( limitTo ( items , 3 , 0 ) ) ;
91
+ expect ( limitTo ( items , - 3 , 'null' ) ) . toEqual ( limitTo ( items , - 3 ) ) ;
92
+ expect ( limitTo ( items , '3' , 'undefined' ) ) . toEqual ( limitTo ( items , '3' , 0 ) ) ;
93
+ expect ( limitTo ( items , '-3' , null ) ) . toEqual ( limitTo ( items , '-3' ) ) ;
94
+ expect ( limitTo ( items , 3 , undefined ) ) . toEqual ( limitTo ( items , 3 , 0 ) ) ;
95
+ expect ( limitTo ( str , 3 , 'bogus' ) ) . toEqual ( limitTo ( str , 3 ) ) ;
96
+ expect ( limitTo ( str , - 3 , 'null' ) ) . toEqual ( limitTo ( str , - 3 , 0 ) ) ;
97
+ expect ( limitTo ( str , '3' , 'undefined' ) ) . toEqual ( limitTo ( str , '3' ) ) ;
98
+ expect ( limitTo ( str , '-3' , null ) ) . toEqual ( limitTo ( str , '-3' , 0 ) ) ;
99
+ expect ( limitTo ( str , 3 , undefined ) ) . toEqual ( limitTo ( str , 3 ) ) ;
100
+ } ) ;
63
101
64
102
it ( 'should return input if not String or Array or Number' , function ( ) {
65
103
expect ( limitTo ( null , 1 ) ) . toEqual ( null ) ;
@@ -99,4 +137,32 @@ describe('Filter: limitTo', function() {
99
137
expect ( limitTo ( str , - Infinity ) ) . toEqual ( str ) ;
100
138
expect ( limitTo ( str , '-Infinity' ) ) . toEqual ( str ) ;
101
139
} ) ;
140
+
141
+ it ( 'should return an empty array if Y exceeds input length' , function ( ) {
142
+ expect ( limitTo ( items , '3' , 12 ) ) . toEqual ( [ ] ) ;
143
+ expect ( limitTo ( items , 4 , '-12' ) ) . toEqual ( [ ] ) ;
144
+ expect ( limitTo ( items , - 3 , '12' ) ) . toEqual ( [ ] ) ;
145
+ expect ( limitTo ( items , '-4' , - 12 ) ) . toEqual ( [ ] ) ;
146
+ } ) ;
147
+
148
+ it ( 'should return an empty string if Y exceeds input length' , function ( ) {
149
+ expect ( limitTo ( str , '3' , 12 ) ) . toEqual ( "" ) ;
150
+ expect ( limitTo ( str , 4 , '-12' ) ) . toEqual ( "" ) ;
151
+ expect ( limitTo ( str , - 3 , '12' ) ) . toEqual ( "" ) ;
152
+ expect ( limitTo ( str , '-4' , - 12 ) ) . toEqual ( "" ) ;
153
+ } ) ;
154
+
155
+ it ( 'should return the entire string beginning from Y if X is positive and X+Y exceeds input length' , function ( ) {
156
+ expect ( limitTo ( items , 7 , 3 ) ) . toEqual ( [ 'd' , 'e' , 'f' , 'g' , 'h' ] ) ;
157
+ expect ( limitTo ( items , 7 , - 3 ) ) . toEqual ( [ 'f' , 'g' , 'h' ] ) ;
158
+ expect ( limitTo ( str , 6 , 3 ) ) . toEqual ( "wxyz" ) ;
159
+ expect ( limitTo ( str , 6 , - 3 ) ) . toEqual ( "xyz" ) ;
160
+ } ) ;
161
+
162
+ it ( 'should return the entire string until index Y if X is negative and X+Y exceeds input length' , function ( ) {
163
+ expect ( limitTo ( items , - 7 , 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
164
+ expect ( limitTo ( items , - 7 , - 3 ) ) . toEqual ( [ 'a' , 'b' , 'c' , 'd' , 'e' ] ) ;
165
+ expect ( limitTo ( str , - 6 , 3 ) ) . toEqual ( "tuv" ) ;
166
+ expect ( limitTo ( str , - 6 , - 3 ) ) . toEqual ( "tuvw" ) ;
167
+ } ) ;
102
168
} ) ;
0 commit comments