Ingen beskrivning

index.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var isWindows = process.platform === 'win32';
  3. // Regex to split a windows path into three parts: [*, device, slash,
  4. // tail] windows-only
  5. var splitDeviceRe =
  6. /^([a-zA-Z]:|[\\\/]{2}[^\\\/]+[\\\/]+[^\\\/]+)?([\\\/])?([\s\S]*?)$/;
  7. // Regex to split the tail part of the above into [*, dir, basename, ext]
  8. var splitTailRe =
  9. /^([\s\S]*?)((?:\.{1,2}|[^\\\/]+?|)(\.[^.\/\\]*|))(?:[\\\/]*)$/;
  10. var win32 = {};
  11. // Function to split a filename into [root, dir, basename, ext]
  12. function win32SplitPath(filename) {
  13. // Separate device+slash from tail
  14. var result = splitDeviceRe.exec(filename),
  15. device = (result[1] || '') + (result[2] || ''),
  16. tail = result[3] || '';
  17. // Split the tail into dir, basename and extension
  18. var result2 = splitTailRe.exec(tail),
  19. dir = result2[1],
  20. basename = result2[2],
  21. ext = result2[3];
  22. return [device, dir, basename, ext];
  23. }
  24. win32.parse = function(pathString) {
  25. if (typeof pathString !== 'string') {
  26. throw new TypeError(
  27. "Parameter 'pathString' must be a string, not " + typeof pathString
  28. );
  29. }
  30. var allParts = win32SplitPath(pathString);
  31. if (!allParts || allParts.length !== 4) {
  32. throw new TypeError("Invalid path '" + pathString + "'");
  33. }
  34. return {
  35. root: allParts[0],
  36. dir: allParts[0] + allParts[1].slice(0, -1),
  37. base: allParts[2],
  38. ext: allParts[3],
  39. name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
  40. };
  41. };
  42. // Split a filename into [root, dir, basename, ext], unix version
  43. // 'root' is just a slash, or nothing.
  44. var splitPathRe =
  45. /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;
  46. var posix = {};
  47. function posixSplitPath(filename) {
  48. return splitPathRe.exec(filename).slice(1);
  49. }
  50. posix.parse = function(pathString) {
  51. if (typeof pathString !== 'string') {
  52. throw new TypeError(
  53. "Parameter 'pathString' must be a string, not " + typeof pathString
  54. );
  55. }
  56. var allParts = posixSplitPath(pathString);
  57. if (!allParts || allParts.length !== 4) {
  58. throw new TypeError("Invalid path '" + pathString + "'");
  59. }
  60. allParts[1] = allParts[1] || '';
  61. allParts[2] = allParts[2] || '';
  62. allParts[3] = allParts[3] || '';
  63. return {
  64. root: allParts[0],
  65. dir: allParts[0] + allParts[1].slice(0, -1),
  66. base: allParts[2],
  67. ext: allParts[3],
  68. name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
  69. };
  70. };
  71. if (isWindows)
  72. module.exports = win32.parse;
  73. else /* posix */
  74. module.exports = posix.parse;
  75. module.exports.posix = posix.parse;
  76. module.exports.win32 = win32.parse;