Skip to content

Commit bec95d8

Browse files
author
Benjamin A. Petersen
committed
Add closure to filter-limitTo.js to prevent helper functions from becoming globals
1 parent 1382447 commit bec95d8

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

filter-limitTo.js

+64-61
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
1-
function isArray(value) {
2-
return toString.call(value) === '[object Array]';
3-
}
4-
function isString(value){return typeof value === 'string';}
5-
6-
function int(str) {
7-
return parseInt(str, 10);
8-
}
9-
10-
/**
11-
* Creates a new array or string containing only a specified
12-
* number of elements. The elements are taken from either
13-
* the beginning or the end of the source array or string,
14-
* as specified by the value and sign (positive or negative) of `limit`
15-
*
16-
* Note: this is a straight-up port from the Angular limitTo filter
17-
* and they deserve full credit for the implementation.
18-
* @param {Array|string} input input Source array or string to be limited.
19-
* @param {string|number} limit The length of the returned array or string. If the `limit` number
20-
* is positive, `limit` number of items from the beginning of the source array/string are copied.
21-
* If the number is negative, `limit` number of items from the end of the source array/string
22-
* are copied. The `limit` will be trimmed if it exceeds `array.length`
23-
* @return {Array|string} A new sub-array or substring of length `limit` or less if input array
24-
* had less than `limit` elements.
25-
*/
26-
PolymerExpressions.prototype.limitTo = function(input, limit) {
27-
28-
if (!isArray(input) && !isString(input)) return input;
29-
30-
limit = int(limit);
31-
32-
if (isString(input)) {
33-
//NaN check on limit
34-
if (limit) {
35-
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
1+
(function() {
2+
3+
function isArray(value) {
4+
return toString.call(value) === '[object Array]';
5+
}
6+
function isString(value){return typeof value === 'string';}
7+
8+
function int(str) {
9+
return parseInt(str, 10);
10+
}
11+
12+
/**
13+
* Creates a new array or string containing only a specified
14+
* number of elements. The elements are taken from either
15+
* the beginning or the end of the source array or string,
16+
* as specified by the value and sign (positive or negative) of `limit`
17+
*
18+
* Note: this is a straight-up port from the Angular limitTo filter
19+
* and they deserve full credit for the implementation.
20+
* @param {Array|string} input input Source array or string to be limited.
21+
* @param {string|number} limit The length of the returned array or string. If the `limit` number
22+
* is positive, `limit` number of items from the beginning of the source array/string are copied.
23+
* If the number is negative, `limit` number of items from the end of the source array/string
24+
* are copied. The `limit` will be trimmed if it exceeds `array.length`
25+
* @return {Array|string} A new sub-array or substring of length `limit` or less if input array
26+
* had less than `limit` elements.
27+
*/
28+
PolymerExpressions.prototype.limitTo = function(input, limit) {
29+
30+
if (!isArray(input) && !isString(input)) return input;
31+
32+
limit = int(limit);
33+
34+
if (isString(input)) {
35+
//NaN check on limit
36+
if (limit) {
37+
return limit >= 0 ? input.slice(0, limit) : input.slice(limit, input.length);
38+
} else {
39+
return "";
40+
}
41+
}
42+
43+
var out = [],
44+
i, n;
45+
46+
// if abs(limit) exceeds maximum length, trim it
47+
if (limit > input.length)
48+
limit = input.length;
49+
else if (limit < -input.length)
50+
limit = -input.length;
51+
52+
if (limit > 0) {
53+
i = 0;
54+
n = limit;
3655
} else {
37-
return "";
56+
i = input.length + limit;
57+
n = input.length;
3858
}
39-
}
40-
41-
var out = [],
42-
i, n;
43-
44-
// if abs(limit) exceeds maximum length, trim it
45-
if (limit > input.length)
46-
limit = input.length;
47-
else if (limit < -input.length)
48-
limit = -input.length;
49-
50-
if (limit > 0) {
51-
i = 0;
52-
n = limit;
53-
} else {
54-
i = input.length + limit;
55-
n = input.length;
56-
}
57-
58-
for (; i<n; i++) {
59-
out.push(input[i]);
60-
}
61-
62-
return out;
63-
};
59+
60+
for (; i<n; i++) {
61+
out.push(input[i]);
62+
}
63+
64+
return out;
65+
};
66+
})();

0 commit comments

Comments
 (0)