index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. var normalize = require('value-or-function');
  3. var slice = Array.prototype.slice;
  4. function createResolver(config, options) {
  5. // TODO: should the config object be validated?
  6. config = config || {};
  7. options = options || {};
  8. var resolver = {
  9. resolve: resolve,
  10. };
  11. // Keep constants separately
  12. var constants = {};
  13. function resolveConstant(key) {
  14. if (constants.hasOwnProperty(key)) {
  15. return constants[key];
  16. }
  17. var definition = config[key];
  18. // Ignore options that are not defined
  19. if (!definition) {
  20. return;
  21. }
  22. var option = options[key];
  23. if (option != null) {
  24. if (typeof option === 'function') {
  25. return;
  26. }
  27. option = normalize.call(resolver, definition.type, option);
  28. if (option != null) {
  29. constants[key] = option;
  30. return option;
  31. }
  32. }
  33. var fallback = definition.default;
  34. if (option == null && typeof fallback !== 'function') {
  35. constants[key] = fallback;
  36. return fallback;
  37. }
  38. }
  39. // Keep requested keys to detect (and disallow) recursive resolution
  40. var stack = [];
  41. function resolve(key) {
  42. var option = resolveConstant(key);
  43. if (option != null) {
  44. return option;
  45. }
  46. var definition = config[key];
  47. // Ignore options that are not defined
  48. if (!definition) {
  49. return;
  50. }
  51. if (stack.indexOf(key) >= 0) {
  52. throw new Error('Recursive resolution denied.');
  53. }
  54. option = options[key];
  55. var fallback = definition.default;
  56. var appliedArgs = slice.call(arguments, 1);
  57. var args = [definition.type, option].concat(appliedArgs);
  58. function toResolve() {
  59. stack.push(key);
  60. var option = normalize.apply(resolver, args);
  61. if (option == null) {
  62. option = fallback;
  63. if (typeof option === 'function') {
  64. option = option.apply(resolver, appliedArgs);
  65. }
  66. }
  67. return option;
  68. }
  69. function onResolve() {
  70. stack.pop();
  71. }
  72. return tryResolve(toResolve, onResolve);
  73. }
  74. return resolver;
  75. }
  76. function tryResolve(toResolve, onResolve) {
  77. try {
  78. return toResolve();
  79. } finally {
  80. onResolve();
  81. }
  82. }
  83. module.exports = createResolver;