diff options
| author | TEHEK Firefox | 2011-09-23 18:36:09 +0000 | 
|---|---|---|
| committer | Igor Minar | 2011-10-05 11:01:32 -0700 | 
| commit | c115fa99240a55e32e1fa39f037056df429be741 (patch) | |
| tree | 26c3dcfbcc4d032296e018f552a5da9a88fb1ad2 | |
| parent | b7a7fc7065a7bd6031b82a748105cf0415cb68e4 (diff) | |
| download | angular.js-c115fa99240a55e32e1fa39f037056df429be741.tar.bz2 | |
fix($limitTo): properly handle excessive limits
`angular.Array.limitTo`'s  result should not exceed original input array size
Closes #571
| -rw-r--r-- | src/apis.js | 20 | ||||
| -rw-r--r-- | test/ApiSpecs.js | 20 | 
2 files changed, 38 insertions, 2 deletions
| diff --git a/src/apis.js b/src/apis.js index 9f081012..7aa9f0c2 100644 --- a/src/apis.js +++ b/src/apis.js @@ -697,8 +697,9 @@ var angularArray = {     * @param {string|Number} limit The length of the returned array. If the `limit` number is     *     positive, `limit` number of items from the beginning of the source array are copied.     *     If the number is negative, `limit` number  of items from the end of the source array are -   *     copied. -   * @returns {Array} A new sub-array of length `limit`. +   *     copied. The `limit` will be trimmed if it exceeds `array.length` +   * @returns {Array} A new sub-array of length `limit` or less if input array had less than `limit` +   *     elements.     *     * @example       <doc:example> @@ -718,6 +719,11 @@ var angularArray = {             input('limit').enter(-3);             expect(binding('numbers.$limitTo(limit) | json')).toEqual('[7,8,9]');           }); + +         it('should not exceed the maximum size of input array', function() { +           input('limit').enter(100); +           expect(binding('numbers.$limitTo(limit) | json')).toEqual('[1,2,3,4,5,6,7,8,9]'); +         });         </doc:scenario>       </doc:example>     */ @@ -726,6 +732,16 @@ var angularArray = {      var out = [],          i, n; +    // check that array is iterable +    if (!array || !(array instanceof Array)) +      return out; + +    // if abs(limit) exceeds maximum length, trim it +    if (limit > array.length) +      limit = array.length; +    else if (limit < -array.length) +      limit = -array.length; +      if (limit > 0) {        i = 0;        n = limit; diff --git a/test/ApiSpecs.js b/test/ApiSpecs.js index 46c42e53..ef25bb41 100644 --- a/test/ApiSpecs.js +++ b/test/ApiSpecs.js @@ -175,6 +175,26 @@ describe('api', function(){          expect(angular.Array.limitTo(items, null)).toEqual([]);          expect(angular.Array.limitTo(items, undefined)).toEqual([]);        }); + + +      it('should return an empty array when input is not Array type', function() { +        expect(angular.Array.limitTo('bogus', 1)).toEqual([]); +        expect(angular.Array.limitTo(null, 1)).toEqual([]); +        expect(angular.Array.limitTo(undefined, 1)).toEqual([]); +        expect(angular.Array.limitTo(null, 1)).toEqual([]); +        expect(angular.Array.limitTo(undefined, 1)).toEqual([]); +        expect(angular.Array.limitTo({}, 1)).toEqual([]); +      }); + + +      it('should return a copy of input array if X is exceeds array length', function () { +        expect(angular.Array.limitTo(items, 19)).toEqual(items); +        expect(angular.Array.limitTo(items, '9')).toEqual(items); +        expect(angular.Array.limitTo(items, -9)).toEqual(items); +        expect(angular.Array.limitTo(items, '-9')).toEqual(items); + +        expect(angular.Array.limitTo(items, 9)).not.toBe(items); +      });      }); | 
