Repository navigation
Expand file tree
/
Copy pathTemplateCollection.js
More file actions
executable file
·110 lines (86 loc) · 2.53 KB
/
Copy pathTemplateCollection.js
File metadata and controls
executable file
·110 lines (86 loc) · 2.53 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
105
106
107
108
109
110
import { TemplatePath } from "@11ty/eleventy-utils";
import TemplateData from "./Data/TemplateData.js";
import Sortable from "./Util/Objects/Sortable.js";
import { isGlobMatch } from "./Util/GlobMatcher.js";
export default class TemplateCollection extends Sortable {
// Sorted items, tag index, and glob matches, reset by `add`, the only entry point that mutates `items`
#cache = {};
add(item) {
this.#cache = {};
super.add(item);
}
getAll() {
return this.items.slice();
}
#getAllSorted() {
this.#cache.sorted ??= this.sort(Sortable.sortFunctionDateInputPath);
return this.#cache.sorted;
}
// Tag name to items (in sorted order), so tag lookups don’t walk the full collection
#getTagIndex() {
if (!this.#cache.tags) {
let index = new Map();
for (let item of this.#getAllSorted()) {
for (let tagName of TemplateData.getIncludedTagNames(item.data)) {
let tagItems = index.get(tagName);
if (!tagItems) {
tagItems = [];
index.set(tagName, tagItems);
}
tagItems.push(item);
}
}
this.#cache.tags = index;
}
return this.#cache.tags;
}
getTagNames() {
return Array.from(this.#getTagIndex().keys());
}
getAllSorted() {
// Callers sort and reverse the result in place, so hand out a copy
return this.#getAllSorted().slice();
}
getSortedByDate() {
return this.sort(Sortable.sortFunctionDate);
}
getGlobs(globs) {
if (typeof globs === "string") {
globs = [globs];
}
globs = globs.map((glob) => TemplatePath.addLeadingDotSlash(glob));
return globs;
}
getFilteredByGlob(globs) {
globs = this.getGlobs(globs);
this.#cache.globs ??= new Map();
let key = globs.join("::");
if (!this.#cache.globs.has(key)) {
let filtered = this.#getAllSorted().filter((item) => isGlobMatch(item.inputPath, globs));
this.#cache.globs.set(key, filtered);
}
return this.#cache.globs.get(key).slice();
}
getFilteredByTag(tagName) {
if (!tagName) {
return this.getAllSorted();
}
return this.#getTagIndex().get(tagName)?.slice() || [];
}
getFilteredByTags(...tags) {
if (!tags.length) {
return this.getAllSorted();
}
let index = this.#getTagIndex();
let [firstTag, ...remainingTags] = tags;
let matches = index.get(firstTag) || [];
if (!remainingTags.length) {
return matches.slice();
}
// Intersect the first tag’s matches with the remaining tags
let remainingItems = remainingTags.map((tagName) => new Set(index.get(tagName)));
return matches.filter((item) => {
return remainingItems.every((tagItems) => tagItems.has(item));
});
}
}