11export class Collection {
2- constructor ( { useLocations = false } ) {
2+ /** @param {boolean } useLocations */
3+ constructor ( useLocations = false ) {
34 /** @type {Map<string, number[]> } */
45 this . _items = new Map ( )
56 this . _total = 0
6- /** @type {number[] } */
7- this . node_lines = [ ]
8- /** @type {number[] } */
9- this . node_columns = [ ]
10- /** @type {number[] } */
11- this . node_lengths = [ ]
12- /** @type {number[] } */
13- this . node_offsets = [ ]
7+
8+ if ( useLocations ) {
9+ /** @type {number[] } */
10+ this . _node_lines = [ ]
11+ /** @type {number[] } */
12+ this . _node_columns = [ ]
13+ /** @type {number[] } */
14+ this . _node_lengths = [ ]
15+ /** @type {number[] } */
16+ this . _node_offsets = [ ]
17+ }
1418
1519 /** @type {boolean } */
1620 this . _useLocations = useLocations
@@ -20,13 +24,18 @@ export class Collection {
2024 * @param {string } item
2125 * @param {import('css-tree').CssLocation } node_location
2226 */
23- push ( item , node_location ) {
27+ p ( item , node_location ) {
2428 let index = this . _total
2529
26- this . node_lines [ index ] = node_location . start . line
27- this . node_columns [ index ] = node_location . start . column
28- this . node_offsets [ index ] = node_location . start . offset
29- this . node_lengths [ index ] = node_location . end . offset - node_location . start . offset
30+ if ( this . _useLocations ) {
31+ let start = node_location . start
32+ let start_offset = start . offset
33+
34+ this . _node_lines [ index ] = start . line
35+ this . _node_columns [ index ] = start . column
36+ this . _node_offsets [ index ] = start_offset
37+ this . _node_lengths [ index ] = node_location . end . offset - start_offset
38+ }
3039
3140 if ( this . _items . has ( item ) ) {
3241 /** @type number[] */
@@ -53,35 +62,41 @@ export class Collection {
5362 *
5463 * @returns {{ total: number; totalUnique: number; uniquenessRatio: number; unique: Record<string, number>; __unstable__uniqueWithLocations: Record<string, CssLocation[]>} }
5564 */
56- count ( ) {
65+ c ( ) {
66+ let useLocations = this . _useLocations
5767 let uniqueWithLocations = new Map ( )
5868 let unique = { }
59- this . _items . forEach ( ( list , key ) => {
60- let nodes = list . map ( index => ( {
61- line : this . node_lines [ index ] ,
62- column : this . node_columns [ index ] ,
63- offset : this . node_offsets [ index ] ,
64- length : this . node_lengths [ index ] ,
65- } ) )
66- uniqueWithLocations . set ( key , nodes )
69+ let items = this . _items
70+ let size = items . size
71+
72+ items . forEach ( ( list , key ) => {
73+ if ( useLocations ) {
74+ let nodes = list . map ( index => ( {
75+ line : this . _node_lines [ index ] ,
76+ column : this . _node_columns [ index ] ,
77+ offset : this . _node_offsets [ index ] ,
78+ length : this . _node_lengths [ index ] ,
79+ } ) )
80+ uniqueWithLocations . set ( key , nodes )
81+ }
6782 unique [ key ] = list . length
6883 } )
6984
7085 if ( this . _useLocations ) {
7186 return {
7287 total : this . _total ,
73- totalUnique : this . _items . size ,
88+ totalUnique : size ,
7489 unique,
75- uniquenessRatio : this . _total === 0 ? 0 : this . _items . size / this . _total ,
90+ uniquenessRatio : this . _total === 0 ? 0 : size / this . _total ,
7691 __unstable__uniqueWithLocations : Object . fromEntries ( uniqueWithLocations ) ,
7792 }
7893 }
7994
8095 return {
8196 total : this . _total ,
82- totalUnique : this . _items . size ,
97+ totalUnique : size ,
8398 unique,
84- uniquenessRatio : this . _total === 0 ? 0 : this . _items . size / this . _total ,
99+ uniquenessRatio : this . _total === 0 ? 0 : size / this . _total ,
85100 }
86101 }
87102}
0 commit comments