-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfilter-limitFrom.js
43 lines (36 loc) · 956 Bytes
/
filter-limitFrom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Limit displayed items from an array at an offset by a limit
*
* @param {array} input
* @param {integer} offset
* @param {integer} limit
* @return {array}
*/
PolymerExpressions.prototype.limitFrom = function (input, offset, limit) {
if (!(input instanceof Array) && !(input instanceof String)) return input;
limit = parseInt(limit, 10);
if (input instanceof String) {
if (limit) {
return limit >= 0 ? input.slice(offset, limit) : input.slice(limit, input.length);
} else {
return "";
}
}
var out = [],
i, n;
if (limit > input.length)
limit = input.length;
else if (limit < -input.length)
limit = -input.length;
if (limit > 0) {
i = offset;
n = limit;
} else {
i = input.length + limit;
n = input.length;
}
for (; i < n; i++) {
out.push(input[i]);
}
return out;
};