import {Key, KeyFallback, Props} from './env';
export interface Enabled {
enabled(key: string): boolean;
}
export class PropEnabled implements Enabled {
readonly acceptValues: Set<string>;
readonly key: KeyFallback;
constructor(key: Key, readonly props: Props, ...acceptValues: string[]) {
this.acceptValues = new Set<string>(acceptValues);
this.key = typeof key === 'string' ? { name: key } : { ...key };
}
enabled(key?: Key, or?: () => string): boolean {
const val = this.props.getProp(key || this.key, or);
return val ? this.acceptValues.has(val) : false;
}
extend(prefix: string, ...acceptValues: string[]): PropEnabled {
const props = this.props.getConfig(prefix);
const accept: string[] = [...this.acceptValues, ...acceptValues];
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return new ChildEnabled(
{
...this.key,
name: prefix,
},
this,
props,
...accept,
);
}
}
export class ChildEnabled extends PropEnabled {
constructor(key: Key, readonly parent: PropEnabled, props: Props, ...acceptValues: string[]) {
super(key, props, ...acceptValues);
}
enabled(key: string, or?: () => string): boolean {
return super.enabled(key) || this.parent.enabled(key, or);
}
}
|