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

Commit 1c8a745

Browse files
hjastbtford
authored andcommitted
feat(limitTo): support numeric input to limitTo
Closes #8926
1 parent 9cd272a commit 1c8a745

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/ng/filter/limitTo.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
*
88
* @description
99
* Creates a new array or string containing only a specified number of elements. The elements
10-
* are taken from either the beginning or the end of the source array or string, as specified by
11-
* the value and sign (positive or negative) of `limit`.
10+
* are taken from either the beginning or the end of the source array, string or number, as specified by
11+
* the value and sign (positive or negative) of `limit`. If a number is used as input, it is
12+
* converted to a string.
1213
*
13-
* @param {Array|string} input Source array or string to be limited.
14+
* @param {Array|string|number} input Source array, string or number to be limited.
1415
* @param {string|number} limit The length of the returned array or string. If the `limit` number
1516
* is positive, `limit` number of items from the beginning of the source array/string are copied.
1617
* If the number is negative, `limit` number of items from the end of the source array/string
@@ -26,52 +27,67 @@
2627
.controller('ExampleController', ['$scope', function($scope) {
2728
$scope.numbers = [1,2,3,4,5,6,7,8,9];
2829
$scope.letters = "abcdefghi";
30+
$scope.longNumber = 2345432342;
2931
$scope.numLimit = 3;
3032
$scope.letterLimit = 3;
33+
$scope.longNumberLimit = 3;
3134
}]);
3235
</script>
3336
<div ng-controller="ExampleController">
3437
Limit {{numbers}} to: <input type="integer" ng-model="numLimit">
3538
<p>Output numbers: {{ numbers | limitTo:numLimit }}</p>
3639
Limit {{letters}} to: <input type="integer" ng-model="letterLimit">
3740
<p>Output letters: {{ letters | limitTo:letterLimit }}</p>
41+
Limit {{longNumber}} to: <input type="integer" ng-model="longNumberLimit">
42+
<p>Output long number: {{ longNumber | limitTo:longNumberLimit }}</p>
3843
</div>
3944
</file>
4045
<file name="protractor.js" type="protractor">
4146
var numLimitInput = element(by.model('numLimit'));
4247
var letterLimitInput = element(by.model('letterLimit'));
48+
var longNumberLimitInput = element(by.model('longNumberLimit'));
4349
var limitedNumbers = element(by.binding('numbers | limitTo:numLimit'));
4450
var limitedLetters = element(by.binding('letters | limitTo:letterLimit'));
51+
var limitedLongNumber = element(by.binding('longNumber | limitTo:longNumberLimit'));
4552
4653
it('should limit the number array to first three items', function() {
4754
expect(numLimitInput.getAttribute('value')).toBe('3');
4855
expect(letterLimitInput.getAttribute('value')).toBe('3');
56+
expect(longNumberLimitInput.getAttribute('value')).toBe('3');
4957
expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3]');
5058
expect(limitedLetters.getText()).toEqual('Output letters: abc');
59+
expect(limitedLongNumber.getText()).toEqual('Output long number: 234');
5160
});
5261
5362
it('should update the output when -3 is entered', function() {
5463
numLimitInput.clear();
5564
numLimitInput.sendKeys('-3');
5665
letterLimitInput.clear();
5766
letterLimitInput.sendKeys('-3');
67+
longNumberLimitInput.clear();
68+
longNumberLimitInput.sendKeys('-3');
5869
expect(limitedNumbers.getText()).toEqual('Output numbers: [7,8,9]');
5970
expect(limitedLetters.getText()).toEqual('Output letters: ghi');
71+
expect(limitedLongNumber.getText()).toEqual('Output long number: 342');
6072
});
6173
6274
it('should not exceed the maximum size of input array', function() {
6375
numLimitInput.clear();
6476
numLimitInput.sendKeys('100');
6577
letterLimitInput.clear();
6678
letterLimitInput.sendKeys('100');
79+
longNumberLimitInput.clear();
80+
longNumberLimitInput.sendKeys('100');
6781
expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3,4,5,6,7,8,9]');
6882
expect(limitedLetters.getText()).toEqual('Output letters: abcdefghi');
83+
expect(limitedLongNumber.getText()).toEqual('Output long number: 2345432342');
6984
});
7085
</file>
7186
</example>
7287
*/
7388
function limitToFilter(){
7489
return function(input, limit) {
90+
if (isNumber(input)) input = input.toString();
7591
if (!isArray(input) && !isString(input)) return input;
7692

7793
if (Math.abs(Number(limit)) === Infinity) {

test/ng/filter/limitToSpec.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
describe('Filter: limitTo', function() {
44
var items;
55
var str;
6+
var number;
67
var limitTo;
78

89
beforeEach(inject(function($filter) {
910
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
1011
str = "tuvwxyz";
12+
number = 100.045;
1113
limitTo = $filter('limitTo');
1214
}));
1315

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

2226

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

3036

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

4753

48-
it('should return input if not String or Array', function() {
49-
expect(limitTo(1,1)).toEqual(1);
54+
it('should return input if not String or Array or Number', function() {
5055
expect(limitTo(null, 1)).toEqual(null);
5156
expect(limitTo(undefined, 1)).toEqual(undefined);
5257
expect(limitTo({}, 1)).toEqual({});
@@ -67,6 +72,8 @@ describe('Filter: limitTo', function() {
6772
expect(limitTo(str, '9')).toEqual(str);
6873
expect(limitTo(str, -9)).toEqual(str);
6974
expect(limitTo(str, '-9')).toEqual(str);
75+
expect(limitTo(number, 9)).toEqual(number.toString());
76+
expect(limitTo(number, '-9')).toEqual(number.toString());
7077
});
7178

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

0 commit comments

Comments
 (0)