(path: string, ext: string)
| 351 | }; |
| 352 | |
| 353 | const basename = function basename(path: string, ext: string) { |
| 354 | if (ext !== undefined && typeof ext !== 'string') { |
| 355 | throw new TypeError('"ext" argument must be a string'); |
| 356 | } |
| 357 | assertPath(path); |
| 358 | |
| 359 | let start = 0; |
| 360 | let end = -1; |
| 361 | let matchedSlash = true; |
| 362 | let i; |
| 363 | |
| 364 | if (ext !== undefined && ext.length > 0 && ext.length <= path.length) { |
| 365 | if (ext.length === path.length && ext === path) { |
| 366 | return ''; |
| 367 | } |
| 368 | let extIdx = ext.length - 1; |
| 369 | let firstNonSlashEnd = -1; |
| 370 | for (i = path.length - 1; i >= 0; --i) { |
| 371 | const code = path.charCodeAt(i); |
| 372 | if (code === 47 /*/*/) { |
| 373 | // If we reached a path separator that was not part of a set of path |
| 374 | // separators at the end of the string, stop now |
| 375 | if (!matchedSlash) { |
| 376 | start = i + 1; |
| 377 | break; |
| 378 | } |
| 379 | } else { |
| 380 | if (firstNonSlashEnd === -1) { |
| 381 | // We saw the first non-path separator, remember this index in case |
| 382 | // we need it if the extension ends up not matching |
| 383 | matchedSlash = false; |
| 384 | firstNonSlashEnd = i + 1; |
| 385 | } |
| 386 | if (extIdx >= 0) { |
| 387 | // Try to match the explicit extension |
| 388 | if (code === ext.charCodeAt(extIdx)) { |
| 389 | if (--extIdx === -1) { |
| 390 | // We matched the extension, so mark this as the end of our path |
| 391 | // component |
| 392 | end = i; |
| 393 | } |
| 394 | } else { |
| 395 | // Extension does not match, so our result is the entire path |
| 396 | // component |
| 397 | extIdx = -1; |
| 398 | end = firstNonSlashEnd; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (start === end) { |
| 405 | end = firstNonSlashEnd; |
| 406 | } else if (end === -1) { |
| 407 | end = path.length; |
| 408 | } |
| 409 | return path.slice(start, end); |
| 410 | } else { |
no test coverage detected
searching dependent graphs…