index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. /*!
  2. * normalize-path <https://github.com/jonschlinkert/normalize-path>
  3. *
  4. * Copyright (c) 2014-2018, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. module.exports = function(path, stripTrailing) {
  8. if (typeof path !== 'string') {
  9. throw new TypeError('expected path to be a string');
  10. }
  11. if (path === '\\' || path === '/') return '/';
  12. var len = path.length;
  13. if (len <= 1) return path;
  14. // ensure that win32 namespaces has two leading slashes, so that the path is
  15. // handled properly by the win32 version of path.parse() after being normalized
  16. // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
  17. var prefix = '';
  18. if (len > 4 && path[3] === '\\') {
  19. var ch = path[2];
  20. if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
  21. path = path.slice(2);
  22. prefix = '//';
  23. }
  24. }
  25. var segs = path.split(/[/\\]+/);
  26. if (stripTrailing !== false && segs[segs.length - 1] === '') {
  27. segs.pop();
  28. }
  29. return prefix + segs.join('/');
  30. };