MCPcopy
hub / github.com/directus/directus / normalizePath

Function normalizePath

packages/utils/shared/normalize-path.ts:4–40  ·  view source on GitHub ↗
(
	path: string,
	{
		removeLeading,
	}: {
		removeLeading: boolean;
	} = { removeLeading: false },
)

Source from the content-addressed store, hash-verified

2 * Replace windows style backslashes with unix forwards slashes
3 */
4export const normalizePath = (
5 path: string,
6 {
7 removeLeading,
8 }: {
9 removeLeading: boolean;
10 } = { removeLeading: false },
11): string => {
12 if (path === '\\' || path === '/') return '/';
13
14 if (path.length <= 1) {
15 return path;
16 }
17
18 let prefix = '';
19
20 if (path.length > 4 && path[3] === '\\') {
21 if (['?', '.'].includes(path[2]!) && path.slice(0, 2) === '\\\\') {
22 path = path.slice(2);
23 prefix = '//';
24 }
25 }
26
27 const segments = path.split(/[/\\]+/);
28
29 if (segments.at(-1) === '') {
30 segments.pop();
31 }
32
33 const normalizedPath = prefix + segments.join('/');
34
35 if (removeLeading && path.startsWith('/')) {
36 return normalizedPath.substring(1);
37 }
38
39 return normalizedPath;
40};

Callers 15

constructorMethod · 0.90
fullPathMethod · 0.90
statMethod · 0.90
listGeneratorMethod · 0.90
constructorMethod · 0.90
fullPathMethod · 0.90
index.test.tsFile · 0.90
constructorMethod · 0.90
fullPathMethod · 0.90
statMethod · 0.90
deleteMethod · 0.90
constructorMethod · 0.90

Calls 2

sliceMethod · 0.80
joinMethod · 0.80

Tested by

no test coverage detected