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

Commit 9e96d98

Browse files
jtymespkozlowski-opensource
authored andcommitted
feat(limitTo): limitTo filter accepts strings
This allows strings to be filtered by limitTo, using the same methods Closes #653
1 parent 1e13544 commit 9e96d98

File tree

2 files changed

+74
-38
lines changed

2 files changed

+74
-38
lines changed

src/ng/filter/limitTo.js

+50-33
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,97 @@
66
* @function
77
*
88
* @description
9-
* Creates a new array containing only a specified number of elements in an array. The elements
10-
* are taken from either the beginning or the end of the source array, as specified by the
11-
* value and sign (positive or negative) of `limit`.
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`.
1212
*
1313
* Note: This function is used to augment the `Array` type in Angular expressions. See
1414
* {@link ng.$filter} for more information about Angular arrays.
1515
*
16-
* @param {Array} array Source array to be limited.
17-
* @param {string|Number} limit The length of the returned array. If the `limit` number is
18-
* positive, `limit` number of items from the beginning of the source array are copied.
19-
* If the number is negative, `limit` number of items from the end of the source array are
20-
* copied. The `limit` will be trimmed if it exceeds `array.length`
21-
* @returns {Array} A new sub-array of length `limit` or less if input array had less than `limit`
22-
* elements.
16+
* @param {Array|string} input Source array or string to be limited.
17+
* @param {string|number} limit The length of the returned array or string. If the `limit` number
18+
* is positive, `limit` number of items from the beginning of the source array/string are copied.
19+
* If the number is negative, `limit` number of items from the end of the source array/string
20+
* are copied. The `limit` will be trimmed if it exceeds `array.length`
21+
* @returns {Array|string} A new sub-array or substring of length `limit` or less if input array
22+
* had less than `limit` elements.
2323
*
2424
* @example
2525
<doc:example>
2626
<doc:source>
2727
<script>
2828
function Ctrl($scope) {
2929
$scope.numbers = [1,2,3,4,5,6,7,8,9];
30-
$scope.limit = 3;
30+
$scope.letters = "abcdefghi";
31+
$scope.numLimit = 3;
32+
$scope.letterLimit = 3;
3133
}
3234
</script>
3335
<div ng-controller="Ctrl">
34-
Limit {{numbers}} to: <input type="integer" ng-model="limit">
35-
<p>Output: {{ numbers | limitTo:limit }}</p>
36+
Limit {{numbers}} to: <input type="integer" ng-model="numLimit">
37+
<p>Output numbers: {{ numbers | limitTo:numLimit }}</p>
38+
Limit {{letters}} to: <input type="integer" ng-model="letterLimit">
39+
<p>Output letters: {{ letters | limitTo:letterLimit }}</p>
3640
</div>
3741
</doc:source>
3842
<doc:scenario>
39-
it('should limit the numer array to first three items', function() {
40-
expect(element('.doc-example-live input[ng-model=limit]').val()).toBe('3');
41-
expect(binding('numbers | limitTo:limit')).toEqual('[1,2,3]');
43+
it('should limit the number array to first three items', function() {
44+
expect(element('.doc-example-live input[ng-model=numLimit]').val()).toBe('3');
45+
expect(element('.doc-example-live input[ng-model=letterLimit]').val()).toBe('3');
46+
expect(binding('numbers | limitTo:numLimit')).toEqual('[1,2,3]');
47+
expect(binding('letters | limitTo:letterLimit')).toEqual('abc');
4248
});
4349
4450
it('should update the output when -3 is entered', function() {
45-
input('limit').enter(-3);
46-
expect(binding('numbers | limitTo:limit')).toEqual('[7,8,9]');
51+
input('numLimit').enter(-3);
52+
input('letterLimit').enter(-3);
53+
expect(binding('numbers | limitTo:numLimit')).toEqual('[7,8,9]');
54+
expect(binding('letters | limitTo:letterLimit')).toEqual('ghi');
4755
});
4856
4957
it('should not exceed the maximum size of input array', function() {
50-
input('limit').enter(100);
51-
expect(binding('numbers | limitTo:limit')).toEqual('[1,2,3,4,5,6,7,8,9]');
58+
input('numLimit').enter(100);
59+
input('letterLimit').enter(100);
60+
expect(binding('numbers | limitTo:numLimit')).toEqual('[1,2,3,4,5,6,7,8,9]');
61+
expect(binding('letters | limitTo:letterLimit')).toEqual('abcdefghi');
5262
});
5363
</doc:scenario>
5464
</doc:example>
5565
*/
5666
function limitToFilter(){
57-
return function(array, limit) {
58-
if (!(array instanceof Array)) return array;
67+
return function(input, limit) {
68+
if (!isArray(input) && !isString(input)) return input;
69+
5970
limit = int(limit);
71+
72+
if (isString(input)) {
73+
//NaN check on limit
74+
if (limit) {
75+
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
76+
} else {
77+
return "";
78+
}
79+
}
80+
6081
var out = [],
6182
i, n;
6283

63-
// check that array is iterable
64-
if (!array || !(array instanceof Array))
65-
return out;
66-
6784
// if abs(limit) exceeds maximum length, trim it
68-
if (limit > array.length)
69-
limit = array.length;
70-
else if (limit < -array.length)
71-
limit = -array.length;
85+
if (limit > input.length)
86+
limit = input.length;
87+
else if (limit < -input.length)
88+
limit = -input.length;
7289

7390
if (limit > 0) {
7491
i = 0;
7592
n = limit;
7693
} else {
77-
i = array.length + limit;
78-
n = array.length;
94+
i = input.length + limit;
95+
n = input.length;
7996
}
8097

8198
for (; i<n; i++) {
82-
out.push(array[i]);
99+
out.push(input[i]);
83100
}
84101

85102
return out;

test/ng/filter/limitToSpec.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
describe('Filter: limitTo', function() {
44
var items;
5+
var str
56
var limitTo;
67

78
beforeEach(inject(function($filter) {
89
items = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
10+
str = "tuvwxyz";
911
limitTo = $filter('limitTo');
1012
}));
1113

1214

1315
it('should return the first X items when X is positive', function() {
1416
expect(limitTo(items, 3)).toEqual(['a', 'b', 'c']);
1517
expect(limitTo(items, '3')).toEqual(['a', 'b', 'c']);
18+
expect(limitTo(str, 3)).toEqual("tuv");
19+
expect(limitTo(str, '3')).toEqual("tuv");
1620
});
1721

1822

1923
it('should return the last X items when X is negative', function() {
2024
expect(limitTo(items, -3)).toEqual(['f', 'g', 'h']);
2125
expect(limitTo(items, '-3')).toEqual(['f', 'g', 'h']);
26+
expect(limitTo(str, -3)).toEqual("xyz");
27+
expect(limitTo(str, '-3')).toEqual("xyz");
2228
});
2329

2430

@@ -30,23 +36,36 @@ describe('Filter: limitTo', function() {
3036
expect(limitTo(items, undefined)).toEqual([]);
3137
});
3238

39+
it('should return an empty string when X cannot be parsed', function() {
40+
expect(limitTo(str, 'bogus')).toEqual("");
41+
expect(limitTo(str, 'null')).toEqual("");
42+
expect(limitTo(str, 'undefined')).toEqual("");
43+
expect(limitTo(str, null)).toEqual("");
44+
expect(limitTo(str, undefined)).toEqual("");
45+
});
3346

34-
it('should return an empty array when input is not Array type', function() {
35-
expect(limitTo('bogus', 1)).toEqual('bogus');
36-
expect(limitTo(null, 1)).toEqual(null);
37-
expect(limitTo(undefined, 1)).toEqual(undefined);
47+
48+
it('should return input if not String or Array', function() {
49+
expect(limitTo(1,1)).toEqual(1);
3850
expect(limitTo(null, 1)).toEqual(null);
3951
expect(limitTo(undefined, 1)).toEqual(undefined);
4052
expect(limitTo({}, 1)).toEqual({});
4153
});
4254

4355

4456
it('should return a copy of input array if X is exceeds array length', function () {
45-
expect(limitTo(items, 19)).toEqual(items);
57+
expect(limitTo(items, 9)).toEqual(items);
4658
expect(limitTo(items, '9')).toEqual(items);
4759
expect(limitTo(items, -9)).toEqual(items);
4860
expect(limitTo(items, '-9')).toEqual(items);
4961

5062
expect(limitTo(items, 9)).not.toBe(items);
5163
});
64+
65+
it('should return the entire string if X exceeds input length', function() {
66+
expect(limitTo(str, 9)).toEqual(str);
67+
expect(limitTo(str, '9')).toEqual(str);
68+
expect(limitTo(str, -9)).toEqual(str);
69+
expect(limitTo(str, '-9')).toEqual(str);
70+
})
5271
});

0 commit comments

Comments
 (0)