MCPcopy Index your code
hub / github.com/code-pushup/cli / truncatePaths

Function truncatePaths

packages/utils/src/lib/file-system.ts:165–206  ·  view source on GitHub ↗
(paths: string[])

Source from the content-addressed store, hash-verified

163}
164
165export function truncatePaths(paths: string[]): string[] {
166 const segmentedPaths = paths
167 .map(splitFilePath)
168 .map(({ folders, file }): string[] => [...folders, file]);
169
170 const first = segmentedPaths[0];
171 const others = segmentedPaths.slice(1);
172 if (!first) {
173 return paths;
174 }
175
176 /* eslint-disable functional/no-let,functional/no-loop-statements,unicorn/no-for-loop */
177 let offsetLeft = 0;
178 let offsetRight = 0;
179 for (let left = 0; left < first.length; left++) {
180 if (others.every(segments => segments[left] === first[left])) {
181 offsetLeft++;
182 } else {
183 break;
184 }
185 }
186 for (let right = 1; right <= first.length; right++) {
187 if (others.every(segments => segments.at(-right) === first.at(-right))) {
188 offsetRight++;
189 } else {
190 break;
191 }
192 }
193 /* eslint-enable functional/no-let,functional/no-loop-statements,unicorn/no-for-loop */
194
195 return segmentedPaths.map(segments => {
196 const uniqueSegments = segments.slice(
197 offsetLeft,
198 offsetRight > 0 ? -offsetRight : undefined,
199 );
200 return path.join(
201 offsetLeft > 0 ? '…' : '',
202 ...uniqueSegments,
203 offsetRight > 0 ? '…' : '',
204 );
205 });
206}

Callers 2

logLcovRecordsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected