-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(src / templates): added group-by-region-with-country-codes template
- Loading branch information
1 parent
8a1bcf3
commit 5001994
Showing
38 changed files
with
14,034 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/template-tests/group-by-region-with-country-codes.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import { | ||
alphabeticGeographicAreaNameSort, | ||
createFiles as groupByRegionCreateFiles, | ||
} from '../../templates/group-by-region-with-country-code/group-by-region-with-country-codes' | ||
|
||
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) | ||
|
||
const australiaValues = JSON.parse(australia.json); | ||
const americaValues = JSON.parse(america.json); | ||
const atlanticValues = JSON.parse(atlantic.json); | ||
|
||
expect(americaValues.locationList).toEqual([ | ||
{"location": 'Argentina/Tucuman', "locationDisplayName": "Argentina - Tucuman", "countryCodes": ["AR"]} | ||
]); | ||
|
||
expect(atlanticValues.locationList).toEqual([ | ||
{"location": 'Cape_Verde', "locationDisplayName": "Cape Verde", "countryCodes": ["CV"]} | ||
]); | ||
|
||
expect(australiaValues.locationList).toEqual([ | ||
{"location": 'Sydney', "locationDisplayName": "Sydney", "countryCodes": ["AU"]}, | ||
{"location": 'Melbourne', "locationDisplayName": "Melbourne", "countryCodes": ["AU"]} | ||
]); | ||
|
||
const expectedGeographicList = [ | ||
{"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); | ||
expect(geographicValues).toEqual(expectedGeographicList); | ||
}) | ||
}); | ||
|
||
describe('alphabeticGeographicAreaNameSort', () => { | ||
it('should return -1 when name a is before name b', () => { | ||
const a = {geographicAreaDisplayName: 'a'} as any; | ||
const b = {geographicAreaDisplayName: 'b'} as any; | ||
expect(alphabeticGeographicAreaNameSort(a, b)).toEqual(-1) | ||
}); | ||
|
||
it('should return 1 when name b is before name a', () => { | ||
const a = {geographicAreaDisplayName: 'd'} as any; | ||
const b = {geographicAreaDisplayName: 'c'} as any; | ||
expect(alphabeticGeographicAreaNameSort(a, b)).toEqual(1) | ||
}); | ||
|
||
it('should return 0 when the names are equal', () => { | ||
const a = {geographicAreaDisplayName: 'e'} as any; | ||
const b = {geographicAreaDisplayName: 'e'} as any; | ||
expect(alphabeticGeographicAreaNameSort(a, b)).toEqual(0) | ||
}) | ||
}); | ||
|
||
const groupByRegionSampleExtractedData = { | ||
version: 'sample-data-1', | ||
numberOfZones: 1, | ||
zones: [ | ||
{ | ||
countryCodes: ['AU'], | ||
timezoneName: 'Australia/Sydney', | ||
geographicArea: 'Australia', | ||
geographicAreaDisplayName: 'Australia', | ||
location: 'Sydney', | ||
locationDisplayName: 'Sydney', | ||
comments: 'New South Wales (most areas)' | ||
}, | ||
{ | ||
countryCodes: [ | ||
'AU' | ||
], | ||
geographicArea: 'Australia', | ||
timezoneName: 'Australia/Melbourne', | ||
geographicAreaDisplayName: 'Australia', | ||
location: 'Melbourne', | ||
locationDisplayName: 'Melbourne', | ||
comments: 'Victoria' | ||
}, | ||
{ | ||
countryCodes: [ | ||
'AR' | ||
], | ||
timezoneName: 'America/Argentina/Tucuman', | ||
geographicArea: 'America', | ||
geographicAreaDisplayName: 'America', | ||
location: 'Argentina/Tucuman', | ||
locationDisplayName: 'Argentina - Tucuman', | ||
comments: 'Tucumán (TM)' | ||
}, | ||
{ | ||
countryCodes: [ | ||
'GB', | ||
'GG', | ||
'IM', | ||
'JE' | ||
], | ||
timezoneName: 'Europe/London', | ||
geographicArea: 'Europe', | ||
geographicAreaDisplayName: 'Europe', | ||
location: 'London', | ||
locationDisplayName: 'London', | ||
comments: null | ||
}, | ||
{ | ||
countryCodes: [ | ||
"CC" | ||
], | ||
timezoneName: "Indian/Cocos", | ||
geographicArea: "Indian", | ||
geographicAreaDisplayName: "Indian Ocean", | ||
location: "Cocos", | ||
locationDisplayName: "Cocos", | ||
comments: null | ||
}, | ||
{ | ||
countryCodes: [ | ||
"CV" | ||
], | ||
timezoneName: "Atlantic/Cape_Verde", | ||
geographicArea: "Atlantic", | ||
geographicAreaDisplayName: "Atlantic Ocean", | ||
location: "Cape_Verde", | ||
locationDisplayName: "Cape Verde", | ||
comments: null | ||
}, | ||
{ | ||
countryCodes: [ | ||
"EC" | ||
], | ||
timezoneName: "Pacific/Galapagos", | ||
geographicArea: "Pacific", | ||
geographicAreaDisplayName: "Pacific Ocean", | ||
location: "Galapagos", | ||
locationDisplayName: "Galapgaos", | ||
comments: "Galápagos Islands" | ||
}, | ||
], | ||
originalFileName: 'zone-file-foo' | ||
}; |
63 changes: 63 additions & 0 deletions
63
templates/group-by-region-with-country-code/group-by-region-with-country-codes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {FileToBeCreated, IExtractedTimezoneData} from '../../src/types-for-ts-templates'; | ||
import {GeographicAreaListValue, GeographicAreaMap} from './group-by-region-with-country-codes.type'; | ||
|
||
export function createFiles(extractedTimezoneData: IExtractedTimezoneData): FileToBeCreated[] { | ||
const geographicAreas = extractedTimezoneData.zones.reduce((acc, zone) => { | ||
const {geographicArea, location, locationDisplayName, geographicAreaDisplayName, countryCodes} = zone; | ||
|
||
if(!acc[geographicArea]) { | ||
acc[geographicArea] = { | ||
geographicArea, | ||
geographicAreaDisplayName, | ||
locationList: [{location, locationDisplayName, countryCodes}], | ||
} | ||
} else { | ||
const geoArea = acc[geographicArea]; | ||
geoArea.locationList.push({location, locationDisplayName, countryCodes}); | ||
} | ||
|
||
return acc; | ||
}, {} as GeographicAreaMap); | ||
|
||
const areaList: GeographicAreaListValue[] = Object.keys(geographicAreas) | ||
.map(areaKey => { | ||
const {geographicAreaDisplayName, geographicArea} = geographicAreas[areaKey]; | ||
const result = { | ||
geographicArea, | ||
geographicAreaDisplayName, | ||
}; | ||
return result | ||
}) | ||
.sort(alphabeticGeographicAreaNameSort); | ||
|
||
const files = areaList | ||
.map(({geographicArea}) => { | ||
return { | ||
fileName: geographicArea, | ||
json: JSON.stringify(geographicAreas[geographicArea]) | ||
} | ||
}); | ||
|
||
files.push({ | ||
fileName: 'geographic-area-list', | ||
json: JSON.stringify(areaList) | ||
}); | ||
|
||
return files; | ||
} | ||
|
||
export function alphabeticGeographicAreaNameSort(a: GeographicAreaListValue, b: GeographicAreaListValue) { | ||
const nameA = a.geographicAreaDisplayName.toUpperCase(); | ||
const nameB = b.geographicAreaDisplayName.toUpperCase(); | ||
|
||
if(nameA < nameB) { | ||
return -1; | ||
} | ||
|
||
if(nameA > nameB) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
|
18 changes: 18 additions & 0 deletions
18
templates/group-by-region-with-country-code/group-by-region-with-country-codes.type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export interface GeographicAreaMap { | ||
[geographicArea: string]: { | ||
geographicAreaDisplayName: string, | ||
geographicArea: string; | ||
locationList: LocationListValue[], | ||
} | ||
} | ||
|
||
export interface LocationListValue { | ||
location: string, | ||
locationDisplayName: string, | ||
countryCodes: string[] | ||
} | ||
|
||
export interface GeographicAreaListValue { | ||
geographicArea: string, | ||
geographicAreaDisplayName: string | ||
} |
Oops, something went wrong.