import { URL } from "url" ; import { YoutubeRawData } from "./types" ; export const getDuration = ( s ) : number => { s = s . replace ( /:/g , "." ) ; const spl = s . split ( "." ) ; if ( spl . length === 0 ) return + spl ; else { const sumStr = spl . pop ( ) as string ; let sum = + sumStr ; if ( spl . length === 1 ) sum += + spl [ 0 ] * 60 ; if ( spl . length === 2 ) { sum += + spl [ 1 ] * 60 ; sum += + spl [ 0 ] * 3600 ; } return sum ; } } ; export const getQueryParameter = ( url , queryName ) : string => { try { return new URL ( url ) . searchParams . get ( queryName ) || url ; } catch ( err ) { return url ; } } ; export const stripToInt = ( string ) : number | null => { if ( ! string ) return null ; return + string . replace ( /[^0-9]/g , "" ) ; } ; export const getContinuationFromItems = ( items , accessors = [ "continuationEndpoint" ] ) : string | undefined => { const continuation = items [ items . length - 1 ] ; const renderer = continuation ?. continuationItemRenderer ; if ( ! renderer ) return ; let current = renderer ; for ( const accessor of accessors ) { current = current [ accessor ] ; } return current . continuationCommand . token ; } ; export const mapFilter = ( items , key ) : YoutubeRawData => { return items . filter ( ( item ) => item [ key ] ) . map ( ( item ) => item [ key ] ) ; } ;