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

Commit 7e01fbe

Browse files
committed
Add Numbers Support in limitTo filter
feat (limitTo) : Add support for numbers in limitTo Added support for numbers as input in limitTo filter : - Will treat number as a string and limit number of places shown. Closes #8926
1 parent 6d1e7cd commit 7e01fbe

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/ng/filter/limitTo.js

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
*/
7373
function limitToFilter(){
7474
return function(input, limit) {
75+
if (typeof input == "number") input = input.toString();
7576
if (!isArray(input) && !isString(input)) return input;
7677

7778
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)