@@ -43,18 +43,22 @@ function ratio(part: number, total: number): number {
4343 return part / total
4444}
4545
46- let defaults = {
47- useLocations : false ,
48- }
49-
50- type Options = {
46+ export type Options = {
5147 /** @description Use Locations (`{ 'item': [{ line, column, offset, length }] }`) instead of a regular count per occurrence (`{ 'item': 3 }`) */
5248 useLocations ?: boolean
5349}
5450
55- export function analyze ( css : string , options : Options = { } ) {
56- let settings = Object . assign ( { } , defaults , options )
57- let useLocations = settings . useLocations === true
51+ export function analyze ( css : string , options ?: Options & { useLocations ?: false | undefined } ) : ReturnType < typeof analyzeInternal < false > >
52+ export function analyze ( css : string , options : Options & { useLocations : true } ) : ReturnType < typeof analyzeInternal < true > >
53+ export function analyze ( css : string , options : Options = { } ) : any {
54+ const useLocations = options . useLocations === true
55+ if ( useLocations ) {
56+ return analyzeInternal ( css , options , true )
57+ }
58+ return analyzeInternal ( css , options , false )
59+ }
60+
61+ function analyzeInternal < T extends boolean > ( css : string , options : Options , useLocations : T ) {
5862 let start = Date . now ( )
5963
6064 /**
@@ -77,8 +81,14 @@ export function analyze(css: string, options: Options = {}) {
7781 let embedSize = 0
7882 let embedTypes = {
7983 total : 0 ,
80- /** @type {Map<string, { size: number, count: number } & ({ uniqueWithLocations?: undefined } | ({ uniqueWithLocations: { offset: number, line: number, column: number, length: number }[] })) }>} */
81- unique : new Map ( ) ,
84+ unique : new Map ( ) as Map <
85+ string ,
86+ {
87+ size : number
88+ count : number
89+ uniqueWithLocations ?: { offset : number ; line : number ; column : number ; length : number } [ ]
90+ }
91+ > ,
8292 }
8393
8494 let startParse = Date . now ( )
@@ -430,11 +440,11 @@ export function analyze(css: string, options: Options = {}) {
430440 }
431441
432442 if ( embedTypes . unique . has ( type ) ) {
433- let item = embedTypes . unique . get ( type )
443+ let item = embedTypes . unique . get ( type ) !
434444 item . count ++
435445 item . size += size
436446 embedTypes . unique . set ( type , item )
437- if ( useLocations ) {
447+ if ( useLocations && item . uniqueWithLocations ) {
438448 item . uniqueWithLocations . push ( loc )
439449 }
440450 } else {
0 commit comments