generated from lifeart/els-a11y-addon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
73 lines (63 loc) · 1.72 KB
/
utils.js
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
function normalizeToAngleBracketComponent(name) {
const SIMPLE_DASHERIZE_REGEXP = /[a-z]|\/|-/g;
const ALPHA = /[A-Za-z0-9]/;
if (name.includes(".")) {
return name;
}
return name.replace(SIMPLE_DASHERIZE_REGEXP, (char, index) => {
if (char === "/") {
return "::";
}
if (index === 0 || !ALPHA.test(name[index - 1])) {
return char.toUpperCase();
}
// Remove all occurrences of '-'s from the name that aren't starting with `-`
return char === "-" ? "" : char.toLowerCase();
});
}
let matchFunctions = [];
function normalizeFilePath(filePath) {
return filePath.split("\\").join("/");
}
function hasMatchFunctions() {
return matchFunctions.length > 0;
}
function waitForFileNameContains(name, timeout = 10000) {
let timeoutUid = null;
let resolve = null;
let reject = null;
let deleteFunction = null;
let item = new Promise((res, rej) => {
resolve = res;
reject = (reason) => {
deleteFunction();
rej(reason);
};
});
timeoutUid = setTimeout(() => {
reject("timeout");
}, timeout);
let fn = (uri) => {
if (normalizeFilePath(uri).includes(name)) {
deleteFunction();
setTimeout(resolve);
}
};
matchFunctions.push(fn);
deleteFunction = () => {
clearTimeout(timeoutUid);
matchFunctions = matchFunctions.filter((f) => f !== fn);
};
return item;
}
function watcherFn(uri, changeType) {
// created
if (changeType !== 1) {
return;
}
matchFunctions.forEach((fn) => fn(uri));
}
module.exports.normalizeToAngleBracketComponent = normalizeToAngleBracketComponent;
module.exports.watcherFn = watcherFn;
module.exports.waitForFileNameContains = waitForFileNameContains;
module.exports.hasMatchFunctions = hasMatchFunctions;