minpath.browser.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. // A derivative work based on:
  2. // <https://github.com/browserify/path-browserify>.
  3. // Which is licensed:
  4. //
  5. // MIT License
  6. //
  7. // Copyright (c) 2013 James Halliday
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. // this software and associated documentation files (the "Software"), to deal in
  11. // the Software without restriction, including without limitation the rights to
  12. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  13. // the Software, and to permit persons to whom the Software is furnished to do so,
  14. // subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in all
  17. // copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  21. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  22. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  23. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. // A derivative work based on:
  26. //
  27. // Parts of that are extracted from Node’s internal `path` module:
  28. // <https://github.com/nodejs/node/blob/master/lib/path.js>.
  29. // Which is licensed:
  30. //
  31. // Copyright Joyent, Inc. and other Node contributors.
  32. //
  33. // Permission is hereby granted, free of charge, to any person obtaining a
  34. // copy of this software and associated documentation files (the
  35. // "Software"), to deal in the Software without restriction, including
  36. // without limitation the rights to use, copy, modify, merge, publish,
  37. // distribute, sublicense, and/or sell copies of the Software, and to permit
  38. // persons to whom the Software is furnished to do so, subject to the
  39. // following conditions:
  40. //
  41. // The above copyright notice and this permission notice shall be included
  42. // in all copies or substantial portions of the Software.
  43. //
  44. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  45. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  46. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  47. // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  48. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  49. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  50. // USE OR OTHER DEALINGS IN THE SOFTWARE.
  51. export const path = {basename, dirname, extname, join, sep: '/'}
  52. /* eslint-disable max-depth, complexity */
  53. /**
  54. * Get the basename from a path.
  55. *
  56. * @param {string} path
  57. * File path.
  58. * @param {string | null | undefined} [ext]
  59. * Extension to strip.
  60. * @returns {string}
  61. * Stem or basename.
  62. */
  63. function basename(path, ext) {
  64. if (ext !== undefined && typeof ext !== 'string') {
  65. throw new TypeError('"ext" argument must be a string')
  66. }
  67. assertPath(path)
  68. let start = 0
  69. let end = -1
  70. let index = path.length
  71. /** @type {boolean | undefined} */
  72. let seenNonSlash
  73. if (ext === undefined || ext.length === 0 || ext.length > path.length) {
  74. while (index--) {
  75. if (path.codePointAt(index) === 47 /* `/` */) {
  76. // If we reached a path separator that was not part of a set of path
  77. // separators at the end of the string, stop now.
  78. if (seenNonSlash) {
  79. start = index + 1
  80. break
  81. }
  82. } else if (end < 0) {
  83. // We saw the first non-path separator, mark this as the end of our
  84. // path component.
  85. seenNonSlash = true
  86. end = index + 1
  87. }
  88. }
  89. return end < 0 ? '' : path.slice(start, end)
  90. }
  91. if (ext === path) {
  92. return ''
  93. }
  94. let firstNonSlashEnd = -1
  95. let extIndex = ext.length - 1
  96. while (index--) {
  97. if (path.codePointAt(index) === 47 /* `/` */) {
  98. // If we reached a path separator that was not part of a set of path
  99. // separators at the end of the string, stop now.
  100. if (seenNonSlash) {
  101. start = index + 1
  102. break
  103. }
  104. } else {
  105. if (firstNonSlashEnd < 0) {
  106. // We saw the first non-path separator, remember this index in case
  107. // we need it if the extension ends up not matching.
  108. seenNonSlash = true
  109. firstNonSlashEnd = index + 1
  110. }
  111. if (extIndex > -1) {
  112. // Try to match the explicit extension.
  113. if (path.codePointAt(index) === ext.codePointAt(extIndex--)) {
  114. if (extIndex < 0) {
  115. // We matched the extension, so mark this as the end of our path
  116. // component
  117. end = index
  118. }
  119. } else {
  120. // Extension does not match, so our result is the entire path
  121. // component
  122. extIndex = -1
  123. end = firstNonSlashEnd
  124. }
  125. }
  126. }
  127. }
  128. if (start === end) {
  129. end = firstNonSlashEnd
  130. } else if (end < 0) {
  131. end = path.length
  132. }
  133. return path.slice(start, end)
  134. }
  135. /**
  136. * Get the dirname from a path.
  137. *
  138. * @param {string} path
  139. * File path.
  140. * @returns {string}
  141. * File path.
  142. */
  143. function dirname(path) {
  144. assertPath(path)
  145. if (path.length === 0) {
  146. return '.'
  147. }
  148. let end = -1
  149. let index = path.length
  150. /** @type {boolean | undefined} */
  151. let unmatchedSlash
  152. // Prefix `--` is important to not run on `0`.
  153. while (--index) {
  154. if (path.codePointAt(index) === 47 /* `/` */) {
  155. if (unmatchedSlash) {
  156. end = index
  157. break
  158. }
  159. } else if (!unmatchedSlash) {
  160. // We saw the first non-path separator
  161. unmatchedSlash = true
  162. }
  163. }
  164. return end < 0
  165. ? path.codePointAt(0) === 47 /* `/` */
  166. ? '/'
  167. : '.'
  168. : end === 1 && path.codePointAt(0) === 47 /* `/` */
  169. ? '//'
  170. : path.slice(0, end)
  171. }
  172. /**
  173. * Get an extname from a path.
  174. *
  175. * @param {string} path
  176. * File path.
  177. * @returns {string}
  178. * Extname.
  179. */
  180. function extname(path) {
  181. assertPath(path)
  182. let index = path.length
  183. let end = -1
  184. let startPart = 0
  185. let startDot = -1
  186. // Track the state of characters (if any) we see before our first dot and
  187. // after any path separator we find.
  188. let preDotState = 0
  189. /** @type {boolean | undefined} */
  190. let unmatchedSlash
  191. while (index--) {
  192. const code = path.codePointAt(index)
  193. if (code === 47 /* `/` */) {
  194. // If we reached a path separator that was not part of a set of path
  195. // separators at the end of the string, stop now.
  196. if (unmatchedSlash) {
  197. startPart = index + 1
  198. break
  199. }
  200. continue
  201. }
  202. if (end < 0) {
  203. // We saw the first non-path separator, mark this as the end of our
  204. // extension.
  205. unmatchedSlash = true
  206. end = index + 1
  207. }
  208. if (code === 46 /* `.` */) {
  209. // If this is our first dot, mark it as the start of our extension.
  210. if (startDot < 0) {
  211. startDot = index
  212. } else if (preDotState !== 1) {
  213. preDotState = 1
  214. }
  215. } else if (startDot > -1) {
  216. // We saw a non-dot and non-path separator before our dot, so we should
  217. // have a good chance at having a non-empty extension.
  218. preDotState = -1
  219. }
  220. }
  221. if (
  222. startDot < 0 ||
  223. end < 0 ||
  224. // We saw a non-dot character immediately before the dot.
  225. preDotState === 0 ||
  226. // The (right-most) trimmed path component is exactly `..`.
  227. (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
  228. ) {
  229. return ''
  230. }
  231. return path.slice(startDot, end)
  232. }
  233. /**
  234. * Join segments from a path.
  235. *
  236. * @param {Array<string>} segments
  237. * Path segments.
  238. * @returns {string}
  239. * File path.
  240. */
  241. function join(...segments) {
  242. let index = -1
  243. /** @type {string | undefined} */
  244. let joined
  245. while (++index < segments.length) {
  246. assertPath(segments[index])
  247. if (segments[index]) {
  248. joined =
  249. joined === undefined ? segments[index] : joined + '/' + segments[index]
  250. }
  251. }
  252. return joined === undefined ? '.' : normalize(joined)
  253. }
  254. /**
  255. * Normalize a basic file path.
  256. *
  257. * @param {string} path
  258. * File path.
  259. * @returns {string}
  260. * File path.
  261. */
  262. // Note: `normalize` is not exposed as `path.normalize`, so some code is
  263. // manually removed from it.
  264. function normalize(path) {
  265. assertPath(path)
  266. const absolute = path.codePointAt(0) === 47 /* `/` */
  267. // Normalize the path according to POSIX rules.
  268. let value = normalizeString(path, !absolute)
  269. if (value.length === 0 && !absolute) {
  270. value = '.'
  271. }
  272. if (value.length > 0 && path.codePointAt(path.length - 1) === 47 /* / */) {
  273. value += '/'
  274. }
  275. return absolute ? '/' + value : value
  276. }
  277. /**
  278. * Resolve `.` and `..` elements in a path with directory names.
  279. *
  280. * @param {string} path
  281. * File path.
  282. * @param {boolean} allowAboveRoot
  283. * Whether `..` can move above root.
  284. * @returns {string}
  285. * File path.
  286. */
  287. function normalizeString(path, allowAboveRoot) {
  288. let result = ''
  289. let lastSegmentLength = 0
  290. let lastSlash = -1
  291. let dots = 0
  292. let index = -1
  293. /** @type {number | undefined} */
  294. let code
  295. /** @type {number} */
  296. let lastSlashIndex
  297. while (++index <= path.length) {
  298. if (index < path.length) {
  299. code = path.codePointAt(index)
  300. } else if (code === 47 /* `/` */) {
  301. break
  302. } else {
  303. code = 47 /* `/` */
  304. }
  305. if (code === 47 /* `/` */) {
  306. if (lastSlash === index - 1 || dots === 1) {
  307. // Empty.
  308. } else if (lastSlash !== index - 1 && dots === 2) {
  309. if (
  310. result.length < 2 ||
  311. lastSegmentLength !== 2 ||
  312. result.codePointAt(result.length - 1) !== 46 /* `.` */ ||
  313. result.codePointAt(result.length - 2) !== 46 /* `.` */
  314. ) {
  315. if (result.length > 2) {
  316. lastSlashIndex = result.lastIndexOf('/')
  317. if (lastSlashIndex !== result.length - 1) {
  318. if (lastSlashIndex < 0) {
  319. result = ''
  320. lastSegmentLength = 0
  321. } else {
  322. result = result.slice(0, lastSlashIndex)
  323. lastSegmentLength = result.length - 1 - result.lastIndexOf('/')
  324. }
  325. lastSlash = index
  326. dots = 0
  327. continue
  328. }
  329. } else if (result.length > 0) {
  330. result = ''
  331. lastSegmentLength = 0
  332. lastSlash = index
  333. dots = 0
  334. continue
  335. }
  336. }
  337. if (allowAboveRoot) {
  338. result = result.length > 0 ? result + '/..' : '..'
  339. lastSegmentLength = 2
  340. }
  341. } else {
  342. if (result.length > 0) {
  343. result += '/' + path.slice(lastSlash + 1, index)
  344. } else {
  345. result = path.slice(lastSlash + 1, index)
  346. }
  347. lastSegmentLength = index - lastSlash - 1
  348. }
  349. lastSlash = index
  350. dots = 0
  351. } else if (code === 46 /* `.` */ && dots > -1) {
  352. dots++
  353. } else {
  354. dots = -1
  355. }
  356. }
  357. return result
  358. }
  359. /**
  360. * Make sure `path` is a string.
  361. *
  362. * @param {string} path
  363. * File path.
  364. * @returns {asserts path is string}
  365. * Nothing.
  366. */
  367. function assertPath(path) {
  368. if (typeof path !== 'string') {
  369. throw new TypeError(
  370. 'Path must be a string. Received ' + JSON.stringify(path)
  371. )
  372. }
  373. }
  374. /* eslint-enable max-depth, complexity */