index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. var path = require('path');
  3. var inspect = require('util').inspect;
  4. function assertPath(path) {
  5. if (typeof path !== 'string') {
  6. throw new TypeError('Path must be a string. Received ' + inspect(path));
  7. }
  8. }
  9. function posix(path) {
  10. assertPath(path);
  11. if (path.length === 0)
  12. return '.';
  13. var code = path.charCodeAt(0);
  14. var hasRoot = (code === 47/*/*/);
  15. var end = -1;
  16. var matchedSlash = true;
  17. for (var i = path.length - 1; i >= 1; --i) {
  18. code = path.charCodeAt(i);
  19. if (code === 47/*/*/) {
  20. if (!matchedSlash) {
  21. end = i;
  22. break;
  23. }
  24. } else {
  25. // We saw the first non-path separator
  26. matchedSlash = false;
  27. }
  28. }
  29. if (end === -1)
  30. return hasRoot ? '/' : '.';
  31. if (hasRoot && end === 1)
  32. return '//';
  33. return path.slice(0, end);
  34. }
  35. function win32(path) {
  36. assertPath(path);
  37. var len = path.length;
  38. if (len === 0)
  39. return '.';
  40. var rootEnd = -1;
  41. var end = -1;
  42. var matchedSlash = true;
  43. var offset = 0;
  44. var code = path.charCodeAt(0);
  45. // Try to match a root
  46. if (len > 1) {
  47. if (code === 47/*/*/ || code === 92/*\*/) {
  48. // Possible UNC root
  49. rootEnd = offset = 1;
  50. code = path.charCodeAt(1);
  51. if (code === 47/*/*/ || code === 92/*\*/) {
  52. // Matched double path separator at beginning
  53. var j = 2;
  54. var last = j;
  55. // Match 1 or more non-path separators
  56. for (; j < len; ++j) {
  57. code = path.charCodeAt(j);
  58. if (code === 47/*/*/ || code === 92/*\*/)
  59. break;
  60. }
  61. if (j < len && j !== last) {
  62. // Matched!
  63. last = j;
  64. // Match 1 or more path separators
  65. for (; j < len; ++j) {
  66. code = path.charCodeAt(j);
  67. if (code !== 47/*/*/ && code !== 92/*\*/)
  68. break;
  69. }
  70. if (j < len && j !== last) {
  71. // Matched!
  72. last = j;
  73. // Match 1 or more non-path separators
  74. for (; j < len; ++j) {
  75. code = path.charCodeAt(j);
  76. if (code === 47/*/*/ || code === 92/*\*/)
  77. break;
  78. }
  79. if (j === len) {
  80. // We matched a UNC root only
  81. return path;
  82. }
  83. if (j !== last) {
  84. // We matched a UNC root with leftovers
  85. // Offset by 1 to include the separator after the UNC root to
  86. // treat it as a "normal root" on top of a (UNC) root
  87. rootEnd = offset = j + 1;
  88. }
  89. }
  90. }
  91. }
  92. } else if ((code >= 65/*A*/ && code <= 90/*Z*/) ||
  93. (code >= 97/*a*/ && code <= 122/*z*/)) {
  94. // Possible device root
  95. code = path.charCodeAt(1);
  96. if (path.charCodeAt(1) === 58/*:*/) {
  97. rootEnd = offset = 2;
  98. if (len > 2) {
  99. code = path.charCodeAt(2);
  100. if (code === 47/*/*/ || code === 92/*\*/)
  101. rootEnd = offset = 3;
  102. }
  103. }
  104. }
  105. } else if (code === 47/*/*/ || code === 92/*\*/) {
  106. return path[0];
  107. }
  108. for (var i = len - 1; i >= offset; --i) {
  109. code = path.charCodeAt(i);
  110. if (code === 47/*/*/ || code === 92/*\*/) {
  111. if (!matchedSlash) {
  112. end = i;
  113. break;
  114. }
  115. } else {
  116. // We saw the first non-path separator
  117. matchedSlash = false;
  118. }
  119. }
  120. if (end === -1) {
  121. if (rootEnd === -1)
  122. return '.';
  123. else
  124. end = rootEnd;
  125. }
  126. return path.slice(0, end);
  127. }
  128. module.exports = process.platform === 'win32' ? win32 : posix;
  129. module.exports.posix = posix;
  130. module.exports.win32 = win32;