Skip to content

Commit e5b0e68

Browse files
author
Bart Veneman
committed
Store location data with Collections
1 parent 1a0900b commit e5b0e68

File tree

4 files changed

+155
-80
lines changed

4 files changed

+155
-80
lines changed

src/collection.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export class Collection {
2+
constructor() {
3+
/** @type {Map<string, Array<Number>>} */
4+
this._items = new Map()
5+
this._total = 0
6+
this.node_lines = []
7+
this.node_columns = []
8+
this.node_lenghts = []
9+
this.node_offsets = []
10+
}
11+
12+
/**
13+
* @param {string} item
14+
* @param {import('css-tree').CssLocation} node_location
15+
*/
16+
push(item, node_location) {
17+
let index = this._totalq
18+
this.node_lines[index] = node_location.start.line
19+
this.node_columns[index] = node_location.start.column
20+
this.node_offsets[index] = node_location.start.offset
21+
this.node_lenghts[index] = node_location.end.offset - node_location.start.offset
22+
23+
if (this._items.has(item)) {
24+
/** @type number[] */
25+
let list = this._items.get(item)
26+
list.push(index)
27+
// this._items.set(item, list)
28+
this._total++
29+
return
30+
}
31+
32+
this._items.set(item, [index])
33+
this._total++
34+
}
35+
36+
size() {
37+
return this._total
38+
}
39+
40+
count() {
41+
let items = new Map()
42+
let unique = {}
43+
this._items.forEach((list, key) => {
44+
let nodes = list.map(index => ({
45+
line: this.node_lines[index],
46+
column: this.node_columns[index],
47+
offset: this.node_offsets[index],
48+
length: this.node_lenghts[index],
49+
}))
50+
items.set(key, nodes)
51+
unique[key] = list.length
52+
})
53+
54+
return {
55+
total: this._total,
56+
totalUnique: this._items.size,
57+
// unique: Object.fromEntries(this._items),
58+
unique,
59+
uniquenessRatio: this._total === 0 ? 0 : this._items.size / this._total,
60+
// __unstable__itemsWithLocations: Object.fromEntries(items),
61+
}
62+
}
63+
}

src/context-collection.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
import { CountableCollection } from './countable-collection.js'
1+
import { Collection } from './collection.js'
22

33
class ContextCollection {
44
constructor() {
5-
this._list = new CountableCollection()
6-
/** @type {Map<string, CountableCollection>} */
5+
this._list = new Collection()
6+
/** @type {Map<string, Collection>} */
77
this._contexts = new Map()
88
}
99

1010
/**
1111
* Add an item to this _list's context
1212
* @param {string} item Item to push
1313
* @param {string} context Context to push Item to
14+
* @param {import('css-tree').CssLocation} node_location
1415
*/
15-
push(item, context) {
16-
this._list.push(item)
16+
push(item, context, node_location) {
17+
this._list.push(item, node_location)
1718

1819
if (!this._contexts.has(context)) {
19-
this._contexts.set(context, new CountableCollection())
20+
this._contexts.set(context, new Collection())
2021
}
2122

22-
this._contexts.get(context).push(item)
23+
this._contexts.get(context).push(item, node_location)
2324
}
2425

2526
count() {

0 commit comments

Comments
 (0)