(path: string, ext?: string)
| 764 | }, |
| 765 | |
| 766 | basename(path: string, ext?: string): string { |
| 767 | if (ext !== undefined) { |
| 768 | validateString(ext, 'ext'); |
| 769 | } |
| 770 | validateString(path, 'path'); |
| 771 | let start = 0; |
| 772 | let end = -1; |
| 773 | let matchedSlash = true; |
| 774 | let i; |
| 775 | |
| 776 | // Check for a drive letter prefix so as not to mistake the following |
| 777 | // path separator as an extra separator at the end of the path that can be |
| 778 | // disregarded |
| 779 | if (path.length >= 2 && isWindowsDeviceRoot(path.charCodeAt(0)) && path.charCodeAt(1) === CHAR_COLON) { |
| 780 | start = 2; |
| 781 | } |
| 782 | |
| 783 | if (ext !== undefined && ext.length > 0 && ext.length <= path.length) { |
| 784 | if (ext === path) { |
| 785 | return ''; |
| 786 | } |
| 787 | let extIdx = ext.length - 1; |
| 788 | let firstNonSlashEnd = -1; |
| 789 | for (i = path.length - 1; i >= start; --i) { |
| 790 | const code = path.charCodeAt(i); |
| 791 | if (isPathSeparator(code)) { |
| 792 | // If we reached a path separator that was not part of a set of path |
| 793 | // separators at the end of the string, stop now |
| 794 | if (!matchedSlash) { |
| 795 | start = i + 1; |
| 796 | break; |
| 797 | } |
| 798 | } else { |
| 799 | if (firstNonSlashEnd === -1) { |
| 800 | // We saw the first non-path separator, remember this index in case |
| 801 | // we need it if the extension ends up not matching |
| 802 | matchedSlash = false; |
| 803 | firstNonSlashEnd = i + 1; |
| 804 | } |
| 805 | if (extIdx >= 0) { |
| 806 | // Try to match the explicit extension |
| 807 | if (code === ext.charCodeAt(extIdx)) { |
| 808 | if (--extIdx === -1) { |
| 809 | // We matched the extension, so mark this as the end of our path |
| 810 | // component |
| 811 | end = i; |
| 812 | } |
| 813 | } else { |
| 814 | // Extension does not match, so our result is the entire path |
| 815 | // component |
| 816 | extIdx = -1; |
| 817 | end = firstNonSlashEnd; |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (start === end) { |
no test coverage detected