You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.7 KiB

4 years ago
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /**
  7. * Gets the value at path of object
  8. * @param {object} obj object to query
  9. * @param {string} path query path
  10. * @returns {any} - if {@param path} requests element from array, then `undefined` will be returned
  11. */
  12. const getProperty = (obj, path) => {
  13. let name = path.split(".");
  14. for (let i = 0; i < name.length - 1; i++) {
  15. obj = obj[name[i]];
  16. if (typeof obj !== "object" || !obj || Array.isArray(obj)) return;
  17. }
  18. return obj[name.pop()];
  19. };
  20. /**
  21. * Sets the value at path of object. Stops execution, if {@param path} requests element from array to be set
  22. * @param {object} obj object to query
  23. * @param {string} path query path
  24. * @param {any} value value to be set
  25. * @returns {void}
  26. */
  27. const setProperty = (obj, path, value) => {
  28. let name = path.split(".");
  29. for (let i = 0; i < name.length - 1; i++) {
  30. if (typeof obj[name[i]] !== "object" && obj[name[i]] !== undefined) return;
  31. if (Array.isArray(obj[name[i]])) return;
  32. if (!obj[name[i]]) obj[name[i]] = {};
  33. obj = obj[name[i]];
  34. }
  35. obj[name.pop()] = value;
  36. };
  37. /**
  38. * @typedef {'call' | 'make' | 'append'} ConfigType
  39. */
  40. /**
  41. * @typedef {(options: object) => any} MakeConfigHandler
  42. */
  43. /**
  44. * @typedef {(value: any, options: object) => any} CallConfigHandler
  45. */
  46. /**
  47. * @typedef {any[]} AppendConfigValues
  48. */
  49. class OptionsDefaulter {
  50. constructor() {
  51. /**
  52. * Stores default options settings or functions for computing them
  53. */
  54. this.defaults = {};
  55. /**
  56. * Stores configuration for options
  57. * @type {{[key: string]: ConfigType}}
  58. */
  59. this.config = {};
  60. }
  61. /**
  62. * Enhancing {@param options} with default values
  63. * @param {object} options provided options
  64. * @returns {object} - enhanced options
  65. * @throws {Error} - will throw error, if configuration value is other then `undefined` or {@link ConfigType}
  66. */
  67. process(options) {
  68. options = Object.assign({}, options);
  69. for (let name in this.defaults) {
  70. switch (this.config[name]) {
  71. /**
  72. * If {@link ConfigType} doesn't specified and current value is `undefined`, then default value will be assigned
  73. */
  74. case undefined:
  75. if (getProperty(options, name) === undefined) {
  76. setProperty(options, name, this.defaults[name]);
  77. }
  78. break;
  79. /**
  80. * Assign result of {@link CallConfigHandler}
  81. */
  82. case "call":
  83. setProperty(
  84. options,
  85. name,
  86. this.defaults[name].call(this, getProperty(options, name), options)
  87. );
  88. break;
  89. /**
  90. * Assign result of {@link MakeConfigHandler}, if current value is `undefined`
  91. */
  92. case "make":
  93. if (getProperty(options, name) === undefined) {
  94. setProperty(options, name, this.defaults[name].call(this, options));
  95. }
  96. break;
  97. /**
  98. * Adding {@link AppendConfigValues} at the end of the current array
  99. */
  100. case "append": {
  101. let oldValue = getProperty(options, name);
  102. if (!Array.isArray(oldValue)) {
  103. oldValue = [];
  104. }
  105. oldValue.push(...this.defaults[name]);
  106. setProperty(options, name, oldValue);
  107. break;
  108. }
  109. default:
  110. throw new Error(
  111. "OptionsDefaulter cannot process " + this.config[name]
  112. );
  113. }
  114. }
  115. return options;
  116. }
  117. /**
  118. * Builds up default values
  119. * @param {string} name option path
  120. * @param {ConfigType | any} config if {@param def} is provided, then only {@link ConfigType} is allowed
  121. * @param {MakeConfigHandler | CallConfigHandler | AppendConfigValues} [def] defaults
  122. * @returns {void}
  123. */
  124. set(name, config, def) {
  125. if (def !== undefined) {
  126. this.defaults[name] = def;
  127. this.config[name] = config;
  128. } else {
  129. this.defaults[name] = config;
  130. delete this.config[name];
  131. }
  132. }
  133. }
  134. module.exports = OptionsDefaulter;