Options.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { injectable } from "inversify";
  2. import { IOptions } from "./interfaces/IOptions";
  3. import { IOptionsPreset } from "./interfaces/IOptionsPreset";
  4. import { DEFAULT_PRESET } from "./preset-options/DefaultPreset";
  5. @injectable()
  6. export class Options implements IOptions {
  7. /**
  8. * @type {IOptionsPreset}
  9. */
  10. private static DISABLED_UNICODE_ARRAY_OPTIONS: IOptionsPreset = {
  11. encodeUnicodeLiterals: false,
  12. rotateUnicodeArray: false,
  13. unicodeArray: false,
  14. unicodeArrayThreshold: 0,
  15. wrapUnicodeArrayCalls: false
  16. };
  17. /**
  18. * @type {IOptionsPreset}
  19. */
  20. private options: IOptionsPreset;
  21. /**
  22. * @type {IOptionsPreset}
  23. */
  24. public static SELF_DEFENDING_OPTIONS: IOptionsPreset = {
  25. compact: true,
  26. selfDefending: true
  27. };
  28. /**
  29. * @param options
  30. */
  31. public assign (options: IOptionsPreset): void {
  32. if (this.options) {
  33. throw new Error('Options can\'t be reassigned!');
  34. }
  35. this.options = Object.assign({}, DEFAULT_PRESET, options);
  36. this.normalizeOptions();
  37. }
  38. /**
  39. * @param optionName
  40. * @returns {any}
  41. */
  42. public getOption (optionName: string): any {
  43. return this.options[optionName];
  44. }
  45. /**
  46. * @returns {IOptionsPreset}
  47. */
  48. public getOptions (): IOptionsPreset {
  49. return this.options;
  50. }
  51. private normalizeOptions (): void {
  52. let normalizedOptions: IOptionsPreset = Object.assign({}, this.options);
  53. normalizedOptions = Options.unicodeArrayRule(normalizedOptions);
  54. normalizedOptions = Options.unicodeArrayThresholdRule(normalizedOptions);
  55. normalizedOptions = Options.selfDefendingRule(normalizedOptions);
  56. this.options = Object.freeze(normalizedOptions);
  57. }
  58. /**
  59. * @param options
  60. * @returns {IOptionsPreset}
  61. */
  62. private static selfDefendingRule (options: IOptionsPreset): IOptionsPreset {
  63. if (options['selfDefending']) {
  64. Object.assign(options, Options.SELF_DEFENDING_OPTIONS);
  65. }
  66. return options;
  67. }
  68. /**
  69. * @param options
  70. * @returns {IOptionsPreset}
  71. */
  72. private static unicodeArrayRule (options: IOptionsPreset): IOptionsPreset {
  73. if (!options['unicodeArray']) {
  74. Object.assign(options, Options.DISABLED_UNICODE_ARRAY_OPTIONS);
  75. }
  76. return options;
  77. }
  78. /**
  79. * @param options
  80. * @returns {IOptionsPreset}
  81. */
  82. private static unicodeArrayThresholdRule (options: IOptionsPreset): IOptionsPreset {
  83. const minValue: number = 0,
  84. maxValue: number = 1;
  85. options['unicodeArrayThreshold'] = Math.min(
  86. Math.max(
  87. options['unicodeArrayThreshold'],
  88. minValue
  89. ),
  90. maxValue
  91. );
  92. return options;
  93. }
  94. }