Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(limitTo): Add support for numbers in limitTo #8926

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/ng/filter/limitTo.js
Original file line number Diff line number Diff line change
@@ -7,10 +7,11 @@
*
* @description
* Creates a new array or string containing only a specified number of elements. The elements
* are taken from either the beginning or the end of the source array or string, as specified by
* the value and sign (positive or negative) of `limit`.
* are taken from either the beginning or the end of the source array, string or number, as specified by
* the value and sign (positive or negative) of `limit`. If a number is used as input, it is
* converted to a string.
*
* @param {Array|string} input Source array or string to be limited.
* @param {Array|string|number} input Source array, string or number to be limited.
* @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
@@ -26,52 +27,67 @@
.controller('ExampleController', ['$scope', function($scope) {
$scope.numbers = [1,2,3,4,5,6,7,8,9];
$scope.letters = "abcdefghi";
$scope.longNumber = 2345432342;
$scope.numLimit = 3;
$scope.letterLimit = 3;
$scope.longNumberLimit = 3;
}]);
</script>
<div ng-controller="ExampleController">
Limit {{numbers}} to: <input type="integer" ng-model="numLimit">
<p>Output numbers: {{ numbers | limitTo:numLimit }}</p>
Limit {{letters}} to: <input type="integer" ng-model="letterLimit">
<p>Output letters: {{ letters | limitTo:letterLimit }}</p>
Limit {{longNumber}} to: <input type="integer" ng-model="longNumberLimit">
<p>Output long number: {{ longNumber | limitTo:longNumberLimit }}</p>
</div>
</file>
<file name="protractor.js" type="protractor">
var numLimitInput = element(by.model('numLimit'));
var letterLimitInput = element(by.model('letterLimit'));
var longNumberLimitInput = element(by.model('longNumberLimit'));
var limitedNumbers = element(by.binding('numbers | limitTo:numLimit'));
var limitedLetters = element(by.binding('letters | limitTo:letterLimit'));
var limitedLongNumber = element(by.binding('longNumber | limitTo:longNumberLimit'));

it('should limit the number array to first three items', function() {
expect(numLimitInput.getAttribute('value')).toBe('3');
expect(letterLimitInput.getAttribute('value')).toBe('3');
expect(longNumberLimitInput.getAttribute('value')).toBe('3');
expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3]');
expect(limitedLetters.getText()).toEqual('Output letters: abc');
expect(limitedLongNumber.getText()).toEqual('Output long number: 234');
});

it('should update the output when -3 is entered', function() {
numLimitInput.clear();
numLimitInput.sendKeys('-3');
letterLimitInput.clear();
letterLimitInput.sendKeys('-3');
longNumberLimitInput.clear();
longNumberLimitInput.sendKeys('-3');
expect(limitedNumbers.getText()).toEqual('Output numbers: [7,8,9]');
expect(limitedLetters.getText()).toEqual('Output letters: ghi');
expect(limitedLongNumber.getText()).toEqual('Output long number: 342');
});

it('should not exceed the maximum size of input array', function() {
numLimitInput.clear();
numLimitInput.sendKeys('100');
letterLimitInput.clear();
letterLimitInput.sendKeys('100');
longNumberLimitInput.clear();
longNumberLimitInput.sendKeys('100');
expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3,4,5,6,7,8,9]');
expect(limitedLetters.getText()).toEqual('Output letters: abcdefghi');
expect(limitedLongNumber.getText()).toEqual('Output long number: 2345432342');
});
</file>
</example>
*/
function limitToFilter(){
return function(input, limit) {
if (isNumber(input)) input = input.toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this PR is missing the type signature / doc update

if (!isArray(input) && !isString(input)) return input;

if (Math.abs(Number(limit)) === Infinity) {
11 changes: 9 additions & 2 deletions test/ng/filter/limitToSpec.js
Original file line number Diff line number Diff line change
@@ -3,11 +3,13 @@
describe('Filter: limitTo', function() {
var items;
var str;
var number;
var limitTo;

beforeEach(inject(function($filter) {
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
str = "tuvwxyz";
number = 100.045;
limitTo = $filter('limitTo');
}));

@@ -17,6 +19,8 @@ describe('Filter: limitTo', function() {
expect(limitTo(items, '3')).toEqual(['a', 'b', 'c']);
expect(limitTo(str, 3)).toEqual("tuv");
expect(limitTo(str, '3')).toEqual("tuv");
expect(limitTo(number, 3)).toEqual("100");
expect(limitTo(number, '3')).toEqual("100");
});


@@ -25,6 +29,8 @@ describe('Filter: limitTo', function() {
expect(limitTo(items, '-3')).toEqual(['f', 'g', 'h']);
expect(limitTo(str, -3)).toEqual("xyz");
expect(limitTo(str, '-3')).toEqual("xyz");
expect(limitTo(number, -3)).toEqual("045");
expect(limitTo(number, '-3')).toEqual("045");
});


@@ -45,8 +51,7 @@ describe('Filter: limitTo', function() {
});


it('should return input if not String or Array', function() {
expect(limitTo(1,1)).toEqual(1);
it('should return input if not String or Array or Number', function() {
expect(limitTo(null, 1)).toEqual(null);
expect(limitTo(undefined, 1)).toEqual(undefined);
expect(limitTo({}, 1)).toEqual({});
@@ -67,6 +72,8 @@ describe('Filter: limitTo', function() {
expect(limitTo(str, '9')).toEqual(str);
expect(limitTo(str, -9)).toEqual(str);
expect(limitTo(str, '-9')).toEqual(str);
expect(limitTo(number, 9)).toEqual(number.toString());
expect(limitTo(number, '-9')).toEqual(number.toString());
});

it('should return entire input array when limited by Infinity', function() {