-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
126 lines (116 loc) · 3.54 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var predicate = require('commonform-predicate')
var has = require('has')
var withPath = function (result, type, key, path) {
var hasType = has(result, type)
if (hasType && has(result[type], key)) {
result[type][key].push(path)
} else {
result[type][key] = [path]
}
return result
}
var propertyNames = ['definition', 'blank', 'reference', 'use']
var analyze = function recurse (form, result, path) {
return form.content.reduce(function (result, element, index) {
var elementPath
var target
var plural
var heading
if (predicate.text(element)) {
return result
} else {
var name = propertyNames.find(function (name) {
return has(element, name)
})
// Blanks
if (name && name === 'blank') {
result.blanks.push(path.concat('content', index))
return result
// Other Content Elements
} else if (name) {
plural = name + 's'
elementPath = path.concat('content', index)
target = element[name]
return withPath(result, plural, target, elementPath)
// Children
} else if (predicate.child(element)) {
elementPath = path.concat('content', index)
// Heading, if any
if (has(element, 'heading')) {
heading = element.heading
result = withPath(result, 'headings', heading, elementPath)
}
var contentPath = elementPath.concat('form')
return recurse(element.form, result, contentPath)
// Components
} else if (predicate.component(element)) {
elementPath = path.concat('content', index)
// Heading, if any
if (has(element, 'heading')) {
heading = element.heading
result = withPath(result, 'headings', heading, elementPath)
}
// Iterate substitutions, treating them as uses and references.
var substitutions = element.substitutions
Object.keys(substitutions.terms).forEach(function (key) {
var substitute = substitutions.terms[key]
var substitutePath = path.concat(
'content', index, 'substitutions', 'terms', substitute
)
result = withPath(result, 'uses', substitute, substitutePath)
})
Object.keys(substitutions.headings).forEach(function (key) {
var substitute = substitutions.headings[key]
var substitutePath = path.concat(
'content', index, 'substitutions', 'headings', substitute
)
result = withPath(result, 'headings', substitute, substitutePath)
})
result.components.push(
[
{
component: element.component,
version: element.version,
substitutions: clone(element.substitutions)
},
elementPath
]
)
return result
} else {
return result
}
}
}, result)
}
function sortComponents (a, b) {
var keyOrder = ['component', 'version']
for (var index = 0; index < keyOrder.length; index++) {
var key = keyOrder[index]
var comparison = a[0][key].localeCompare(b[0][key])
if (comparison === 0 && index < (keyOrder.length - 1)) {
continue
} else {
return comparison
}
}
}
module.exports = function (form) {
var result = analyze(
form,
{
definitions: {},
uses: {},
headings: {},
references: {},
blanks: [],
components: []
},
[]
)
result.components.sort(sortComponents)
return result
}
function clone (argument) {
return JSON.parse(JSON.stringify(argument))
}