namespace.js 971 B

1234567891011121314151617181920212223242526272829303132
  1. var colorspace = require('colorspace');
  2. /**
  3. * Prefix the messages with a colored namespace.
  4. *
  5. * @param {Array} messages The messages array that is getting written.
  6. * @param {Object} options Options for diagnostics.
  7. * @returns {Array} Altered messages array.
  8. * @public
  9. */
  10. module.exports = function colorNamespace(args, options) {
  11. var namespace = options.namespace;
  12. if (options.colors === false) {
  13. args[0] = namespace +': '+ args[0];
  14. return args;
  15. }
  16. var color = colorspace(namespace);
  17. //
  18. // The console API supports a special %c formatter in browsers. This is used
  19. // to style console messages with any CSS styling, in our case we want to
  20. // use colorize the namespace for clarity. As these are formatters, and
  21. // we need to inject our CSS string as second messages argument so it
  22. // gets picked up correctly.
  23. //
  24. args[0] = '%c'+ namespace +':%c '+ args[0];
  25. args.splice(1, 0, 'color:'+ color, 'color:inherit');
  26. return args;
  27. };