-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
30 lines (27 loc) · 770 Bytes
/
index.js
File metadata and controls
30 lines (27 loc) · 770 Bytes
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
module.exports = function(input) {
if (input == void 0)
throw new Error("Input must be specified.");
var ranges = [], rstart, rend;
input = input.sort(function(a,b){return a - b});
for (var i = 0; i < input.length; i++) {
rstart = input[i];
rend = rstart;
while (input[i + 1] - input[i] == 1) {
rend = input[i + 1];
i++;
}
if (rstart == rend) {
ranges.push(rstart);
} else if (rend - rstart == 1) {
ranges.push(rstart);
ranges.push(rend);
} else {
ranges.push(rstart + "-" + rend);
}
}
return ranges.join(", ");
};
module.exports.middleware = function(request, response, next) {
response.locals.prettyRange = module.exports;
next();
};