Maintenance scheduled for Thursday, January 27th at 15:00 MST. Expected downtime <1 hour.

Commit ae54be25 authored by Jeremy M Fee's avatar Jeremy M Fee Committed by Edward J Hunter
Browse files

Refactor place component methods into pipes

parent dea61635
This diff is collapsed.
import { DistanceAzimuthPipe } from './distance-azimuth.pipe';
describe('DistanceAzimuthPipe', () => {
let pipe;
let place;
beforeEach(() => {
pipe = new DistanceAzimuthPipe();
place = {
admin1_code: 'NM',
admin1_name: 'New Mexico',
azimuth: -557.3,
country_code: 'US',
country_name: 'United States',
distance: 2.736,
elevation: 1633,
feature_class: 'P',
feature_code: 'PPL',
latitude: 34.97645,
longitude: -104.99111,
name: 'Vegas Junction',
population: 0
};
});
describe('compassWinds', () => {
it('should accept compass wind as input and preserve', () => {
expect(pipe.compassWinds('SSE')).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(pipe.compassWinds(place.azimuth)).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(pipe.compassWinds(162.7)).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(pipe.compassWinds(522.7)).toEqual('SSE');
});
});
});
import { Pipe, PipeTransform } from '@angular/core';
import { Place } from './place';
import { getDistanceAzimuth } from './distance-azimuth';
@Pipe({
name: 'distanceAzimuth'
})
export class DistanceAzimuthPipe implements PipeTransform {
compassWinds(azimuth: string | number): string {
const fullwind = 22.5;
const directions = [
'N',
'NNE',
'NE',
'ENE',
'E',
'ESE',
'SE',
'SSE',
'S',
'SSW',
'SW',
'WSW',
'W',
'WNW',
'NW',
'NNW',
'N'
];
// if direction is already in compass points
if (typeof azimuth === 'string' && directions.indexOf(azimuth) > -1) {
return azimuth;
}
// adjust azimuth if negative
azimuth = +azimuth;
while (azimuth < 0) {
azimuth = azimuth + 360;
}
return directions[Math.round((azimuth % 360) / fullwind)];
}
kmToMi(km: number): number {
return km * 0.621371;
}
round(raw: number, decimals: number): number {
const factor = Math.pow(10, decimals);
return Math.round(raw * factor) / factor;
}
transform(place: Place, referencePlace?: Place): any {
if (!place) {
return null;
}
const distAz = getDistanceAzimuth(place, referencePlace);
const azimuth = distAz.azimuth;
const distance = distAz.distance;
return (
this.round(distance, 1) +
' km ' +
'(' +
this.round(this.kmToMi(distance), 1) +
' mi) ' +
this.compassWinds(azimuth)
);
}
}
import * as calculator from 'azimuth';
import { Place } from './place';
export interface DistanceAzimuth {
azimuth: number;
distance: number;
}
export function getDistanceAzimuth(
place: Place,
referencePlace?: Place
): DistanceAzimuth {
let azimuth = +place.azimuth;
let distance = place.distance;
if (referencePlace) {
const distAz = calculator.azimuth(
{
elv: 0,
lat: place.latitude,
lng: place.longitude
},
{
elv: 0,
lat: referencePlace.latitude,
lng: referencePlace.longitude
}
);
azimuth = distAz.azimuth;
distance = distAz.distance;
}
return {
azimuth,
distance
};
}
......@@ -9,6 +9,7 @@ import {
import { AdminRegionComponent } from './admin-region/admin-region.component';
import { AuthoritativeRegionComponent } from './authoritative-region/authoritative-region.component';
import { DistanceAzimuthPipe } from './distance-azimuth.pipe';
import { GeoserveOutputComponent } from './geoserve-output.component';
import { ListItemComponent } from './list-item/list-item.component';
import { LocationOutputComponent } from './location-output/location-output.component';
......@@ -20,6 +21,7 @@ import { NeicResponseRegionComponent } from './neic-response-region/neic-respons
import { NoDataComponent } from './no-data/no-data.component';
import { OffshoreRegionComponent } from './offshore-region/offshore-region.component';
import { OverlaysService } from './overlays.service';
import { PlaceNamePipe } from './place-name.pipe';
import { PlacesService } from './places.service';
import { RegionsService } from './regions.service';
import { TectonicSummaryRegionComponent } from './tectonic-summary-region/tectonic-summary-region.component';
......@@ -28,6 +30,7 @@ import { TectonicSummaryRegionComponent } from './tectonic-summary-region/tecton
declarations: [
AdminRegionComponent,
AuthoritativeRegionComponent,
DistanceAzimuthPipe,
GeoserveOutputComponent,
ListItemComponent,
LocationOutputComponent,
......@@ -38,11 +41,13 @@ import { TectonicSummaryRegionComponent } from './tectonic-summary-region/tecton
NeicResponseRegionComponent,
NoDataComponent,
OffshoreRegionComponent,
PlaceNamePipe,
TectonicSummaryRegionComponent
],
exports: [
AdminRegionComponent,
AuthoritativeRegionComponent,
DistanceAzimuthPipe,
GeoserveOutputComponent,
ListItemComponent,
LocationOutputComponent,
......@@ -53,6 +58,7 @@ import { TectonicSummaryRegionComponent } from './tectonic-summary-region/tecton
NeicResponseRegionComponent,
NoDataComponent,
OffshoreRegionComponent,
PlaceNamePipe,
TectonicSummaryRegionComponent
],
imports: [
......
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DistanceAzimuthPipe } from '../distance-azimuth.pipe';
import { NearbyPlaceListComponent } from './nearby-place-list.component';
import { NearbyPlaceComponent } from '../nearby-place/nearby-place.component';
import { NoDataComponent } from '../no-data/no-data.component';
import { PlaceNamePipe } from '../place-name.pipe';
describe('NearbyPlaceListComponent', () => {
let component: NearbyPlaceListComponent;
......@@ -11,9 +13,11 @@ describe('NearbyPlaceListComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
DistanceAzimuthPipe,
NearbyPlaceComponent,
NearbyPlaceListComponent,
NoDataComponent
NoDataComponent,
PlaceNamePipe
]
}).compileComponents();
}));
......
<span class="name">{{ getName(place) }}</span>
<aside class="distance">{{ getDistance(place) }}</aside>
<aside class="population">{{ getPopulation(place) }}</aside>
<span class="name">{{ place | placeName }}</span>
<aside class="distance">{{ place | distanceAzimuth: referencePlace }}</aside>
<aside class="population">Population: {{ place.population || '-' }}</aside>
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DistanceAzimuthPipe } from '../distance-azimuth.pipe';
import { NearbyPlaceComponent } from './nearby-place.component';
import { Place } from '../place';
import { PlaceNamePipe } from '../place-name.pipe';
describe('NearbyPlaceComponent', () => {
let component: NearbyPlaceComponent;
......@@ -10,7 +12,7 @@ describe('NearbyPlaceComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [NearbyPlaceComponent]
declarations: [DistanceAzimuthPipe, NearbyPlaceComponent, PlaceNamePipe]
}).compileComponents();
}));
......@@ -41,27 +43,4 @@ describe('NearbyPlaceComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});
describe('getName', () => {
it('should return the formatted name components', () => {
expect(component.getName(place)).toEqual(
'Vegas Junction, New Mexico, United States'
);
});
});
describe('compassWinds', () => {
it('should accept compass wind as input and preserve', () => {
expect(component.compassWinds('SSE')).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(component.compassWinds(place.azimuth)).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(component.compassWinds(162.7)).toEqual('SSE');
});
it('should calculate correct winds when azimuth is negative', () => {
expect(component.compassWinds(522.7)).toEqual('SSE');
});
});
});
import { Component, Input } from '@angular/core';
import { Component, Input, OnChanges } from '@angular/core';
import { Place } from '../place';
@Component({
......@@ -6,80 +6,14 @@ import { Place } from '../place';
styleUrls: ['./nearby-place.component.css'],
templateUrl: './nearby-place.component.html'
})
export class NearbyPlaceComponent {
export class NearbyPlaceComponent implements OnChanges {
@Input()
place: Place;
constructor() {}
compassWinds(azimuth: string | number): string {
const fullwind = 22.5;
const directions = [
'N',
'NNE',
'NE',
'ENE',
'E',
'ESE',
'SE',
'SSE',
'S',
'SSW',
'SW',
'WSW',
'W',
'WNW',
'NW',
'NNW',
'N'
];
// if direction is already in compass points
if (typeof azimuth === 'string' && directions.indexOf(azimuth) > -1) {
return azimuth;
}
// adjust azimuth if negative
azimuth = +azimuth;
while (azimuth < 0) {
azimuth = azimuth + 360;
}
return directions[Math.round((azimuth % 360) / fullwind)];
}
getDistance(place: Place): string {
const distanceKm = place.distance;
return (
this.round(distanceKm, 1) +
' km ' +
'(' +
this.round(this.kmToMi(distanceKm), 1) +
' mi) ' +
this.compassWinds(place.azimuth)
);
}
getName(place: Place): string {
const placeArr = [place.name, place.admin1_name, place.country_name].filter(
str => !!str
);
return placeArr.join(', ');
}
getPopulation(place: Place): string {
return 'Population: ' + (place.population || '-');
}
kmToMi(km: number): number {
return km * 0.621371;
}
@Input()
referencePlace: Place = null;
round(raw: number, decimals: number): number {
const factor = Math.pow(10, decimals);
constructor() {}
return Math.round(raw * factor) / factor;
}
ngOnChanges() {}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClient, HttpHandler } from '@angular/common/http';
import { PlacesService } from '../places.service';
import { DistanceAzimuthPipe } from '../distance-azimuth.pipe';
import { NearbyPlaceComponent } from '../nearby-place/nearby-place.component';
import { NearbyPlacesComponent } from './nearby-places.component';
import { NoDataComponent } from '../no-data/no-data.component';
import { NearbyPlaceListComponent } from '../nearby-place-list/nearby-place-list.component';
import { PlacesService } from '../places.service';
import { PlaceNamePipe } from '../place-name.pipe';
describe('NearbyPlacesComponent', () => {
let component: NearbyPlacesComponent;
......@@ -15,10 +16,12 @@ describe('NearbyPlacesComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
DistanceAzimuthPipe,
NearbyPlaceComponent,
NearbyPlaceListComponent,
NearbyPlacesComponent,
NoDataComponent
NoDataComponent,
PlaceNamePipe
],
providers: [
HttpClient,
......
import { PlaceNamePipe } from './place-name.pipe';
describe('PlaceNamePipe', () => {
let pipe;
let place;
beforeEach(() => {
pipe = new PlaceNamePipe();
place = {
admin1_code: 'NM',
admin1_name: 'New Mexico',
azimuth: -557.3,
country_code: 'US',
country_name: 'United States',
distance: 2.736,
elevation: 1633,
feature_class: 'P',
feature_code: 'PPL',
latitude: 34.97645,
longitude: -104.99111,
name: 'Vegas Junction',
population: 0
};
});
it('create an instance', () => {
expect(pipe).toBeTruthy();
});
describe('transform', () => {
it('should return the formatted name components', () => {
expect(pipe.transform(place)).toEqual(
'Vegas Junction, New Mexico, United States'
);
});
});
});
import { Pipe, PipeTransform } from '@angular/core';
import { Place } from './place';
@Pipe({
name: 'placeName'
})
export class PlaceNamePipe implements PipeTransform {
transform(place: Place): any {
if (!place) {
return null;
}
const placeArr = [place.name, place.admin1_name, place.country_name].filter(
str => !!str
);
return placeArr.join(', ');
}
}
......@@ -48,7 +48,14 @@ export class PlacesService {
.subscribe((response: PlacesJson) => {
const places = response.event.features.map(
(feature: Feature): Place => {
return feature.properties;
const place: Place = Object.assign({}, feature.properties);
if (feature.geometry && feature.geometry.coordinates) {
const coords = feature.geometry.coordinates;
place.elevation = coords[2];
place.latitude = coords[1];
place.longitude = coords[0];
}
return place;
}
);
......
......@@ -48,7 +48,7 @@ describe('RegionsService', () => {
});
describe('getRegions', () => {
it('calls http get', () => {
it('calls http get', (done) => {
let latitude, longitude;
const regionsJson = {
......@@ -71,10 +71,11 @@ describe('RegionsService', () => {
regionsService.adminRegions$.subscribe(result => {
expect(result).toEqual(regionsJson.admin.features[0]);
done();
});
});
it('handles errors', () => {
it('handles errors', (done) => {
let latitude, longitude;
latitude = 0;
......@@ -90,6 +91,7 @@ describe('RegionsService', () => {
regionsService.adminRegions$.subscribe(result => {
expect(result).toBe(null);
done();
});
});
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment