-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathutils.js
More file actions
60 lines (50 loc) 路 1.65 KB
/
utils.js
File metadata and controls
60 lines (50 loc) 路 1.65 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
const memoTagsCleanup = (wordReplacements, tagTransforms) => {
const wordReplacementsMap = new Map();
for (let [key, value] of Object.entries(wordReplacements)) {
// match words surrounded by white space or at beginning/end
wordReplacementsMap.set(
key,
new RegExp(`(?<=^|\\s+)(${value})(?=\\s+|$)`, 'gi')
);
}
// convert to a map, flipping kv relationship to get fast lookups
const tagTransformsMap = new Map();
for (let [key, values] of Object.entries(tagTransforms)) {
if (!Array.isArray(values)) {
throw Error(
`Value for key "${key}" should be of type "string[]", found type "${typeof values}"`
);
}
for (let value of values) {
const normalizedValue = value.trim().toLowerCase();
if (tagTransformsMap.has(normalizedValue)) {
console.warn(
`WARN: Tag replacement value "${value}" found more than once, last seen in key "${key}"`
);
}
tagTransformsMap.set(normalizedValue, key);
}
}
// return memoized tag cleanup function
return (words) => {
if (!words) return [];
const wordsSet = new Set();
for (let word of words) {
let normalizedWord = word.trim();
// regex replacements
for (let [word, regex] of wordReplacementsMap.entries()) {
normalizedWord = normalizedWord.replaceAll(regex, word);
}
// use special casing if we find a match
const key = normalizedWord.toLowerCase();
if (tagTransformsMap.has(key)) {
normalizedWord = tagTransformsMap.get(key);
}
wordsSet.add(normalizedWord);
}
return [...wordsSet];
};
};
module.exports = {
memoTagsCleanup
};