Skip to content

Commit

Permalink
refactor(tosu/utils): cleanPath function.
Browse files Browse the repository at this point in the history
Because I'm stupid.
  • Loading branch information
GabuTheDev committed Feb 6, 2025
1 parent 1b40505 commit 0db110d
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/tosu/src/utils/converters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { join } from 'path';

const DOUBLE_POWERS_10 = [
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13,
1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22, 1e23, 1e24, 1e25,
Expand Down Expand Up @@ -78,17 +80,28 @@ export const numberFromDecimal = (
};

/**
* Joins multiple paths into a single path, replacing invalid path characters in the process.
* Joins multiple paths, sanitizing each parameter (like invalid windows characters, trailing spaces, etc.) before joining.
*
* @param {...string[]} paths Paths to join
* @returns {string} Joined path
* @param {...string[]} paths Paths to sanitize and join.
* @returns {string} The joined & sanitized path.
*/
export const cleanPath = (...paths: string[]): string => {
paths = paths.map((path) =>
Buffer.from(path.trim())
.toString('utf8')
.replace(process.platform === 'win32' ? /[<>:"|?*]/g : /\//g, '')
);
paths.map((p) => {
// Ensure UTF-8 encoding and trim whitespace
let cleaned = Buffer.from(p.trim(), 'utf-8').toString('utf-8');

// Replace invalid OS-specific characters
cleaned = cleaned.replace(
process.platform === 'win32' ? /[<>:"/\\|?*]/g : /\//g,
''
);

// On Windows, trim trailing dots and spaces
if (process.platform === 'win32')
cleaned = cleaned.replace(/[ .]+$/, '');

return cleaned;
});

return paths.join(...paths);
return join(...paths);
};

0 comments on commit 0db110d

Please sign in to comment.