MCPcopy Create free account
hub / github.com/codesandbox/sandpack / calculateNearestUniquePath

Function calculateNearestUniquePath

sandpack-react/src/utils/stringUtils.ts:6–54  ·  view source on GitHub ↗
(
  currentPath: string,
  otherPaths: string[]
)

Source from the content-addressed store, hash-verified

4};
5
6export const calculateNearestUniquePath = (
7 currentPath: string,
8 otherPaths: string[]
9): string => {
10 const currentPathParts = (
11 currentPath[0] === "/" ? currentPath.slice(1) : currentPath
12 ).split("/");
13 const resultPathParts: string[] = [];
14
15 // If path is on root, there are no parts to loop through
16 if (currentPathParts.length === 1) {
17 resultPathParts.unshift(currentPathParts[0]);
18 } else {
19 // Loop over all other paths to find a unique path
20 for (let fileIndex = 0; fileIndex < otherPaths.length; fileIndex++) {
21 // We go over each part of the path from end to start to find the closest unique directory
22 const otherPathParts = otherPaths[fileIndex].split("/");
23 for (
24 let partsFromEnd = 1;
25 partsFromEnd <= currentPathParts.length;
26 partsFromEnd++
27 ) {
28 const currentPathPart =
29 currentPathParts[currentPathParts.length - partsFromEnd];
30 const otherPathPart =
31 otherPathParts[otherPathParts.length - partsFromEnd];
32
33 // If this part hasn't been added to the result path, we add it here
34 if (resultPathParts.length < partsFromEnd) {
35 resultPathParts.unshift(currentPathPart);
36 }
37
38 // If this part is different between the current path and other path we break
39 // as from this moment the current path is unique compared to this other path
40 if (currentPathPart !== otherPathPart) {
41 break;
42 }
43 }
44 }
45 }
46
47 // Add `..` if this is a relative path
48 if (resultPathParts.length < currentPathParts.length) {
49 resultPathParts.unshift("..");
50 }
51
52 // Join the result path parts into a path string
53 return resultPathParts.join("/");
54};
55
56export const hexToRGB = (
57 hex: string

Callers 2

getTriggerTextFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected