* @param {string} path * @returns {string}
(path)
| 801 | * @returns {string} |
| 802 | */ |
| 803 | dirname(path) { |
| 804 | validateString(path, 'path'); |
| 805 | const len = path.length; |
| 806 | if (len === 0) |
| 807 | return '.'; |
| 808 | let rootEnd = -1; |
| 809 | let offset = 0; |
| 810 | const code = StringPrototypeCharCodeAt(path, 0); |
| 811 | |
| 812 | if (len === 1) { |
| 813 | // `path` contains just a path separator, exit early to avoid |
| 814 | // unnecessary work or a dot. |
| 815 | return isPathSeparator(code) ? path : '.'; |
| 816 | } |
| 817 | |
| 818 | // Try to match a root |
| 819 | if (isPathSeparator(code)) { |
| 820 | // Possible UNC root |
| 821 | |
| 822 | rootEnd = offset = 1; |
| 823 | |
| 824 | if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { |
| 825 | // Matched double path separator at beginning |
| 826 | let j = 2; |
| 827 | let last = j; |
| 828 | // Match 1 or more non-path separators |
| 829 | while (j < len && |
| 830 | !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { |
| 831 | j++; |
| 832 | } |
| 833 | if (j < len && j !== last) { |
| 834 | // Matched! |
| 835 | last = j; |
| 836 | // Match 1 or more path separators |
| 837 | while (j < len && |
| 838 | isPathSeparator(StringPrototypeCharCodeAt(path, j))) { |
| 839 | j++; |
| 840 | } |
| 841 | if (j < len && j !== last) { |
| 842 | // Matched! |
| 843 | last = j; |
| 844 | // Match 1 or more non-path separators |
| 845 | while (j < len && |
| 846 | !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { |
| 847 | j++; |
| 848 | } |
| 849 | if (j === len) { |
| 850 | // We matched a UNC root only |
| 851 | return path; |
| 852 | } |
| 853 | if (j !== last) { |
| 854 | // We matched a UNC root with leftovers |
| 855 | |
| 856 | // Offset by 1 to include the separator after the UNC root to |
| 857 | // treat it as a "normal root" on top of a (UNC) root |
| 858 | rootEnd = offset = j + 1; |
| 859 | } |
| 860 | } |
searching dependent graphs…