-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathpath.js
More file actions
104 lines (89 loc) · 2.88 KB
/
Copy pathpath.js
File metadata and controls
104 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { basename, dirname, resolve } from "path";
import { KeyedCache, KeyedPairCache } from "./cache.js";
/** @type {KeyedCache<string, string>} */
const resolveCache = new KeyedCache();
/** @type {KeyedPairCache<string, string, string>} */
const resolvePairCache = new KeyedPairCache();
/**
*
* @param {string} path Absolute or relative path
* @param {string} segment File or folder
* @returns {boolean} True if resolved path contains segment
*
* @example
* includesSegment("stable/2025-01-01/examples/foo.json", "examples")
* // -> true
*/
export function includesSegment(path, segment) {
return untilLastSegment(path, segment) !== "";
}
/**
* Wraps `path.resolve(path)` with a cache to improve performance
*
* @param {string} path
* @returns {string}
*/
export function resolveCached(path) {
return resolveCache.getOrCreate(path, () => resolve(path));
}
/**
* Wraps `path.resolve(from, to)` with a cache to improve performance
* @param {string} from
* @param {string} to
* @returns {string}
*/
export function resolvePairCached(from, to) {
return resolvePairCache.getOrCreate(from, to, () => resolve(from, to));
}
/**
* @param {string} path Absolute or relative path
* @param {string} segment File or folder
* @returns {string} Portion of resolved path up to (and including) the last occurrence of segment
*
* @example
* untilLastSegment("stable/2025-01-01/examples/foo.json", "examples")
* // -> "{cwd}/stable/2025-01-01/examples"
*/
export function untilLastSegment(path, segment) {
// Shares code with `untilLastSegmentWithParent()`, but not worth refactoring yet
let current = resolveCached(path);
while (true) {
const parent = dirname(current);
if (basename(current) === segment) {
// Found the target folder. Return it.
return current;
} else if (parent === current) {
// Reached the filesystem root (folder not found). Return empty string.
return "";
} else {
// Keep walking upward
current = parent;
}
}
}
/**
* @param {string} path Absolute or relative path
* @param {string} segment File or folder
* @returns {string} Portion of resolved path up to (and including) the last segment with the specified parent
*
* @example
* untilLastSegmentWithParent("specification/foo/data-plane/stable/2025-01-01/foo.json", "specification")
* // -> "{cwd}/specification/foo"
*/
export function untilLastSegmentWithParent(path, segment) {
// Shares code with `untilLastSegment()`, but not worth refactoring yet
let current = resolveCached(path);
while (true) {
const parent = dirname(current);
if (basename(parent) === segment) {
// Found the target parent. Return current;
return current;
} else if (parent === current) {
// Reached the filesystem root (folder not found). Return empty string.
return "";
} else {
// Keep walking upward
current = parent;
}
}
}