|
7 | 7 | *
|
8 | 8 | * @description
|
9 | 9 | * 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. |
12 | 13 | *
|
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. |
14 | 15 | * @param {string|number} limit The length of the returned array or string. If the `limit` number
|
15 | 16 | * is positive, `limit` number of items from the beginning of the source array/string are copied.
|
16 | 17 | * If the number is negative, `limit` number of items from the end of the source array/string
|
|
26 | 27 | .controller('ExampleController', ['$scope', function($scope) {
|
27 | 28 | $scope.numbers = [1,2,3,4,5,6,7,8,9];
|
28 | 29 | $scope.letters = "abcdefghi";
|
| 30 | + $scope.longNumber = 2345432342; |
29 | 31 | $scope.numLimit = 3;
|
30 | 32 | $scope.letterLimit = 3;
|
| 33 | + $scope.longNumberLimit = 3; |
31 | 34 | }]);
|
32 | 35 | </script>
|
33 | 36 | <div ng-controller="ExampleController">
|
34 | 37 | Limit {{numbers}} to: <input type="integer" ng-model="numLimit">
|
35 | 38 | <p>Output numbers: {{ numbers | limitTo:numLimit }}</p>
|
36 | 39 | Limit {{letters}} to: <input type="integer" ng-model="letterLimit">
|
37 | 40 | <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> |
38 | 43 | </div>
|
39 | 44 | </file>
|
40 | 45 | <file name="protractor.js" type="protractor">
|
41 | 46 | var numLimitInput = element(by.model('numLimit'));
|
42 | 47 | var letterLimitInput = element(by.model('letterLimit'));
|
| 48 | + var longNumberLimitInput = element(by.model('longNumberLimit')); |
43 | 49 | var limitedNumbers = element(by.binding('numbers | limitTo:numLimit'));
|
44 | 50 | var limitedLetters = element(by.binding('letters | limitTo:letterLimit'));
|
| 51 | + var limitedLongNumber = element(by.binding('longNumber | limitTo:longNumberLimit')); |
45 | 52 |
|
46 | 53 | it('should limit the number array to first three items', function() {
|
47 | 54 | expect(numLimitInput.getAttribute('value')).toBe('3');
|
48 | 55 | expect(letterLimitInput.getAttribute('value')).toBe('3');
|
| 56 | + expect(longNumberLimitInput.getAttribute('value')).toBe('3'); |
49 | 57 | expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3]');
|
50 | 58 | expect(limitedLetters.getText()).toEqual('Output letters: abc');
|
| 59 | + expect(limitedLongNumber.getText()).toEqual('Output long number: 234'); |
51 | 60 | });
|
52 | 61 |
|
53 | 62 | it('should update the output when -3 is entered', function() {
|
54 | 63 | numLimitInput.clear();
|
55 | 64 | numLimitInput.sendKeys('-3');
|
56 | 65 | letterLimitInput.clear();
|
57 | 66 | letterLimitInput.sendKeys('-3');
|
| 67 | + longNumberLimitInput.clear(); |
| 68 | + longNumberLimitInput.sendKeys('-3'); |
58 | 69 | expect(limitedNumbers.getText()).toEqual('Output numbers: [7,8,9]');
|
59 | 70 | expect(limitedLetters.getText()).toEqual('Output letters: ghi');
|
| 71 | + expect(limitedLongNumber.getText()).toEqual('Output long number: 342'); |
60 | 72 | });
|
61 | 73 |
|
62 | 74 | it('should not exceed the maximum size of input array', function() {
|
63 | 75 | numLimitInput.clear();
|
64 | 76 | numLimitInput.sendKeys('100');
|
65 | 77 | letterLimitInput.clear();
|
66 | 78 | letterLimitInput.sendKeys('100');
|
| 79 | + longNumberLimitInput.clear(); |
| 80 | + longNumberLimitInput.sendKeys('100'); |
67 | 81 | expect(limitedNumbers.getText()).toEqual('Output numbers: [1,2,3,4,5,6,7,8,9]');
|
68 | 82 | expect(limitedLetters.getText()).toEqual('Output letters: abcdefghi');
|
| 83 | + expect(limitedLongNumber.getText()).toEqual('Output long number: 2345432342'); |
69 | 84 | });
|
70 | 85 | </file>
|
71 | 86 | </example>
|
72 | 87 | */
|
73 | 88 | function limitToFilter(){
|
74 | 89 | return function(input, limit) {
|
| 90 | + if (isNumber(input)) input = input.toString(); |
75 | 91 | if (!isArray(input) && !isString(input)) return input;
|
76 | 92 |
|
77 | 93 | if (Math.abs(Number(limit)) === Infinity) {
|
|
0 commit comments