* @param {string} path * @param {string} [suffix] * @returns {string}
(path, suffix)
| 897 | * @returns {string} |
| 898 | */ |
| 899 | basename(path, suffix) { |
| 900 | if (suffix !== undefined) |
| 901 | validateString(suffix, 'suffix'); |
| 902 | validateString(path, 'path'); |
| 903 | let start = 0; |
| 904 | let end = -1; |
| 905 | let matchedSlash = true; |
| 906 | |
| 907 | // Check for a drive letter prefix so as not to mistake the following |
| 908 | // path separator as an extra separator at the end of the path that can be |
| 909 | // disregarded |
| 910 | if (path.length >= 2 && |
| 911 | isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0)) && |
| 912 | StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { |
| 913 | start = 2; |
| 914 | } |
| 915 | |
| 916 | if (suffix !== undefined && suffix.length > 0 && suffix.length <= path.length) { |
| 917 | if (suffix === path) |
| 918 | return ''; |
| 919 | let extIdx = suffix.length - 1; |
| 920 | let firstNonSlashEnd = -1; |
| 921 | for (let i = path.length - 1; i >= start; --i) { |
| 922 | const code = StringPrototypeCharCodeAt(path, i); |
| 923 | if (isPathSeparator(code)) { |
| 924 | // If we reached a path separator that was not part of a set of path |
| 925 | // separators at the end of the string, stop now |
| 926 | if (!matchedSlash) { |
| 927 | start = i + 1; |
| 928 | break; |
| 929 | } |
| 930 | } else { |
| 931 | if (firstNonSlashEnd === -1) { |
| 932 | // We saw the first non-path separator, remember this index in case |
| 933 | // we need it if the extension ends up not matching |
| 934 | matchedSlash = false; |
| 935 | firstNonSlashEnd = i + 1; |
| 936 | } |
| 937 | if (extIdx >= 0) { |
| 938 | // Try to match the explicit extension |
| 939 | if (code === StringPrototypeCharCodeAt(suffix, extIdx)) { |
| 940 | if (--extIdx === -1) { |
| 941 | // We matched the extension, so mark this as the end of our path |
| 942 | // component |
| 943 | end = i; |
| 944 | } |
| 945 | } else { |
| 946 | // Extension does not match, so our result is the entire path |
| 947 | // component |
| 948 | extIdx = -1; |
| 949 | end = firstNonSlashEnd; |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (start === end) |
| 956 | end = firstNonSlashEnd; |
searching dependent graphs…