-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontext-collection.ts
More file actions
57 lines (46 loc) · 1.6 KB
/
Copy pathcontext-collection.ts
File metadata and controls
57 lines (46 loc) · 1.6 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
import { Collection, type CollectionCount, type Location, type UniqueWithLocations } from './collection.js'
export class ContextCollection {
#list: Collection
#contexts: Map<string, Collection>
#useLocations: boolean
constructor(useLocations = false) {
this.#list = new Collection(useLocations)
this.#contexts = new Map()
this.#useLocations = useLocations
}
/**
* Add an item to this #list's context
* @param item Item to push
* @param context Context to push Item to
* @param node_location
*/
push(item: string, context: string, node_location: Location) {
this.#list.p(item, node_location)
if (!this.#contexts.has(context)) {
this.#contexts.set(context, new Collection(this.#useLocations))
}
this.#contexts.get(context)!.p(item, node_location)
}
count() {
let itemsPerContext: Map<string, CollectionCount> = new Map()
for (let [context, value] of this.#contexts.entries()) {
itemsPerContext.set(context, value.c())
}
return Object.assign(this.#list.c(), {
itemsPerContext: Object.fromEntries(itemsPerContext),
})
}
/** Returns location data for the top-level list, or undefined when not tracking locations */
locs(): UniqueWithLocations | undefined {
return this.#list.locs()
}
/** Returns location data per context, or undefined when not tracking locations */
locsPerContext(): Record<string, UniqueWithLocations> | undefined {
if (!this.#useLocations) return undefined
let result: Record<string, UniqueWithLocations> = {}
for (let [context, collection] of this.#contexts.entries()) {
result[context] = collection.locs() ?? {}
}
return result
}
}