@@ -697,8 +697,8 @@ var angularArray = {
697
697
* @param {string|Number } limit The length of the returned array. If the `limit` number is
698
698
* positive, `limit` number of items from the beginning of the source array are copied.
699
699
* If the number is negative, `limit` number of items from the end of the source array are
700
- * copied.
701
- * @returns {Array } A new sub-array of length `limit`.
700
+ * copied. The `limit` will be trimmed if it exceeds `array.length`
701
+ * @returns {Array } A new sub-array of length `limit` or less if input array had less than `limit` elements .
702
702
*
703
703
* @example
704
704
<doc:example>
@@ -714,10 +714,17 @@ var angularArray = {
714
714
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3]');
715
715
});
716
716
717
- it('should update the output when -3 is entered', function() {
717
+ it('should update the output when -3 is entered', function() {
718
718
input('limit').enter(-3);
719
719
expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]');
720
720
});
721
+
722
+ it('should not exceed the maximum size of input array', function() {
723
+ input('limit').enter(100);
724
+ expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]');
725
+ });
726
+
727
+
721
728
</doc:scenario>
722
729
</doc:example>
723
730
*/
@@ -726,6 +733,16 @@ var angularArray = {
726
733
var out = [ ] ,
727
734
i , n ;
728
735
736
+ // check that array is iterable
737
+ if ( ! array || ! ( array instanceof Array ) )
738
+ return out ;
739
+
740
+ // if abs(limit) exceeds maximum length, trim it
741
+ if ( limit > array . length )
742
+ limit = array . length ;
743
+ else if ( limit < - array . length )
744
+ limit = - array . length ;
745
+
729
746
if ( limit > 0 ) {
730
747
i = 0 ;
731
748
n = limit ;
0 commit comments