a zip code crypto-currency system good for red ONLY

OptionsDefaulter.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. function getProperty(obj, name) {
  7. name = name.split(".");
  8. for(var i = 0; i < name.length - 1; i++) {
  9. obj = obj[name[i]];
  10. if(typeof obj !== "object" || !obj) return;
  11. }
  12. return obj[name.pop()];
  13. }
  14. function setProperty(obj, name, value) {
  15. name = name.split(".");
  16. for(var i = 0; i < name.length - 1; i++) {
  17. if(typeof obj[name[i]] !== "object" && typeof obj[name[i]] !== "undefined") return;
  18. if(!obj[name[i]]) obj[name[i]] = {};
  19. obj = obj[name[i]];
  20. }
  21. obj[name.pop()] = value;
  22. }
  23. class OptionsDefaulter {
  24. constructor() {
  25. this.defaults = {};
  26. this.config = {};
  27. }
  28. process(options) {
  29. // TODO: change this for webpack 4: options = Object.assign({}, options);
  30. for(let name in this.defaults) {
  31. switch(this.config[name]) {
  32. case undefined:
  33. if(getProperty(options, name) === undefined)
  34. setProperty(options, name, this.defaults[name]);
  35. break;
  36. case "call":
  37. setProperty(options, name, this.defaults[name].call(this, getProperty(options, name), options), options);
  38. break;
  39. case "make":
  40. if(getProperty(options, name) === undefined)
  41. setProperty(options, name, this.defaults[name].call(this, options), options);
  42. break;
  43. case "append":
  44. {
  45. let oldValue = getProperty(options, name);
  46. if(!Array.isArray(oldValue)) oldValue = [];
  47. oldValue.push.apply(oldValue, this.defaults[name]);
  48. setProperty(options, name, oldValue);
  49. break;
  50. }
  51. default:
  52. throw new Error("OptionsDefaulter cannot process " + this.config[name]);
  53. }
  54. }
  55. // TODO: change this for webpack 4: return options;
  56. }
  57. set(name, config, def) {
  58. if(arguments.length === 3) {
  59. this.defaults[name] = def;
  60. this.config[name] = config;
  61. } else {
  62. this.defaults[name] = config;
  63. delete this.config[name];
  64. }
  65. }
  66. }
  67. module.exports = OptionsDefaulter;