Skip to content

Commit 2514929

Browse files
committed
feat: add path normalization helpers
1 parent 9fa74c4 commit 2514929

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

src/text.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function normalizeNewlines(input: string): string {
2+
return input.replace(/\r\n/g, '\n');
3+
}
4+
5+
export function normalizePathSegments(segments: readonly string[]): string {
6+
return segments
7+
.map((segment, index) => {
8+
if (segment === '[]') return segment;
9+
return index > 0 ? `.${segment}` : segment;
10+
})
11+
.join('');
12+
}
13+
14+
export function extractPathName(canonicalPath: string): string {
15+
return canonicalPath
16+
.split(".")
17+
.filter((part) => part.length > 0 && part !== "[]")
18+
.at(-1) ?? "";
19+
}
20+
21+
export function sortedReadonly<T>(values: readonly T[], compare: (left: T, right: T) => number): readonly T[] {
22+
const copy = [...values];
23+
copy.sort(compare);
24+
return copy;
25+
}

0 commit comments

Comments
 (0)