@@ -3,17 +3,12 @@ import { REMAIN_WORD_PREFIX } from '@db/common/constant'
33import { log } from '@src/common/logger'
44import { escapeRegExp } from '@util/pattern'
55import { isNotZeroResult } from '@util/stat'
6- import { cvtGroupId2Host , formatDateStr , GROUP_PREFIX , increase } from './common'
7- import { filterRow , processCondition } from './filter '
6+ import { cvtGroupId2Host , formatDateStr , GROUP_PREFIX , increase , zeroResult } from './common'
7+ import { filterDate , filterHost , filterNumberRange , processCondition , type ProcessedCondition } from './condition '
88import type { StatCondition , StatDatabase } from './types'
99
10- function createZeroResult ( ) : timer . core . Result {
11- return { focus : 0 , time : 0 }
12- }
13-
14- function mergeMigration ( exist : timer . core . Result | undefined , another : any ) {
15- exist = exist || createZeroResult ( )
16- return increase ( exist , { focus : another . focus ?? 0 , time : another . time ?? 0 , run : another . run ?? 0 } )
10+ function mergeMigration ( exist : timer . core . Result | undefined , another : any ) : timer . core . Result {
11+ return increase ( { focus : another . focus ?? 0 , time : another . time ?? 0 , run : another . run ?? 0 } , exist ?? zeroResult ( ) )
1712}
1813
1914/**
@@ -36,11 +31,21 @@ function migrate(exists: { [key: string]: timer.core.Result }, data: any): Recor
3631 if ( typeof value !== "object" ) return
3732 const exist = exists [ key ]
3833 const merged = mergeMigration ( exist , value )
39- merged && isNotZeroResult ( merged ) && ( result [ key ] = mergeMigration ( exist , value ) )
34+ isNotZeroResult ( merged ) && ( result [ key ] = merged )
4035 } )
4136 return result
4237}
4338
39+ function filterRow ( row : timer . core . Row , condition : ProcessedCondition ) : boolean {
40+ const { host, date, focus, time } = row
41+ const { timeStart, timeEnd, focusStart, focusEnd } = condition
42+
43+ return filterHost ( host , condition )
44+ && filterDate ( date , condition )
45+ && filterNumberRange ( time , [ timeStart , timeEnd ] )
46+ && filterNumberRange ( focus , [ focusStart , focusEnd ] )
47+ }
48+
4449/**
4550 * Default implementation by `chrome.storage.local`
4651 */
@@ -73,10 +78,10 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
7378 }
7479
7580 private async accumulateInner ( key : string , item : timer . core . Result ) : Promise < timer . core . Result > {
76- let exist = await this . storage . getOne < timer . core . Result > ( key )
77- exist = increase ( exist || createZeroResult ( ) , item )
78- await this . setByKey ( key , exist )
79- return exist
81+ const exist = await this . storage . getOne < timer . core . Result > ( key )
82+ const value = increase ( item , exist )
83+ await this . setByKey ( key , value )
84+ return value
8085 }
8186
8287 /**
@@ -99,7 +104,7 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
99104 const afterUpdated : Record < string , timer . core . Result > = { }
100105 Object . entries ( keys ) . forEach ( ( [ host , key ] ) => {
101106 const item = data [ host ]
102- const exist : timer . core . Result = increase ( items [ key ] as timer . core . Result || createZeroResult ( ) , item )
107+ const exist : timer . core . Result = increase ( item , items [ key ] as timer . core . Result )
103108 toUpdate [ key ] = afterUpdated [ host ] = exist
104109 } )
105110 await this . storage . set ( toUpdate )
@@ -127,12 +132,8 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
127132 }
128133 const { focus, time, run } = value as timer . core . Result
129134 const row : timer . core . Row = { host, date, focus, time }
130- if ( run !== undefined ) {
131- row . run = run
132- }
133- if ( filterRow ( row , cond ) ) {
134- result . push ( row )
135- }
135+ run !== undefined && ( row . run = run )
136+ filterRow ( row , cond ) && result . push ( row )
136137 } )
137138 return result
138139 }
@@ -159,7 +160,7 @@ export class ClassicStatDatabase extends BaseDatabase implements StatDatabase {
159160 async get ( host : string , date : Date | string ) : Promise < timer . core . Result > {
160161 const key = generateKey ( host , date )
161162 const exist = await this . storage . getOne < timer . core . Result > ( key )
162- return exist || createZeroResult ( )
163+ return exist ?? zeroResult ( )
163164 }
164165
165166 /**
0 commit comments