All files / app/address/googleAutocomplete AddressSelector.ts

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55    26x             20x   10x 10x     26x 3x     26x 1x     26x 2x     26x 5x         26x 5x     26x 3x     26x       125x   19x 16x     3x   26x  
import { GoogleAddressFieldType } from './googleAutocompleteTypes';
 
export default class AddressSelector {
    protected _address: google.maps.GeocoderAddressComponent[] | undefined;
    protected _name: string;
 
    constructor(
        googlePlace: google.maps.places.PlaceResult
    ) {
        const { address_components, name } = googlePlace;
 
        this._name = name;
        this._address = address_components;
    }
 
    getState(): string {
        return this._get('administrative_area_level_1', 'short_name');
    }
 
    getStreet(): string {
        return this._name;
    }
 
    getStreet2(): string {
        return '';
    }
 
    getCity(): string {
        return this._get('postal_town', 'long_name') ||
            this._get('locality', 'long_name') ||
            this._get('neighborhood', 'short_name');
    }
 
    getCountry(): string {
        return this._get('country', 'short_name');
    }
 
    getPostCode(): string {
        return this._get('postal_code', 'short_name');
    }
 
    protected _get(
        type: GoogleAddressFieldType,
        access: Exclude<keyof google.maps.GeocoderAddressComponent, 'types'>
    ): string {
        const element = this._address && this._address.find(field => field.types.indexOf(type) !== -1);
 
        if (element) {
            return element[access];
        }
 
        return '';
    }
}