123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- export const path = {basename, dirname, extname, join, sep: '/'}
- function basename(path, ext) {
- if (ext !== undefined && typeof ext !== 'string') {
- throw new TypeError('"ext" argument must be a string')
- }
- assertPath(path)
- let start = 0
- let end = -1
- let index = path.length
-
- let seenNonSlash
- if (ext === undefined || ext.length === 0 || ext.length > path.length) {
- while (index--) {
- if (path.codePointAt(index) === 47 ) {
-
-
- if (seenNonSlash) {
- start = index + 1
- break
- }
- } else if (end < 0) {
-
-
- seenNonSlash = true
- end = index + 1
- }
- }
- return end < 0 ? '' : path.slice(start, end)
- }
- if (ext === path) {
- return ''
- }
- let firstNonSlashEnd = -1
- let extIndex = ext.length - 1
- while (index--) {
- if (path.codePointAt(index) === 47 ) {
-
-
- if (seenNonSlash) {
- start = index + 1
- break
- }
- } else {
- if (firstNonSlashEnd < 0) {
-
-
- seenNonSlash = true
- firstNonSlashEnd = index + 1
- }
- if (extIndex > -1) {
-
- if (path.codePointAt(index) === ext.codePointAt(extIndex--)) {
- if (extIndex < 0) {
-
-
- end = index
- }
- } else {
-
-
- extIndex = -1
- end = firstNonSlashEnd
- }
- }
- }
- }
- if (start === end) {
- end = firstNonSlashEnd
- } else if (end < 0) {
- end = path.length
- }
- return path.slice(start, end)
- }
- function dirname(path) {
- assertPath(path)
- if (path.length === 0) {
- return '.'
- }
- let end = -1
- let index = path.length
-
- let unmatchedSlash
-
- while (--index) {
- if (path.codePointAt(index) === 47 ) {
- if (unmatchedSlash) {
- end = index
- break
- }
- } else if (!unmatchedSlash) {
-
- unmatchedSlash = true
- }
- }
- return end < 0
- ? path.codePointAt(0) === 47
- ? '/'
- : '.'
- : end === 1 && path.codePointAt(0) === 47
- ? '//'
- : path.slice(0, end)
- }
- function extname(path) {
- assertPath(path)
- let index = path.length
- let end = -1
- let startPart = 0
- let startDot = -1
-
-
- let preDotState = 0
-
- let unmatchedSlash
- while (index--) {
- const code = path.codePointAt(index)
- if (code === 47 ) {
-
-
- if (unmatchedSlash) {
- startPart = index + 1
- break
- }
- continue
- }
- if (end < 0) {
-
-
- unmatchedSlash = true
- end = index + 1
- }
- if (code === 46 ) {
-
- if (startDot < 0) {
- startDot = index
- } else if (preDotState !== 1) {
- preDotState = 1
- }
- } else if (startDot > -1) {
-
-
- preDotState = -1
- }
- }
- if (
- startDot < 0 ||
- end < 0 ||
-
- preDotState === 0 ||
-
- (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)
- ) {
- return ''
- }
- return path.slice(startDot, end)
- }
- function join(...segments) {
- let index = -1
-
- let joined
- while (++index < segments.length) {
- assertPath(segments[index])
- if (segments[index]) {
- joined =
- joined === undefined ? segments[index] : joined + '/' + segments[index]
- }
- }
- return joined === undefined ? '.' : normalize(joined)
- }
- function normalize(path) {
- assertPath(path)
- const absolute = path.codePointAt(0) === 47
-
- let value = normalizeString(path, !absolute)
- if (value.length === 0 && !absolute) {
- value = '.'
- }
- if (value.length > 0 && path.codePointAt(path.length - 1) === 47 ) {
- value += '/'
- }
- return absolute ? '/' + value : value
- }
- function normalizeString(path, allowAboveRoot) {
- let result = ''
- let lastSegmentLength = 0
- let lastSlash = -1
- let dots = 0
- let index = -1
-
- let code
-
- let lastSlashIndex
- while (++index <= path.length) {
- if (index < path.length) {
- code = path.codePointAt(index)
- } else if (code === 47 ) {
- break
- } else {
- code = 47
- }
- if (code === 47 ) {
- if (lastSlash === index - 1 || dots === 1) {
-
- } else if (lastSlash !== index - 1 && dots === 2) {
- if (
- result.length < 2 ||
- lastSegmentLength !== 2 ||
- result.codePointAt(result.length - 1) !== 46 ||
- result.codePointAt(result.length - 2) !== 46
- ) {
- if (result.length > 2) {
- lastSlashIndex = result.lastIndexOf('/')
- if (lastSlashIndex !== result.length - 1) {
- if (lastSlashIndex < 0) {
- result = ''
- lastSegmentLength = 0
- } else {
- result = result.slice(0, lastSlashIndex)
- lastSegmentLength = result.length - 1 - result.lastIndexOf('/')
- }
- lastSlash = index
- dots = 0
- continue
- }
- } else if (result.length > 0) {
- result = ''
- lastSegmentLength = 0
- lastSlash = index
- dots = 0
- continue
- }
- }
- if (allowAboveRoot) {
- result = result.length > 0 ? result + '/..' : '..'
- lastSegmentLength = 2
- }
- } else {
- if (result.length > 0) {
- result += '/' + path.slice(lastSlash + 1, index)
- } else {
- result = path.slice(lastSlash + 1, index)
- }
- lastSegmentLength = index - lastSlash - 1
- }
- lastSlash = index
- dots = 0
- } else if (code === 46 && dots > -1) {
- dots++
- } else {
- dots = -1
- }
- }
- return result
- }
- function assertPath(path) {
- if (typeof path !== 'string') {
- throw new TypeError(
- 'Path must be a string. Received ' + JSON.stringify(path)
- )
- }
- }
|