file-protocol.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * MIT License http://opensource.org/licenses/MIT
  3. * Author: Ben Holloway @bholloway
  4. */
  5. 'use strict';
  6. /**
  7. * Prepend file:// protocol to source path string or source-map sources.
  8. */
  9. function prepend(candidate) {
  10. if (typeof candidate === 'string') {
  11. return 'file://' + candidate;
  12. } else if (candidate && (typeof candidate === 'object') && Array.isArray(candidate.sources)) {
  13. return Object.assign({}, candidate, {
  14. sources: candidate.sources.map(prepend)
  15. });
  16. } else {
  17. throw new Error('expected string|object');
  18. }
  19. }
  20. exports.prepend = prepend;
  21. /**
  22. * Remove file:// protocol from source path string or source-map sources.
  23. */
  24. function remove(candidate) {
  25. if (typeof candidate === 'string') {
  26. return candidate.replace(/^file:\/{2}/, '');
  27. } else if (candidate && (typeof candidate === 'object') && Array.isArray(candidate.sources)) {
  28. return Object.assign({}, candidate, {
  29. sources: candidate.sources.map(remove)
  30. });
  31. } else {
  32. throw new Error('expected string|object');
  33. }
  34. }
  35. exports.remove = remove;