import { Component , OnInit } from '@angular/core' ; import * as stringSimilarity from 'string-similarity' ; import { EventService } from '@core/event.service' ; /**
 * Wildcard component. This route will load if a valid event is returned with a
 *  mistyped/malformed url endpoint after the event
 */ @ Component ( { selector : 'wildcard' , styleUrls : [ './wildcard.component.scss' ] , templateUrl : './wildcard.component.html' } ) export class WildcardComponent implements OnInit { event$ : any ; eventId : string ; productPagesComparator = { 'finite-fault' : 'finite-fault' , 'focal-mechanism' : 'focal-mechanism' , 'ground-failure' : 'ground-failure' , losspager : 'pager' , 'moment-tensor' : 'moment-tensor' , oaf : 'oaf' , origin : 'origin' , shakemap : 'shakemap' } ; products = [ 'executive' , 'map' , 'region-info' , 'tellus' , 'impact' , 'dyfi' , 'technical' , 'waveforms' ] ; suggestionString ; suggestionStringUrl ; urlMatch : string ; constructor ( public eventService ) { } param event buildProductsArray ( event ) { const eventKeys = Object . keys ( event . data . properties . products ) ; const comparatorKeys = Object . keys ( this . productPagesComparator ) ; eventKeys . filter ( product => { if ( comparatorKeys . includes ( product ) ) { this . products . push ( this . productPagesComparator [ product ] ) ; } } ) ; } param url param products returns getMatch ( url , products ) { return stringSimilarity . findBestMatch ( url , products ) ; } /**
     * Calls on the string-similarity package to check the mistyped url
     */ getStringSimilarity ( ) { try { const match = this . getMatch ( this . urlMatch , this . products ) ; if ( match . bestMatch ) { const bestMatch = match . bestMatch ; if ( bestMatch . rating >= 0.5 ) { this . setSuggestion ( bestMatch . target ) ; } else { this . setSuggestion ( null ) ; } } } catch ( e ) { console . log ( e ) ; } } ngOnInit ( ) { this . splitUrl ( ) ; this . eventService . event$ . subscribe ( event => { if ( event . data ) { this . eventId = event . id ; this . buildProductsArray ( event ) ; this . getStringSimilarity ( ) ; } } ) ; } param suggestion setSuggestion ( suggestion ) { if ( ! suggestion || suggestion === null ) { this . suggestionString = null ; this . setSuggestionUrl ( null ) ; } else { if ( suggestion === 'tellus' ) { this . suggestionString = 'tell-us' ; this . setSuggestionUrl ( 'tellus' ) ; } else if ( suggestion === 'executive' ) { this . suggestionString = 'overview' ; this . setSuggestionUrl ( 'executive' ) ; } else { this . suggestionString = suggestion ; this . setSuggestionUrl ( suggestion ) ; } } } param suggestion setSuggestionUrl ( suggestion ) { if ( ! suggestion || suggestion === null ) { this . suggestionStringUrl = null ; } else { this . suggestionStringUrl = `/${ this . eventId }/${ suggestion }` ; } } /**
     * Splits the location and gets the last endpoint after the eventId param in
     *    the url and sets the urlMatch property
     */ splitUrl ( ) { const urlSplit = location . href . split ( '/' ) ; this . urlMatch = urlSplit [ urlSplit . length - 1 ] ; } }