123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { injectable } from "inversify";
- import { IOptions } from "./interfaces/IOptions";
- import { IOptionsPreset } from "./interfaces/IOptionsPreset";
- import { DEFAULT_PRESET } from "./preset-options/DefaultPreset";
- @injectable()
- export class Options implements IOptions {
- /**
- * @type {IOptionsPreset}
- */
- private static DISABLED_UNICODE_ARRAY_OPTIONS: IOptionsPreset = {
- encodeUnicodeLiterals: false,
- rotateUnicodeArray: false,
- unicodeArray: false,
- unicodeArrayThreshold: 0,
- wrapUnicodeArrayCalls: false
- };
- /**
- * @type {IOptionsPreset}
- */
- private options: IOptionsPreset;
- /**
- * @type {IOptionsPreset}
- */
- public static SELF_DEFENDING_OPTIONS: IOptionsPreset = {
- compact: true,
- selfDefending: true
- };
- /**
- * @param options
- */
- public assign (options: IOptionsPreset): void {
- if (this.options) {
- throw new Error('Options can\'t be reassigned!');
- }
- this.options = Object.assign({}, DEFAULT_PRESET, options);
- this.normalizeOptions();
- }
- /**
- * @param optionName
- * @returns {any}
- */
- public getOption (optionName: string): any {
- return this.options[optionName];
- }
- /**
- * @returns {IOptionsPreset}
- */
- public getOptions (): IOptionsPreset {
- return this.options;
- }
- private normalizeOptions (): void {
- let normalizedOptions: IOptionsPreset = Object.assign({}, this.options);
- normalizedOptions = Options.unicodeArrayRule(normalizedOptions);
- normalizedOptions = Options.unicodeArrayThresholdRule(normalizedOptions);
- normalizedOptions = Options.selfDefendingRule(normalizedOptions);
- this.options = Object.freeze(normalizedOptions);
- }
- /**
- * @param options
- * @returns {IOptionsPreset}
- */
- private static selfDefendingRule (options: IOptionsPreset): IOptionsPreset {
- if (options['selfDefending']) {
- Object.assign(options, Options.SELF_DEFENDING_OPTIONS);
- }
- return options;
- }
- /**
- * @param options
- * @returns {IOptionsPreset}
- */
- private static unicodeArrayRule (options: IOptionsPreset): IOptionsPreset {
- if (!options['unicodeArray']) {
- Object.assign(options, Options.DISABLED_UNICODE_ARRAY_OPTIONS);
- }
- return options;
- }
- /**
- * @param options
- * @returns {IOptionsPreset}
- */
- private static unicodeArrayThresholdRule (options: IOptionsPreset): IOptionsPreset {
- const minValue: number = 0,
- maxValue: number = 1;
- options['unicodeArrayThreshold'] = Math.min(
- Math.max(
- options['unicodeArrayThreshold'],
- minValue
- ),
- maxValue
- );
- return options;
- }
- }
|