import { Element } from "parse5" ; import { getAttributeValue , hasClassName } from "./attributes" ; import { textContent } from "./textContent" ; import { findChildren } from "./findChildren" ; import { isValueClass } from "./nodeMatchers" ; import { ParsingOptions } from "../types" ; interface Options { datetime : boolean ; } const datetimeProp = ( node ) : string | undefined => getAttributeValue ( node , "datetime" ) ; const valueTitle = ( node ) : string | undefined => { if ( hasClassName ( node , "value-title" ) ) { return getAttributeValue ( node , "title" ) ; } return ; } ; const handleDate = ( dateStrings ) : string | undefined => dateStrings . sort ( ( a ) => a . match ( /^[0-9]{4}/ ) ? - 1 : 1 ) . join ( " " ) . trim ( ) . replace ( /((\+|-)[0-2][0-9]):([0-5][0-9])$/ , ( s ) => s . replace ( ":" , "" ) ) . replace ( /([0-2]?[0-9])(:[0-5][0-9])?(:[0-5][0-9])?(a\.?m\.?|p\.?m\.?)/i , ( _s , hour , min , sec , ampm ) => { const isAm = /a/i . test ( ampm ) ; const newHour = isAm ? hour . padStart ( 2 , "0" ) : `${ parseInt ( hour , 10 ) + 12 }` ; return `${ newHour }${ min ? min : ":00" }${ sec || "" }` ; } ) . toUpperCase ( ) ; export const valueClassPattern = ( node , options ) : string | undefined => { const values = findChildren ( node , isValueClass ) ; if ( ! values . length ) { return ; } if ( options . datetime ) { const date = values . map ( ( node ) => datetimeProp ( node ) ?? valueTitle ( node ) ?? textContent ( node , options ) ) ; return handleDate ( date ) ; } return values . map ( ( node ) => valueTitle ( node ) ?? textContent ( node , options ) ) . join ( "" ) . trim ( ) ; } ;