-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (33 loc) · 1.02 KB
/
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
29
30
31
32
33
34
35
var predicate = require('commonform-predicate')
module.exports = function commonformGroupSeries (form) {
return form.content
.reduce(function (groups, element, index, content) {
if (isChild(element)) {
// `element` is part of the previous series.
if (index > 0 && isChild(content[index - 1])) {
groups[groups.length - 1].content.push(element)
// `element` starts a new series.
} else {
groups.push({
type: 'series',
content: [element]
})
}
} else {
// `element` is part of the previous paragraph.
if (index > 0 && !isChild(content[index - 1])) {
groups[groups.length - 1].content.push(element)
// `element` starts a new paragraph.
} else {
groups.push({
type: 'paragraph',
content: [element]
})
}
}
return groups
}, [])
}
function isChild (argument) {
return predicate.child(argument) || predicate.component(argument)
}