import { retryCollector , RetryStatistics } from './collector' ; import { retryTimesProp , failedProp , succeededProp } from './constants' ; export type Domain = string [ ] | { [ x ] : string ; } ; export interface DomainMap { [ x ] : string ; } example param { Domain } domains returns { DomainMap } export const prepareDomainMap = function ( domains ) : DomainMap { if ( Array . isArray ( domains ) ) { return domains . reduce ( function ( domainMap , domain , idx , array ) { domainMap [ domain ] = array [ ( idx + 1 ) % array . length ] ; return domainMap ; } , { } as DomainMap ) ; } return domains ; } ; example param { string } src param { string } currentDomain returns { string } export const getUrlPath = function ( src , currentDomain ) { return src . substr ( src . indexOf ( currentDomain ) + currentDomain . length , src . length ) ; } ; param { string } src param { { [ x : string ] : string } } domainMap returns export const getCurrentDomain = function ( src , domainMap ) { return ( Object . keys ( domainMap ) . filter ( function ( domain ) { return src . indexOf ( domain ) > - 1 ; } ) . sort ( ( prev , next ) => next . length - prev . length ) [ 0 ] ) ; } ; param { string } url returns export const extractInfoFromUrl = function ( url , domainMap ) : [ string ? , RetryStatistics ? ] { const [ srcPath , currentDomain ] = splitUrl ( url , domainMap ) ; if ( ! srcPath ) { return [ ] ; } retryCollector [ srcPath ] = retryCollector [ srcPath ] || { [ retryTimesProp ] : 0 , [ failedProp ] : [ ] , [ succeededProp ] : [ ] } ; return [ currentDomain , retryCollector [ srcPath ] ] ; } ; export const splitUrl = function ( url , domainMap ) : [ string , string ] { const currentDomain = getCurrentDomain ( url , domainMap ) ; if ( ! currentDomain ) { return [ '' , '' ] ; } const srcPath = getUrlPath ( url , currentDomain ) ; return [ srcPath , currentDomain ] ; } ;