mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-01-12 22:45:52 +08:00
feat(limitTo): ignore limit when invalid
BREAKING CHANGE: limitTo changed behavior when limit value is invalid. Instead of returning empty object/array it returns unchanged input. Closes #10510
This commit is contained in:
@@ -15,7 +15,8 @@
|
||||
* @param {string|number} limit The length of the returned array or string. If the `limit` number
|
||||
* is positive, `limit` number of items from the beginning of the source array/string are copied.
|
||||
* If the number is negative, `limit` number of items from the end of the source array/string
|
||||
* are copied. The `limit` will be trimmed if it exceeds `array.length`
|
||||
* are copied. The `limit` will be trimmed if it exceeds `array.length`. If `limit` is undefined,
|
||||
* the input will be returned unchanged.
|
||||
* @returns {Array|string} A new sub-array or substring of length `limit` or less if input array
|
||||
* had less than `limit` elements.
|
||||
*
|
||||
@@ -88,20 +89,16 @@
|
||||
*/
|
||||
function limitToFilter() {
|
||||
return function(input, limit) {
|
||||
if (isNumber(input)) input = input.toString();
|
||||
if (!isArray(input) && !isString(input)) return input;
|
||||
|
||||
if (Math.abs(Number(limit)) === Infinity) {
|
||||
limit = Number(limit);
|
||||
} else {
|
||||
limit = int(limit);
|
||||
}
|
||||
if (isNaN(limit)) return input;
|
||||
|
||||
//NaN check on limit
|
||||
if (limit) {
|
||||
return limit > 0 ? input.slice(0, limit) : input.slice(limit);
|
||||
} else {
|
||||
return isString(input) ? "" : [];
|
||||
}
|
||||
if (isNumber(input)) input = input.toString();
|
||||
if (!isArray(input) && !isString(input)) return input;
|
||||
|
||||
return limit >= 0 ? input.slice(0, limit) : input.slice(limit);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,20 +34,30 @@ describe('Filter: limitTo', function() {
|
||||
});
|
||||
|
||||
|
||||
it('should return an empty array when X cannot be parsed', function() {
|
||||
expect(limitTo(items, 'bogus')).toEqual([]);
|
||||
expect(limitTo(items, 'null')).toEqual([]);
|
||||
expect(limitTo(items, 'undefined')).toEqual([]);
|
||||
expect(limitTo(items, null)).toEqual([]);
|
||||
expect(limitTo(items, undefined)).toEqual([]);
|
||||
it('should return an empty array when X = 0', function() {
|
||||
expect(limitTo(items, 0)).toEqual([]);
|
||||
expect(limitTo(items, '0')).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return an empty string when X cannot be parsed', function() {
|
||||
expect(limitTo(str, 'bogus')).toEqual("");
|
||||
expect(limitTo(str, 'null')).toEqual("");
|
||||
expect(limitTo(str, 'undefined')).toEqual("");
|
||||
expect(limitTo(str, null)).toEqual("");
|
||||
expect(limitTo(str, undefined)).toEqual("");
|
||||
it('should return entire array when X cannot be parsed', function() {
|
||||
expect(limitTo(items, 'bogus')).toEqual(items);
|
||||
expect(limitTo(items, 'null')).toEqual(items);
|
||||
expect(limitTo(items, 'undefined')).toEqual(items);
|
||||
expect(limitTo(items, null)).toEqual(items);
|
||||
expect(limitTo(items, undefined)).toEqual(items);
|
||||
});
|
||||
|
||||
it('should return an empty string when X = 0', function() {
|
||||
expect(limitTo(str, 0)).toEqual("");
|
||||
expect(limitTo(str, '0')).toEqual("");
|
||||
});
|
||||
|
||||
it('should return entire string when X cannot be parsed', function() {
|
||||
expect(limitTo(str, 'bogus')).toEqual(str);
|
||||
expect(limitTo(str, 'null')).toEqual(str);
|
||||
expect(limitTo(str, 'undefined')).toEqual(str);
|
||||
expect(limitTo(str, null)).toEqual(str);
|
||||
expect(limitTo(str, undefined)).toEqual(str);
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user