Skip to content

Commit

Permalink
feat(src / templates): added group-by-region-with-country-codes template
Browse files Browse the repository at this point in the history
  • Loading branch information
vespertilian committed Jul 13, 2019
1 parent 8a1bcf3 commit 5001994
Show file tree
Hide file tree
Showing 38 changed files with 14,034 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe('.createJSONFromTemplatesAndZoneData', () => {

it('should call .createJSONFromHandlebarsTemplates with custom parameters and get files and zone data', async() => {
const createJSONFromHandlebarsTemplatesSpy = jasmine
.createSpy('createJSONFromHandlebarsTemplates');
.createSpy('createJSONFromHandlebarsTemplatesSpy').and.returnValue(Promise.resolve('templateSpyResult'));
const createJSONFromTsFunctionSpy = jasmine
.createSpy('createJSONFromHandlebarsTemplates');
.createSpy('createJSONFromTsFunctionSpy').and.returnValue(Promise.resolve('tsFunctionSpyResult'));

const getIANATzDataStub = jasmine.createSpy('getIANATzDataSpy');

Expand All @@ -55,12 +55,14 @@ describe('.createJSONFromTemplatesAndZoneData', () => {
.createSpy('walkAsyncSpy')
.and.returnValue(new Promise((resolve, reject) => { resolve(['foo.hbs']) }));

const settings = {
templatesPath: 'fooTemplates',
saveDirectory: 'fooSaveDirectory',
zoneFileNames: ['foo.bar']
} as any; // settings

await createJSONFromTemplatesAndZoneData(
{
templatesPath: 'fooTemplates',
saveDirectory: 'fooSaveDirectory',
zoneFileNames: ['foo.bar']
} as any, // settings
settings,
getIANATzDataStub,
createJSONFromHandlebarsTemplatesSpy,
createJSONFromTsFunctionSpy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export async function createJSONFromTemplatesAndZoneData(
const handlebarsTemplateFileNames = allFiles.filter(isHandleBarsFile);
const typescriptFiles = allFiles.filter(isTypescriptFile);

zoneFileNames.forEach(async(zoneFileName) => {
// for loop works with await
for (let index = 0; index < zoneFileNames.length; index++) {
const zoneFileName = zoneFileNames[index];
const extractedZoneData = await _extractTzData(zoneData, zoneFileName);

await _createJSONFromHandlebarsTemplates({
Expand All @@ -55,8 +57,8 @@ export async function createJSONFromTemplatesAndZoneData(
templatesPath,
zoneFileName,
saveDirectory
})
})
});
}
}

function isHandleBarsFile(filename: string) {
Expand Down
145 changes: 145 additions & 0 deletions src/template-tests/group-by-region-with-country-codes.spec.ts
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'
};
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;
}


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
}
Loading

0 comments on commit 5001994

Please sign in to comment.