diff --git a/lib/path.js b/lib/path.js index 71788e53d3efd8..4845dbb0f0b553 100644 --- a/lib/path.js +++ b/lib/path.js @@ -21,6 +21,13 @@ 'use strict'; +const { + FunctionPrototypeBind, + StringPrototypeCharCodeAt, + StringPrototypeLastIndexOf, + StringPrototypeSlice, + StringPrototypeToLowerCase, +} = primordials; const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { CHAR_UPPERCASE_A, @@ -57,7 +64,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { let code = 0; for (let i = 0; i <= path.length; ++i) { if (i < path.length) - code = path.charCodeAt(i); + code = StringPrototypeCharCodeAt(path, i); else if (isPathSeparator(code)) break; else @@ -68,16 +75,17 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { // NOOP } else if (dots === 2) { if (res.length < 2 || lastSegmentLength !== 2 || - res.charCodeAt(res.length - 1) !== CHAR_DOT || - res.charCodeAt(res.length - 2) !== CHAR_DOT) { + StringPrototypeCharCodeAt(res, res.length - 1) !== CHAR_DOT || + StringPrototypeCharCodeAt(res, res.length - 2) !== CHAR_DOT) { if (res.length > 2) { - const lastSlashIndex = res.lastIndexOf(separator); + const lastSlashIndex = StringPrototypeLastIndexOf(res, separator); if (lastSlashIndex === -1) { res = ''; lastSegmentLength = 0; } else { - res = res.slice(0, lastSlashIndex); - lastSegmentLength = res.length - 1 - res.lastIndexOf(separator); + res = StringPrototypeSlice(res, 0, lastSlashIndex); + lastSegmentLength = + res.length - 1 - StringPrototypeLastIndexOf(res, separator); } lastSlash = i; dots = 0; @@ -96,9 +104,9 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { } } else { if (res.length > 0) - res += `${separator}${path.slice(lastSlash + 1, i)}`; + res += `${separator}${StringPrototypeSlice(path, lastSlash + 1, i)}`; else - res = path.slice(lastSlash + 1, i); + res = StringPrototypeSlice(path, lastSlash + 1, i); lastSegmentLength = i - lastSlash - 1; } lastSlash = i; @@ -170,8 +178,9 @@ const win32 = { // Verify that a cwd was found and that it actually points // to our drive. If not, default to the drive's root. if (path === undefined || - (path.slice(0, 2).toLowerCase() !== resolvedDevice.toLowerCase() && - path.charCodeAt(2) === CHAR_BACKWARD_SLASH)) { + (StringPrototypeSlice(path, 0, 2).toLowerCase() !== + StringPrototypeToLowerCase(resolvedDevice) && + StringPrototypeCharCodeAt(path, 2) === CHAR_BACKWARD_SLASH)) { path = `${resolvedDevice}\\`; } } @@ -180,7 +189,7 @@ const win32 = { let rootEnd = 0; let device = ''; let isAbsolute = false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); // Try to match a root if (len === 1) { @@ -196,32 +205,36 @@ const win32 = { // absolute path of some kind (UNC or otherwise) isAbsolute = true; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // Matched double path separator at beginning let j = 2; let last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { - const firstPart = path.slice(last, j); + const firstPart = StringPrototypeSlice(path, last, j); // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len || j !== last) { // We matched a UNC root - device = `\\\\${firstPart}\\${path.slice(last, j)}`; + device = + `\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`; rootEnd = j; } } @@ -230,11 +243,11 @@ const win32 = { rootEnd = 1; } } else if (isWindowsDeviceRoot(code) && - path.charCodeAt(1) === CHAR_COLON) { + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root - device = path.slice(0, 2); + device = StringPrototypeSlice(path, 0, 2); rootEnd = 2; - if (len > 2 && isPathSeparator(path.charCodeAt(2))) { + if (len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { // Treat separator following drive name as an absolute path // indicator isAbsolute = true; @@ -244,7 +257,8 @@ const win32 = { if (device.length > 0) { if (resolvedDevice.length > 0) { - if (device.toLowerCase() !== resolvedDevice.toLowerCase()) + if (StringPrototypeToLowerCase(device) !== + StringPrototypeToLowerCase(resolvedDevice)) // This path points to another device so it is not applicable continue; } else { @@ -256,7 +270,8 @@ const win32 = { if (resolvedDevice.length > 0) break; } else { - resolvedTail = `${path.slice(rootEnd)}\\${resolvedTail}`; + resolvedTail = + `${StringPrototypeSlice(path, rootEnd)}\\${resolvedTail}`; resolvedAbsolute = isAbsolute; if (isAbsolute && resolvedDevice.length > 0) { break; @@ -289,7 +304,7 @@ const win32 = { let rootEnd = 0; let device; let isAbsolute = false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); // Try to match a root if (len === 1) { @@ -304,38 +319,42 @@ const win32 = { // path of some kind (UNC or otherwise) isAbsolute = true; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // Matched double path separator at beginning let j = 2; let last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { - const firstPart = path.slice(last, j); + const firstPart = StringPrototypeSlice(path, last, j); // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { // We matched a UNC root only // Return the normalized version of the UNC root since there // is nothing left to process - return `\\\\${firstPart}\\${path.slice(last)}\\`; + return `\\\\${firstPart}\\${StringPrototypeSlice(path, last)}\\`; } if (j !== last) { // We matched a UNC root with leftovers - device = `\\\\${firstPart}\\${path.slice(last, j)}`; + device = + `\\\\${firstPart}\\${StringPrototypeSlice(path, last, j)}`; rootEnd = j; } } @@ -343,11 +362,12 @@ const win32 = { } else { rootEnd = 1; } - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root - device = path.slice(0, 2); + device = StringPrototypeSlice(path, 0, 2); rootEnd = 2; - if (len > 2 && isPathSeparator(path.charCodeAt(2))) { + if (len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { // Treat separator following drive name as an absolute path // indicator isAbsolute = true; @@ -356,11 +376,13 @@ const win32 = { } let tail = rootEnd < len ? - normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator) : + normalizeString(StringPrototypeSlice(path, rootEnd), + !isAbsolute, '\\', isPathSeparator) : ''; if (tail.length === 0 && !isAbsolute) tail = '.'; - if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) + if (tail.length > 0 && + isPathSeparator(StringPrototypeCharCodeAt(path, len - 1))) tail += '\\'; if (device === undefined) { return isAbsolute ? `\\${tail}` : tail; @@ -378,13 +400,13 @@ const win32 = { if (len === 0) return false; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); return isPathSeparator(code) || // Possible device root (len > 2 && isWindowsDeviceRoot(code) && - path.charCodeAt(1) === CHAR_COLON && - isPathSeparator(path.charCodeAt(2))); + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON && + isPathSeparator(StringPrototypeCharCodeAt(path, 2))); }, /** @@ -426,13 +448,14 @@ const win32 = { // path.join('//server', 'share') -> '\\\\server\\share\\') let needsReplace = true; let slashCount = 0; - if (isPathSeparator(firstPart.charCodeAt(0))) { + if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 0))) { ++slashCount; const firstLen = firstPart.length; - if (firstLen > 1 && isPathSeparator(firstPart.charCodeAt(1))) { + if (firstLen > 1 && + isPathSeparator(StringPrototypeCharCodeAt(firstPart, 1))) { ++slashCount; if (firstLen > 2) { - if (isPathSeparator(firstPart.charCodeAt(2))) + if (isPathSeparator(StringPrototypeCharCodeAt(firstPart, 2))) ++slashCount; else { // We matched a UNC path in the first part @@ -444,13 +467,13 @@ const win32 = { if (needsReplace) { // Find any more consecutive slashes we need to replace while (slashCount < joined.length && - isPathSeparator(joined.charCodeAt(slashCount))) { + isPathSeparator(StringPrototypeCharCodeAt(joined, slashCount))) { slashCount++; } // Replace the slashes if needed if (slashCount >= 2) - joined = `\\${joined.slice(slashCount)}`; + joined = `\\${StringPrototypeSlice(joined, slashCount)}`; } return win32.normalize(joined); @@ -478,8 +501,8 @@ const win32 = { if (fromOrig === toOrig) return ''; - from = fromOrig.toLowerCase(); - to = toOrig.toLowerCase(); + from = StringPrototypeToLowerCase(fromOrig); + to = StringPrototypeToLowerCase(toOrig); if (from === to) return ''; @@ -487,13 +510,15 @@ const win32 = { // Trim any leading backslashes let fromStart = 0; while (fromStart < from.length && - from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(from, fromStart) === CHAR_BACKWARD_SLASH) { fromStart++; } // Trim trailing backslashes (applicable to UNC paths only) let fromEnd = from.length; - while (fromEnd - 1 > fromStart && - from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) { + while ( + fromEnd - 1 > fromStart && + StringPrototypeCharCodeAt(from, fromEnd - 1) === CHAR_BACKWARD_SLASH + ) { fromEnd--; } const fromLen = fromEnd - fromStart; @@ -501,13 +526,13 @@ const win32 = { // Trim any leading backslashes let toStart = 0; while (toStart < to.length && - to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(to, toStart) === CHAR_BACKWARD_SLASH) { toStart++; } // Trim trailing backslashes (applicable to UNC paths only) let toEnd = to.length; while (toEnd - 1 > toStart && - to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) { + StringPrototypeCharCodeAt(to, toEnd - 1) === CHAR_BACKWARD_SLASH) { toEnd--; } const toLen = toEnd - toStart; @@ -517,8 +542,8 @@ const win32 = { let lastCommonSep = -1; let i = 0; for (; i < length; i++) { - const fromCode = from.charCodeAt(fromStart + i); - if (fromCode !== to.charCodeAt(toStart + i)) + const fromCode = StringPrototypeCharCodeAt(from, fromStart + i); + if (fromCode !== StringPrototypeCharCodeAt(to, toStart + i)) break; else if (fromCode === CHAR_BACKWARD_SLASH) lastCommonSep = i; @@ -531,19 +556,21 @@ const win32 = { return toOrig; } else { if (toLen > length) { - if (to.charCodeAt(toStart + i) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(to, toStart + i) === + CHAR_BACKWARD_SLASH) { // We get here if `from` is the exact base path for `to`. // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz' - return toOrig.slice(toStart + i + 1); + return StringPrototypeSlice(toOrig, toStart + i + 1); } if (i === 2) { // We get here if `from` is the device root. // For example: from='C:\\'; to='C:\\foo' - return toOrig.slice(toStart + i); + return StringPrototypeSlice(toOrig, toStart + i); } } if (fromLen > length) { - if (from.charCodeAt(fromStart + i) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(from, fromStart + i) === + CHAR_BACKWARD_SLASH) { // We get here if `to` is the exact base path for `from`. // For example: from='C:\\foo\\bar'; to='C:\\foo' lastCommonSep = i; @@ -561,7 +588,8 @@ const win32 = { // Generate the relative path based on the path difference between `to` and // `from` for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { - if (i === fromEnd || from.charCodeAt(i) === CHAR_BACKWARD_SLASH) { + if (i === fromEnd || + StringPrototypeCharCodeAt(from, i) === CHAR_BACKWARD_SLASH) { out += out.length === 0 ? '..' : '\\..'; } } @@ -571,11 +599,11 @@ const win32 = { // Lastly, append the rest of the destination (`to`) path that comes after // the common path parts if (out.length > 0) - return `${out}${toOrig.slice(toStart, toEnd)}`; + return `${out}${StringPrototypeSlice(toOrig, toStart, toEnd)}`; - if (toOrig.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) + if (StringPrototypeCharCodeAt(toOrig, toStart) === CHAR_BACKWARD_SLASH) ++toStart; - return toOrig.slice(toStart, toEnd); + return StringPrototypeSlice(toOrig, toStart, toEnd); }, toNamespacedPath(path) { @@ -592,18 +620,20 @@ const win32 = { if (resolvedPath.length <= 2) return path; - if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) { + if (StringPrototypeCharCodeAt(resolvedPath, 0) === CHAR_BACKWARD_SLASH) { // Possible UNC root - if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) { - const code = resolvedPath.charCodeAt(2); + if (StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_BACKWARD_SLASH) { + const code = StringPrototypeCharCodeAt(resolvedPath, 2); if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) { // Matched non-long UNC root, convert the path to a long UNC path - return `\\\\?\\UNC\\${resolvedPath.slice(2)}`; + return `\\\\?\\UNC\\${StringPrototypeSlice(resolvedPath, 2)}`; } } - } else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0)) && - resolvedPath.charCodeAt(1) === CHAR_COLON && - resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) { + } else if ( + isWindowsDeviceRoot(StringPrototypeCharCodeAt(resolvedPath, 0)) && + StringPrototypeCharCodeAt(resolvedPath, 1) === CHAR_COLON && + StringPrototypeCharCodeAt(resolvedPath, 2) === CHAR_BACKWARD_SLASH + ) { // Matched device root, convert the path to a long UNC path return `\\\\?\\${resolvedPath}`; } @@ -622,7 +652,7 @@ const win32 = { return '.'; let rootEnd = -1; let offset = 0; - const code = path.charCodeAt(0); + const code = StringPrototypeCharCodeAt(path, 0); if (len === 1) { // `path` contains just a path separator, exit early to avoid @@ -636,26 +666,29 @@ const win32 = { rootEnd = offset = 1; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // Matched double path separator at beginning let j = 2; let last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { @@ -673,15 +706,17 @@ const win32 = { } } // Possible device root - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { - rootEnd = len > 2 && isPathSeparator(path.charCodeAt(2)) ? 3 : 2; + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { + rootEnd = + len > 2 && isPathSeparator(StringPrototypeCharCodeAt(path, 2)) ? 3 : 2; offset = rootEnd; } let end = -1; let matchedSlash = true; for (let i = len - 1; i >= offset; --i) { - if (isPathSeparator(path.charCodeAt(i))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) { if (!matchedSlash) { end = i; break; @@ -698,7 +733,7 @@ const win32 = { end = rootEnd; } - return path.slice(0, end); + return StringPrototypeSlice(path, 0, end); }, /** @@ -718,8 +753,8 @@ const win32 = { // path separator as an extra separator at the end of the path that can be // disregarded if (path.length >= 2 && - isWindowsDeviceRoot(path.charCodeAt(0)) && - path.charCodeAt(1) === CHAR_COLON) { + isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0)) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { start = 2; } @@ -729,7 +764,7 @@ const win32 = { let extIdx = ext.length - 1; let firstNonSlashEnd = -1; for (let i = path.length - 1; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -746,7 +781,7 @@ const win32 = { } if (extIdx >= 0) { // Try to match the explicit extension - if (code === ext.charCodeAt(extIdx)) { + if (code === StringPrototypeCharCodeAt(ext, extIdx)) { if (--extIdx === -1) { // We matched the extension, so mark this as the end of our path // component @@ -766,10 +801,10 @@ const win32 = { end = firstNonSlashEnd; else if (end === -1) end = path.length; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); } for (let i = path.length - 1; i >= start; --i) { - if (isPathSeparator(path.charCodeAt(i))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, i))) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now if (!matchedSlash) { @@ -786,7 +821,7 @@ const win32 = { if (end === -1) return ''; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); }, /** @@ -809,13 +844,13 @@ const win32 = { // disregarded if (path.length >= 2 && - path.charCodeAt(1) === CHAR_COLON && - isWindowsDeviceRoot(path.charCodeAt(0))) { + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON && + isWindowsDeviceRoot(StringPrototypeCharCodeAt(path, 0))) { start = startPart = 2; } for (let i = path.length - 1; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -854,10 +889,10 @@ const win32 = { startDot === startPart + 1)) { return ''; } - return path.slice(startDot, end); + return StringPrototypeSlice(path, startDot, end); }, - format: _format.bind(null, '\\'), + format: FunctionPrototypeBind(_format, null, '\\'), /** * @param {string} path @@ -878,7 +913,7 @@ const win32 = { const len = path.length; let rootEnd = 0; - let code = path.charCodeAt(0); + let code = StringPrototypeCharCodeAt(path, 0); if (len === 1) { if (isPathSeparator(code)) { @@ -895,26 +930,29 @@ const win32 = { // Possible UNC root rootEnd = 1; - if (isPathSeparator(path.charCodeAt(1))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 1))) { // Matched double path separator at beginning let j = 2; let last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more path separators - while (j < len && isPathSeparator(path.charCodeAt(j))) { + while (j < len && + isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators - while (j < len && !isPathSeparator(path.charCodeAt(j))) { + while (j < len && + !isPathSeparator(StringPrototypeCharCodeAt(path, j))) { j++; } if (j === len) { @@ -927,7 +965,8 @@ const win32 = { } } } - } else if (isWindowsDeviceRoot(code) && path.charCodeAt(1) === CHAR_COLON) { + } else if (isWindowsDeviceRoot(code) && + StringPrototypeCharCodeAt(path, 1) === CHAR_COLON) { // Possible device root if (len <= 2) { // `path` contains just a drive root, exit early to avoid @@ -936,7 +975,7 @@ const win32 = { return ret; } rootEnd = 2; - if (isPathSeparator(path.charCodeAt(2))) { + if (isPathSeparator(StringPrototypeCharCodeAt(path, 2))) { if (len === 3) { // `path` contains just a drive root, exit early to avoid // unnecessary work @@ -947,7 +986,7 @@ const win32 = { } } if (rootEnd > 0) - ret.root = path.slice(0, rootEnd); + ret.root = StringPrototypeSlice(path, 0, rootEnd); let startDot = -1; let startPart = rootEnd; @@ -961,7 +1000,7 @@ const win32 = { // Get non-dir info for (; i >= rootEnd; --i) { - code = path.charCodeAt(i); + code = StringPrototypeCharCodeAt(path, i); if (isPathSeparator(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -998,11 +1037,11 @@ const win32 = { (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) { - ret.base = ret.name = path.slice(startPart, end); + ret.base = ret.name = StringPrototypeSlice(path, startPart, end); } else { - ret.name = path.slice(startPart, startDot); - ret.base = path.slice(startPart, end); - ret.ext = path.slice(startDot, end); + ret.name = StringPrototypeSlice(path, startPart, startDot); + ret.base = StringPrototypeSlice(path, startPart, end); + ret.ext = StringPrototypeSlice(path, startDot, end); } } @@ -1010,7 +1049,7 @@ const win32 = { // the trailing slash if any (`C:\abc` -> `C:\`). Otherwise, strip out the // trailing slash (`C:\abc\def` -> `C:\abc`). if (startPart > 0 && startPart !== rootEnd) - ret.dir = path.slice(0, startPart - 1); + ret.dir = StringPrototypeSlice(path, 0, startPart - 1); else ret.dir = ret.root; @@ -1044,7 +1083,8 @@ const posix = { } resolvedPath = `${path}/${resolvedPath}`; - resolvedAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + resolvedAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; } // At this point the path should be resolved to a full absolute path, but @@ -1070,9 +1110,10 @@ const posix = { if (path.length === 0) return '.'; - const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const isAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; const trailingSeparator = - path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH; + StringPrototypeCharCodeAt(path, path.length - 1) === CHAR_FORWARD_SLASH; // Normalize the path path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator); @@ -1094,7 +1135,8 @@ const posix = { */ isAbsolute(path) { validateString(path, 'path'); - return path.length > 0 && path.charCodeAt(0) === CHAR_FORWARD_SLASH; + return path.length > 0 && + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; }, /** @@ -1150,26 +1192,27 @@ const posix = { let lastCommonSep = -1; let i = 0; for (; i < length; i++) { - const fromCode = from.charCodeAt(fromStart + i); - if (fromCode !== to.charCodeAt(toStart + i)) + const fromCode = StringPrototypeCharCodeAt(from, fromStart + i); + if (fromCode !== StringPrototypeCharCodeAt(to, toStart + i)) break; else if (fromCode === CHAR_FORWARD_SLASH) lastCommonSep = i; } if (i === length) { if (toLen > length) { - if (to.charCodeAt(toStart + i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(to, toStart + i) === CHAR_FORWARD_SLASH) { // We get here if `from` is the exact base path for `to`. // For example: from='/foo/bar'; to='/foo/bar/baz' - return to.slice(toStart + i + 1); + return StringPrototypeSlice(to, toStart + i + 1); } if (i === 0) { // We get here if `from` is the root // For example: from='/'; to='/foo' - return to.slice(toStart + i); + return StringPrototypeSlice(to, toStart + i); } } else if (fromLen > length) { - if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(from, fromStart + i) === + CHAR_FORWARD_SLASH) { // We get here if `to` is the exact base path for `from`. // For example: from='/foo/bar/baz'; to='/foo/bar' lastCommonSep = i; @@ -1185,14 +1228,15 @@ const posix = { // Generate the relative path based on the path difference between `to` // and `from`. for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { - if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (i === fromEnd || + StringPrototypeCharCodeAt(from, i) === CHAR_FORWARD_SLASH) { out += out.length === 0 ? '..' : '/..'; } } // Lastly, append the rest of the destination (`to`) path that comes after // the common path parts. - return `${out}${to.slice(toStart + lastCommonSep)}`; + return `${out}${StringPrototypeSlice(to, toStart + lastCommonSep)}`; }, toNamespacedPath(path) { @@ -1208,11 +1252,11 @@ const posix = { validateString(path, 'path'); if (path.length === 0) return '.'; - const hasRoot = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const hasRoot = StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; let end = -1; let matchedSlash = true; for (let i = path.length - 1; i >= 1; --i) { - if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(path, i) === CHAR_FORWARD_SLASH) { if (!matchedSlash) { end = i; break; @@ -1227,7 +1271,7 @@ const posix = { return hasRoot ? '/' : '.'; if (hasRoot && end === 1) return '//'; - return path.slice(0, end); + return StringPrototypeSlice(path, 0, end); }, /** @@ -1250,7 +1294,7 @@ const posix = { let extIdx = ext.length - 1; let firstNonSlashEnd = -1; for (let i = path.length - 1; i >= 0; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1267,7 +1311,7 @@ const posix = { } if (extIdx >= 0) { // Try to match the explicit extension - if (code === ext.charCodeAt(extIdx)) { + if (code === StringPrototypeCharCodeAt(ext, extIdx)) { if (--extIdx === -1) { // We matched the extension, so mark this as the end of our path // component @@ -1287,10 +1331,10 @@ const posix = { end = firstNonSlashEnd; else if (end === -1) end = path.length; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); } for (let i = path.length - 1; i >= 0; --i) { - if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) { + if (StringPrototypeCharCodeAt(path, i) === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now if (!matchedSlash) { @@ -1307,7 +1351,7 @@ const posix = { if (end === -1) return ''; - return path.slice(start, end); + return StringPrototypeSlice(path, start, end); }, /** @@ -1324,7 +1368,7 @@ const posix = { // after any path separator we find let preDotState = 0; for (let i = path.length - 1; i >= 0; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1363,7 +1407,7 @@ const posix = { startDot === startPart + 1)) { return ''; } - return path.slice(startDot, end); + return StringPrototypeSlice(path, startDot, end); }, format: _format.bind(null, '/'), @@ -1384,7 +1428,8 @@ const posix = { const ret = { root: '', dir: '', base: '', ext: '', name: '' }; if (path.length === 0) return ret; - const isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH; + const isAbsolute = + StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; let start; if (isAbsolute) { ret.root = '/'; @@ -1404,7 +1449,7 @@ const posix = { // Get non-dir info for (; i >= start; --i) { - const code = path.charCodeAt(i); + const code = StringPrototypeCharCodeAt(path, i); if (code === CHAR_FORWARD_SLASH) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now @@ -1442,16 +1487,16 @@ const posix = { (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) { - ret.base = ret.name = path.slice(start, end); + ret.base = ret.name = StringPrototypeSlice(path, start, end); } else { - ret.name = path.slice(start, startDot); - ret.base = path.slice(start, end); - ret.ext = path.slice(startDot, end); + ret.name = StringPrototypeSlice(path, start, startDot); + ret.base = StringPrototypeSlice(path, start, end); + ret.ext = StringPrototypeSlice(path, startDot, end); } } if (startPart > 0) - ret.dir = path.slice(0, startPart - 1); + ret.dir = StringPrototypeSlice(path, 0, startPart - 1); else if (isAbsolute) ret.dir = '/';