@fontoxml/fontoxml-development-tools
Version:
Development tools for Fonto.
94 lines (76 loc) • 2.02 kB
JavaScript
import Option from './Option.js';
import symbols from './symbols.js';
const breakPartsOnPart = Symbol();
const breakPartsDefaultPattern = /^[-.*]/;
export default class MultiOption extends Option {
constructor(name) {
super(name);
this.isInfinite();
}
// @todo: infinite arg as a callback
// @amazement 4 years later: what the actual did i mean?
isInfinite(infinite) {
this[breakPartsOnPart] = infinite
? (_part) => false
: (part) => part.charAt(0) === '-';
return this;
}
[breakPartsOnPart](part) {
return part.match(breakPartsDefaultPattern);
}
[symbols.spliceInputFromParts](parts, _tiers) {
if (this.short && parts[0].charAt(1) === this.short) {
parts[0] = `-${parts[0].substr(2)}`;
if (parts[0] !== '-') {
return [];
}
}
parts.shift();
const input = [];
do {
if (parts[0] === '-') {
parts.shift();
break;
}
if (!parts[0] || this[breakPartsOnPart](parts[0])) {
break;
}
input.push(parts.shift());
} while (parts.length > 0);
return input;
}
[symbols.applyDefault](value, isUndefined) {
if (this.required && (isUndefined || value === undefined)) {
// Option is required but not specified. Return undefined so it is reported as missing.
return undefined;
}
if (value === undefined || isUndefined) {
if (this.useDefaultIfFlagMissing) {
// The option was not specified, but set to use the default value if missing.
value = this.cloneDefault() || [];
}
} else if (!value.length) {
// The option was specified without value(s), use the default value.
value = this.cloneDefault() || [];
}
return value;
}
[symbols.createContributionToRequestObject](
accumulated,
value,
_isUndefined
) {
const accumulatedOption =
accumulated &&
accumulated.options &&
accumulated.options[this.name];
return {
options: {
[this.name]:
accumulatedOption !== undefined || value !== undefined
? (accumulatedOption || []).concat(value)
: undefined,
},
};
}
}