Skip to content

Commit accfc71

Browse files
committed
Add limitFrom filter.
1 parent 0897261 commit accfc71

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

filter-limitFrom.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Limit displayed items from an array from an offset
3+
*
4+
* @param {array} input
5+
* @param {integer} offset
6+
* @param {integer} limit
7+
* @return {array}
8+
*/
9+
PolymerExpressions.prototype.limitFrom = function (input, offset, limit) {
10+
if (!(input instanceof Array) && !(input instanceof String)) return input;
11+
12+
limit = parseInt(limit, 10);
13+
14+
if (input instanceof String) {
15+
if (limit) {
16+
return limit >= 0 ? input.slice(offset, limit) : input.slice(limit, input.length);
17+
} else {
18+
return "";
19+
}
20+
}
21+
22+
var out = [],
23+
i, n;
24+
25+
if (limit > input.length)
26+
limit = input.length;
27+
else if (limit < -input.length)
28+
limit = -input.length;
29+
30+
if (limit > 0) {
31+
i = offset;
32+
n = limit;
33+
} else {
34+
i = input.length + limit;
35+
n = input.length;
36+
}
37+
38+
for (; i < n; i++) {
39+
out.push(input[i]);
40+
}
41+
42+
return out;
43+
};

0 commit comments

Comments
 (0)