Skip to content

Commit

Permalink
fix(create-json-from-handlebars-templates extract-tz-data templates):…
Browse files Browse the repository at this point in the history
… update to return an array of c

from 1970 onwards timezones can contain multiple country codes

BREAKING CHANGE: json is now returned with country codes not a single country code
  • Loading branch information
vespertilian committed May 12, 2018
1 parent e30d9a9 commit 4e29558
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('create-json-from-handlebars-template', () => {
createJSONFromHandlebarsTemplatesSpy.calls.argsFor(0)[0];

expect(firstCallParams.handlebarsTemplateFileNames).toContain('all-fields.hbs');
expect(firstCallParams.extractedZoneData.zones[0].countryCode).toEqual('AD');
expect(firstCallParams.extractedZoneData.zones[0].countryCodes[0]).toEqual('AD');
expect(firstCallParams.templatesPath).toContain('templates');
expect(firstCallParams.zoneFileName).toContain('zone1970.tab');
expect(firstCallParams.saveDirectory).toContain('timezones');
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('create-json-from-handlebars-template', () => {
numberOfZones: 1,
zones: [
{
countryCode: "AU",
countryCodes: ["AU", "EU"],
timezone: "Australia/Sydney"
}
]
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('create-json-from-handlebars-template', () => {
expect(error!.message).toContain(`
Could not parse JSON please check your templates.
See timezones/error.txt
Error: SyntaxError: Unexpected token } in JSON at position 152`)
Error: SyntaxError: Unexpected token } in JSON at position 157`);

// writes bade json file to correct error file path
const fileWritePath = writeFileAsyncStub.calls.first().args[0];
Expand All @@ -186,7 +186,7 @@ describe('create-json-from-handlebars-template', () => {

// the comma on timezone should not be there
const expectedErrorText = `{
"countryCode": "AU",
"firstCountryCode": "AU",
"timezone": "Australia/Sydney",
}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
"zones": [
{{#each zones}}
{
"countryCode": "{{countryCode}}",
"firstCountryCode": "{{countryCodes.[0]}}",
"timezone": "{{timezoneName}}",
}{{#unless @last}},{{/unless}}
{{/each}}
]
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const sampleExtractedData: IExtractedTimezoneData = {
numberOfZones: 1,
zones: [
{
countryCode: 'AU',
countryCodes: ['AU', 'EU'],
coordinates: {
latitude: {
negative: true,
Expand Down
11 changes: 10 additions & 1 deletion src/create-json-from-handlebars-templates/test-template.hbs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
{{!-- handlebars inline partials --}}
{{#*inline "countryCodesPartial"}}
[
{{#each this}}
"{{this}}"{{#unless @last}},{{/unless}}
{{/each}}
]
{{/inline}}

{{!-- create json file --}}
{
"numberOfZones": {{numberOfZones}},
"zones": [
{{#each zones}}
{
"countryCode": "{{countryCode}}",
"countryCodes": {{> countryCodesPartial countryCodes}},
"timezone": "{{timezoneName}}"
}{{#unless @last}},{{/unless}}
{{/each}}
Expand Down
4 changes: 2 additions & 2 deletions src/extract-tz-data/extract-tz-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('extractTzData', () => {
expect(result.numberOfZones).toEqual(9);

const extractedTimezone: IExtractedTimezone = {
countryCode: 'BR',
countryCodes: ['BR'],
coordinates: {
latitude: { sign: '+', degree: 2, minute: 49, negative: false, second: null, decimal: 2.816667 },
longitude: { sign: '-', degree: 60, minute: 40, negative: true, second: null, decimal: -60.666667}
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('extractTzData', () => {
expect(result.numberOfZones).toEqual(9);

const extractedTimezone: IExtractedTimezone = {
countryCode: 'JM',
countryCodes: ['JM'],
coordinates: {
latitude: { sign: '+', degree: 17, minute: 58, second: 5, negative: false, decimal: 17.968056 },
longitude: { sign: '-', degree: 76, minute: 47, second: 36, negative: true, decimal: -76.793333 } },
Expand Down
8 changes: 3 additions & 5 deletions src/extract-tz-data/extract-tz-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export interface ICoordinates {
}

export interface IExtractedTimezone {
countryCode: string,
countryCodes: string[],
coordinates: ICoordinates
timezoneName: string
comments: string | null
Expand All @@ -49,9 +49,8 @@ export function extractTzData(zoneData: any, zoneFileName: string): IExtractedTi
.map(zoneData => {
const [countryCodes , coordinates, timezoneName, comments] = zoneData;
const allCodes = countryCodes.split(',');
const firstCode = allCodes[0];
return {
countryCode: removeLineBreaks(firstCode),
countryCodes: allCodes,
coordinates: parseCoordinates(coordinates),
timezoneName: removeLineBreaks(timezoneName),
comments: comments || null
Expand All @@ -70,7 +69,7 @@ function parseCoordinates(coordinates: string): ICoordinates {
const _coordinates = parseTzdataCoordinate(coordinates);
['longitude', 'latitude'].forEach((latlong) => {
// add a negative variable for use with the handlebars templates
const coordinate = _coordinates[latlong]
const coordinate = _coordinates[latlong];
coordinate.negative = Boolean(coordinate.sign === '-');

// always return seconds .. just return null when they are not present
Expand All @@ -84,7 +83,6 @@ function parseCoordinates(coordinates: string): ICoordinates {
return _coordinates
}


function convertToDecimal(coordinate: {sign: string, degree: string, minute: string, second: string | null}) {
// use the math library for more accurate calculations with floating point numbers
const decimalDegrees = math.bignumber(coordinate.degree);
Expand Down
14 changes: 12 additions & 2 deletions templates/all-fields.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{!-- handlebars inline partial --}}
{{!-- handlebars inline partials --}}
{{#*inline "coordinates"}}
{
"sign": "{{sign}}",
Expand All @@ -8,6 +8,16 @@
}
{{/inline}}

{{!-- handlebars inline partials --}}
{{#*inline "countryCodesPartial"}}
[
{{#each this}}
"{{this}}"{{#unless @last}},{{/unless}}
{{/each}}
]
{{/inline}}


{{!-- create json file --}}
{
"IANAVersion": "{{version}}",
Expand All @@ -16,7 +26,7 @@
"zones": [
{{#each zones}}
{
"countryCode": "{{countryCode}}",
"countryCodes": {{> countryCodesPartial countryCodes}},
"timezone": "{{timezoneName}}",
"coordinates": {
"latitude": {{> coordinates coordinates.latitude}},
Expand Down
4 changes: 2 additions & 2 deletions templates/all-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export interface IAllFields {
}

export interface IZones {
countryCode: string
countryCodes: string[]
timezone: string
coordinates: {
latitude: ICoordinate,
latitude: ICoordinate
longitude: ICoordinate
}
comments: string | null
Expand Down
11 changes: 10 additions & 1 deletion templates/decimal-lat-long.hbs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
{{!-- handlebars inline partials --}}
{{#*inline "countryCodesPartial"}}
[
{{#each this}}
"{{this}}"{{#unless @last}},{{/unless}}
{{/each}}
]
{{/inline}}

{
"IANAVersion": "{{version}}",
"originalFileName": "{{originalFileName}}",
"numberOfZones": {{numberOfZones}},
"zones": [
{{#each zones}}
{
"countryCode": "{{countryCode}}",
"countryCodes": {{> countryCodesPartial countryCodes}},
"timezone": "{{timezoneName}}",
"coordinates": {
"latitude": {{coordinates.latitude.decimal}},
Expand Down
2 changes: 1 addition & 1 deletion templates/decimal-lat-long.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface IDecimalLatLong {
}

export interface IZones {
countryCode: string
countryCodes: string[]
timezone: string
coordinates: {
latitude: number
Expand Down

0 comments on commit 4e29558

Please sign in to comment.