import { CancelablePromise , Timestamps , Progress , Exporter } from "indexer-neon" ; import { PCREToECMARegExp , isRegStrValid } from '../../../tools/tools.regexp' ; import Logger from "../../../tools/env.logger" ; import indexer from "indexer-neon" ; export default class MergeFormat { private _logger : Logger = new Logger ( "MergeFormat" ) ; private _closed : boolean = false ; private _format : string ; private _task : CancelablePromise < void , void , Timestamps . TFormatVerificationAsyncEvents , Timestamps . TFormatVerificationAsyncEventObject > | undefined ; constructor ( format ) { this . _format = format ; } public validate ( flags = { miss_year : false , miss_month : false , miss_day : false } ) { return new Promise ( ( resolve , reject ) => { const measure = this . _logger . measure ( 'Validate format' ) ; let error ; let format = '' ; this . _task = indexer . checkFormat ( this . _format , flags ) . then ( ( ) => { measure ( ) ; if ( error ) { reject ( new Error ( error ) ) ; } resolve ( format ) ; } ) . catch ( ( indxErr ) => { reject ( indxErr ) ; } ) . finally ( ( ) => { this . _task = undefined ; } ) . on ( 'chunk' , ( event ) => { if ( typeof event . FormatInvalid === 'string' && event . FormatInvalid . trim ( ) !== '' ) { error = event . FormatInvalid ; } else if ( typeof event . FormatRegex !== 'string' || event . FormatRegex === '' ) { error = `Fail to get regexp.` ; } else { event . FormatRegex = PCREToECMARegExp ( event . FormatRegex ) ; if ( ! isRegStrValid ( event . FormatRegex ) ) { error = `Fail convert "${ event . FormatRegex }" from PCRE to ECMA regexp.` ; } else { format = event . FormatRegex ; } } } ) . on ( 'progress' , ( event ) => { this . _logger . env ( event ) ; } ) . on ( 'notification' , ( event ) => { this . _logger . env ( event ) ; } ) ; } ) ; } public destroy ( ) { return this . abort ( ) ; } public abort ( ) { return new Promise ( ( resolve ) => { if ( this . _task === undefined ) { return resolve ( ) ; } this . _task . canceled ( ( ) => { resolve ( ) ; } ) . abort ( ) ; } ) ; } }