Skip to content

Commit

Permalink
feat(extract-tz-data): move locationDisplayName into extract-tz-data …
Browse files Browse the repository at this point in the history
…as it's generically useful
  • Loading branch information
vespertilian committed Jun 8, 2018
1 parent 601e704 commit 8356c4d
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const sampleExtractedData: IExtractedTimezoneData = {
geographicArea: 'Australia',
geographicAreaDisplayName: 'Australia',
location: 'Sydney',
locationDisplayName: 'Sydney',
comments: 'New South Wales (most areas)'
}
],
Expand Down
2 changes: 2 additions & 0 deletions src/extract-tz-data/extract-tz-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('extractTzData', () => {
geographicAreaDisplayName: 'Indian Ocean',
geographicArea: 'Indian',
location: 'Test',
locationDisplayName: 'Test',
comments: 'Foo'
};

Expand Down Expand Up @@ -60,6 +61,7 @@ describe('extractTzData', () => {
geographicArea: 'Cameron',
geographicAreaDisplayName: 'Cameron',
location: 'TestA',
locationDisplayName: 'TestA',
comments: null
};

Expand Down
13 changes: 8 additions & 5 deletions src/extract-tz-data/extract-tz-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ import {removeLineBreaks} from '../util/util';
import * as math from 'mathjs'
import {isValidZoneTabRow} from './is-valid-zone-tab-row';
import {extractGeographicAreaAndLocation} from './extract-geographic-area-and-location';
import {ICoordinates, IExtractedTimezoneData} from '../types-for-ts-templates';
import {ICoordinates, IExtractedTimezone, IExtractedTimezoneData} from '../types-for-ts-templates';
import {formatLocation} from './format-location';

export function extractTzData(zoneData: any, zoneFileName: string): IExtractedTimezoneData {
const separator = '\t';
const parsedCSV: string[][] = CSV.parse(zoneData[zoneFileName], separator);

const filteredZoneData = parsedCSV.filter(isValidZoneTabRow);

const zones = filteredZoneData
const zones: IExtractedTimezone[] = filteredZoneData
.map(zoneData => {
const [countryCodes , coordinates, timezoneName, comments] = zoneData;
const allCodes = countryCodes.split(',');
const {geographicArea, location} = extractGeographicAreaAndLocation(timezoneName);

return {
const extractedTimezone: IExtractedTimezone = {
countryCodes: allCodes,
coordinates: parseCoordinates(coordinates),
timezoneName: removeLineBreaks(timezoneName),
geographicArea: geographicArea,
geographicAreaDisplayName: geographicAreaDisplayName(geographicArea),
location: location,
location,
locationDisplayName: formatLocation(location),
comments: comments || null
}
};
return extractedTimezone
});

return {
Expand Down
14 changes: 14 additions & 0 deletions src/extract-tz-data/format-location.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {formatLocation} from './format-location';

describe('formatLocation', () => {
it('should turn a location string into a location object', () => {
expect(formatLocation('Sydney'))
.toEqual('Sydney');

expect(formatLocation('Argentina/Tucuman'))
.toEqual('Argentina - Tucuman');

expect(formatLocation('Cape_Verde'))
.toEqual('Cape Verde');
})
});
6 changes: 6 additions & 0 deletions src/extract-tz-data/format-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function formatLocation(location: string): string {
const displayName = location
.replace('/', ' - ')
.replace('_', ' ');
return displayName
}
42 changes: 18 additions & 24 deletions src/template-tests/group-by-region.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import {
alphabeticGeographicAreaNameSort,
createFiles as groupByRegionCreateFiles,
formatLocation
} from '../../templates/group-by-region/group-by-region'

describe('formatLocation', () => {
it('should turn a location string into a location object', () => {
expect(formatLocation('Sydney'))
.toEqual({location: 'Sydney', displayName: 'Sydney'});

expect(formatLocation('Argentina/Tucuman'))
.toEqual({location: 'Argentina/Tucuman', displayName: 'Argentina - Tucuman'});

expect(formatLocation('Cape_Verde'))
.toEqual({location: 'Cape_Verde', displayName: 'Cape Verde'});
})
});

describe('group-by-region template', () => {
it('should convert the data into the correct files', () => {
const [america, atlantic, australia, europe, indian, pacific, geographicList] = groupByRegionCreateFiles(groupByRegionSampleExtractedData as any)
Expand All @@ -25,26 +11,27 @@ describe('group-by-region template', () => {
const americaValues = JSON.parse(america.json);
const atlanticValues = JSON.parse(atlantic.json);

americaValues.locationList //?
expect(americaValues.locationList).toEqual([
{"location": 'Argentina/Tucuman', "displayName": "Argentina - Tucuman"}
{"location": 'Argentina/Tucuman', "locationDisplayName": "Argentina - Tucuman"}
]);

expect(atlanticValues.locationList).toEqual([
{"location": 'Cape_Verde', "displayName": "Cape Verde"}
{"location": 'Cape_Verde', "locationDisplayName": "Cape Verde"}
]);

expect(australiaValues.locationList).toEqual([
{"location": 'Sydney', "displayName": "Sydney"},
{"location": 'Melbourne', "displayName": "Melbourne"}
{"location": 'Sydney', "locationDisplayName": "Sydney"},
{"location": 'Melbourne', "locationDisplayName": "Melbourne"}
]);

const expectedGeographicList = [
{"geographicAreaName":"America","geographicAreaDisplayName":"America"},
{"geographicAreaName":"Atlantic","geographicAreaDisplayName":"Atlantic Ocean"},
{"geographicAreaName":"Australia","geographicAreaDisplayName":"Australia"},
{"geographicAreaName":"Europe","geographicAreaDisplayName":"Europe"},
{"geographicAreaName":"Indian","geographicAreaDisplayName":"Indian Ocean"},
{"geographicAreaName":"Pacific","geographicAreaDisplayName":"Pacific Ocean"}
{"geographicArea":"America","geographicAreaDisplayName":"America"},
{"geographicArea":"Atlantic","geographicAreaDisplayName":"Atlantic Ocean"},
{"geographicArea":"Australia","geographicAreaDisplayName":"Australia"},
{"geographicArea":"Europe","geographicAreaDisplayName":"Europe"},
{"geographicArea":"Indian","geographicAreaDisplayName":"Indian Ocean"},
{"geographicArea":"Pacific","geographicAreaDisplayName":"Pacific Ocean"}
];

const geographicValues = JSON.parse(geographicList.json);
Expand Down Expand Up @@ -82,6 +69,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: 'Australia',
geographicAreaDisplayName: 'Australia',
location: 'Sydney',
locationDisplayName: 'Sydney',
comments: 'New South Wales (most areas)'
},
{
Expand All @@ -92,6 +80,7 @@ const groupByRegionSampleExtractedData = {
timezoneName: 'Australia/Melbourne',
geographicAreaDisplayName: 'Australia',
location: 'Melbourne',
locationDisplayName: 'Melbourne',
comments: 'Victoria'
},
{
Expand All @@ -102,6 +91,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: 'America',
geographicAreaDisplayName: 'America',
location: 'Argentina/Tucuman',
locationDisplayName: 'Argentina - Tucuman',
comments: 'Tucumán (TM)'
},
{
Expand All @@ -115,6 +105,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: 'Europe',
geographicAreaDisplayName: 'Europe',
location: 'London',
locationDisplayName: 'London',
comments: null
},
{
Expand All @@ -125,6 +116,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: "Indian",
geographicAreaDisplayName: "Indian Ocean",
location: "Cocos",
locationDisplayName: "Cocos",
comments: null
},
{
Expand All @@ -135,6 +127,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: "Atlantic",
geographicAreaDisplayName: "Atlantic Ocean",
location: "Cape_Verde",
locationDisplayName: "Cape Verde",
comments: null
},
{
Expand All @@ -145,6 +138,7 @@ const groupByRegionSampleExtractedData = {
geographicArea: "Pacific",
geographicAreaDisplayName: "Pacific Ocean",
location: "Galapagos",
locationDisplayName: "Galapgaos",
comments: "Galápagos Islands"
},
],
Expand Down
5 changes: 3 additions & 2 deletions src/types-for-ts-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ export interface ICoordinates {
}

export interface IExtractedTimezone {
countryCodes: string[],
countryCodes: string[]
coordinates: ICoordinates
timezoneName: string
geographicArea: string
geographicAreaDisplayName: string,
geographicAreaDisplayName: string
location: string
locationDisplayName: string
comments: string | null
}

Expand Down
46 changes: 11 additions & 35 deletions templates/group-by-region/group-by-region.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,40 @@
import {FileToBeCreated, IExtractedTimezoneData} from '../../src/types-for-ts-templates';

interface GeographicAreaMap {
[geographicAreaName: string]: {
geographicAreaDisplayName: string,
geographicAreaName: string;
locationList: LocationListValue[]
}
}

interface LocationListValue {
location: string,
displayName: string
}

export interface GeographicAreaListValue {
geographicAreaName: string,
geographicAreaDisplayName: string
}
import {GeographicAreaListValue, GeographicAreaMap} from './group-by-region.type';

export function createFiles(extractedTimezoneData: IExtractedTimezoneData): FileToBeCreated[] {
const geographicAreas = extractedTimezoneData.zones.reduce((acc, zone) => {
const {geographicArea, location, geographicAreaDisplayName} = zone;

const _location = formatLocation(location);
const {geographicArea, location, locationDisplayName, geographicAreaDisplayName} = zone;

if(!acc[geographicArea]) {
acc[geographicArea] = {
geographicAreaName: geographicArea,
geographicArea,
geographicAreaDisplayName,
locationList: [_location],
locationList: [{location, locationDisplayName}],
}
} else {
const geoArea = acc[geographicArea];
geoArea.locationList.push(_location);
geoArea.locationList.push({location, locationDisplayName});
}

return acc;
}, {} as GeographicAreaMap);

const areaList: GeographicAreaListValue[] = Object.keys(geographicAreas)
.map(areaKey => {
const {geographicAreaDisplayName, geographicAreaName} = geographicAreas[areaKey];
const {geographicAreaDisplayName, geographicArea} = geographicAreas[areaKey];
const result = {
geographicAreaName,
geographicArea,
geographicAreaDisplayName
};
return result
})
.sort(alphabeticGeographicAreaNameSort);

const files = areaList
.map(({geographicAreaName}) => {
.map(({geographicArea}) => {
return {
fileName: geographicAreaName,
json: JSON.stringify(geographicAreas[geographicAreaName])
fileName: geographicArea,
json: JSON.stringify(geographicAreas[geographicArea])
}
});

Expand All @@ -79,9 +60,4 @@ export function alphabeticGeographicAreaNameSort(a: GeographicAreaListValue, b:
return 0;
}

export function formatLocation(location: string) {
const displayName = location
.replace('/', ' - ')
.replace('_', ' ');
return {location, displayName}
}

17 changes: 17 additions & 0 deletions templates/group-by-region/group-by-region.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface GeographicAreaMap {
[geographicArea: string]: {
geographicAreaDisplayName: string,
geographicArea: string;
locationList: LocationListValue[]
}
}

export interface LocationListValue {
location: string,
locationDisplayName: string
}

export interface GeographicAreaListValue {
geographicArea: string,
geographicAreaDisplayName: string
}

0 comments on commit 8356c4d

Please sign in to comment.