-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
28 lines (24 loc) · 722 Bytes
/
index.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
function composeRange (range) {
return range
.reduce((acc, cur, idx, arr) => {
if (idx === 0 || cur !== arr[idx - 1] + 1) acc.push([])
acc[acc.length - 1].push(cur)
return acc
}, [])
.map((cur) => {
return cur.length > 1 ? `${cur[0]}-${cur[cur.length - 1]}` : `${cur[0]}`
})
}
function parseRange (range) {
const generateRange = (start, end = start) => Array.from({ length: end - start + 1 }, (cur, idx) => idx + start)
return range
.reduce((acc, cur, idx, arr) => {
const r = cur.split('-').map(cur => parseInt(cur))
return acc.concat(generateRange(...r))
}, [])
}
export default parseRange
export {
parseRange as parse,
composeRange as compose
}