index.js 937 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use strict';
  2. var env = require('env-variable');
  3. /**
  4. * Checks if a given namespace is allowed by the environment variables.
  5. *
  6. * @param {String} name namespace that should be included.
  7. * @param {Array} variables
  8. * @returns {Boolean}
  9. * @api public
  10. */
  11. module.exports = function enabled(name, variables) {
  12. var envy = env()
  13. , variable
  14. , i = 0;
  15. variables = variables || ['diagnostics', 'debug'];
  16. for (; i < variables.length; i++) {
  17. if ((variable = envy[variables[i]])) break;
  18. }
  19. if (!variable) return false;
  20. variables = variable.split(/[\s,]+/);
  21. i = 0;
  22. for (; i < variables.length; i++) {
  23. variable = variables[i].replace('*', '.*?');
  24. if ('-' === variable.charAt(0)) {
  25. if ((new RegExp('^'+ variable.substr(1) +'$')).test(name)) {
  26. return false;
  27. }
  28. continue;
  29. }
  30. if ((new RegExp('^'+ variable +'$')).test(name)) {
  31. return true;
  32. }
  33. }
  34. return false;
  35. };