MCPcopy Index your code
hub / github.com/nodejs/node / basename

Function basename

lib/path.js:899–980  ·  view source on GitHub ↗

* @param {string} path * @param {string} [suffix] * @returns {string}

(path, suffix)

Source from the content-addressed store, hash-verified

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;

Callers 15

__init__Method · 0.90
IsFailureOutputMethod · 0.90
IsFailureOutputMethod · 0.90
ProcessContentsMethod · 0.90
_GetStatusFilesMethod · 0.90
#pollMethod · 0.85
getDirentFunction · 0.85
getDirentSyncFunction · 0.85
getExecutedShardOrderFunction · 0.85

Calls 2

isWindowsDeviceRootFunction · 0.85
isPathSeparatorFunction · 0.85

Tested by 3

__init__Method · 0.72
IsFailureOutputMethod · 0.72
IsFailureOutputMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…