Browse Source

Avoids options object copy

main
Eric Amodio 3 years ago
parent
commit
0742ce2218
1 changed files with 4 additions and 9 deletions
  1. +4
    -9
      src/system/string.ts

+ 4
- 9
src/system/string.ts View File

@ -285,27 +285,22 @@ export function md5(s: string, encoding: BinaryToTextEncoding = 'base64'): strin
return createHash('md5').update(s).digest(encoding); return createHash('md5').update(s).digest(encoding);
} }
export function normalizePath(
fileName: string,
options: { addLeadingSlash?: boolean; stripTrailingSlash?: boolean } = { stripTrailingSlash: true },
) {
export function normalizePath(fileName: string, options?: { addLeadingSlash?: boolean; stripTrailingSlash?: boolean }) {
if (fileName == null || fileName.length === 0) return fileName; if (fileName == null || fileName.length === 0) return fileName;
let normalized = fileName.replace(pathNormalizeRegex, '/'); let normalized = fileName.replace(pathNormalizeRegex, '/');
const { addLeadingSlash, stripTrailingSlash } = { stripTrailingSlash: true, ...options };
if (stripTrailingSlash) {
if (options?.stripTrailingSlash ?? true) {
normalized = normalized.replace(pathStripTrailingSlashRegex, emptyStr); normalized = normalized.replace(pathStripTrailingSlashRegex, emptyStr);
} }
if (addLeadingSlash && normalized.charCodeAt(0) !== CharCode.Slash) {
if ((options?.addLeadingSlash ?? false) && normalized.charCodeAt(0) !== CharCode.Slash) {
normalized = `/${normalized}`; normalized = `/${normalized}`;
} }
if (isWindows) { if (isWindows) {
// Ensure that drive casing is normalized (lower case) // Ensure that drive casing is normalized (lower case)
normalized = normalized.replace(driveLetterNormalizeRegex, (drive: string) => drive.toLowerCase());
normalized = normalized.replace(driveLetterNormalizeRegex, drive => drive.toLowerCase());
} }
return normalized; return normalized;

Loading…
Cancel
Save