UNPKG

1 kBJavaScriptView Raw
1'use strict'
2const arrayify = require('array-back')
3const Output = require('./output')
4
5class GroupedOutput extends Output {
6 toObject () {
7 const superOutput = super.toObject()
8 delete superOutput._unknown
9 const grouped = {
10 _all: superOutput
11 }
12 if (this.unknown.length) grouped._unknown = this.unknown
13
14 this.definitions.whereGrouped().forEach(def => {
15 const outputValue = this.output[def.name]
16 for (const groupName of arrayify(def.group)) {
17 grouped[groupName] = grouped[groupName] || {}
18 if (outputValue && outputValue.isDefined()) {
19 grouped[groupName][def.name] = outputValue.value
20 }
21 }
22 })
23
24 this.definitions.whereNotGrouped().forEach(def => {
25 const outputValue = this.output[def.name]
26 if (outputValue && outputValue.isDefined()) {
27 if (!grouped._none) grouped._none = {}
28 grouped._none[def.name] = outputValue.value
29 }
30 })
31 return grouped
32 }
33}
34
35module.exports = GroupedOutput