40 lines
1.2 KiB
JavaScript
40 lines
1.2 KiB
JavaScript
|
'use strict'
|
||
|
const Option = require('./option')
|
||
|
|
||
|
/**
|
||
|
* A map of { DefinitionNameString: Option }. By default, an Output has an `_unknown` property and any options with defaultValues.
|
||
|
*/
|
||
|
class Output extends Map {
|
||
|
constructor (definitions) {
|
||
|
super()
|
||
|
const Definitions = require('./option-definitions')
|
||
|
/**
|
||
|
* @type {OptionDefinitions}
|
||
|
*/
|
||
|
this.definitions = Definitions.from(definitions)
|
||
|
|
||
|
/* by default, an Output has an `_unknown` property and any options with defaultValues */
|
||
|
this.set('_unknown', Option.create({ name: '_unknown', multiple: true }))
|
||
|
for (const def of this.definitions.whereDefaultValueSet()) {
|
||
|
this.set(def.name, Option.create(def))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
toObject (options) {
|
||
|
const camelCase = require('lodash.camelcase')
|
||
|
options = options || {}
|
||
|
const output = {}
|
||
|
for (const item of this) {
|
||
|
const name = options.camelCase && item[0] !== '_unknown' ? camelCase(item[0]) : item[0]
|
||
|
const option = item[1]
|
||
|
if (name === '_unknown' && !option.get().length) continue
|
||
|
output[name] = option.get()
|
||
|
}
|
||
|
|
||
|
if (options.skipUnknown) delete output._unknown
|
||
|
return output
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Output
|