-
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.
refactor: remove redundant tests for unknown formats in coordinate pa…
…rsers and update README with additional examples
- Loading branch information
Showing
19 changed files
with
406 additions
and
59 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { z } from 'zod'; | ||
import type { Coordinate } from '../types.js'; | ||
import { validateSchema } from '../validate-schema.js'; | ||
import { BaseFormat } from './base-format.js'; | ||
|
||
const REGEX = /^([NS])\s*(\d+)°\s*(\d+)'\s*(\d+(?:\.\d+)?)\"\s*,?\s*([EW])\s*(\d+)°\s*(\d+)'\s*(\d+(?:\.\d+)?)\"$/; | ||
|
||
/** | ||
* Parses coordinates strings in DMS signed prefixed hemisphere format. Coordinate ordering is | ||
* always latitude, longitude. | ||
* | ||
* Supported formats: | ||
* | ||
* N40°7'23" W74°7'23" | ||
* N40°7'23", W74°7'23" | ||
* N40°7'23"W74°7'23" | ||
* N 40°7'23.123" W 74°7'23.123" | ||
*/ | ||
export class DmsSignedPrefixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DmsSignedPrefixedHemisphereFormat.canParse(coordinateString) === false) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
// use the regex to parse the latitude and longitude | ||
const match = coordinateString.match(REGEX); | ||
if (match == null) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
const matchLatDegree = match[2]; | ||
const matchLatMinutes = match[3]; | ||
const matchLatSeconds = match[4]; | ||
const matchLatDirection = match[1]; | ||
const matchLonDegree = match[6]; | ||
const matchLonMinutes = match[7]; | ||
const matchLonSeconds = match[8]; | ||
const matchLonDirection = match[5]; | ||
|
||
this.enforceValidLatitudeDegrees(matchLatDegree, false); | ||
this.enforceValidMinutes(matchLatMinutes); | ||
this.enforceValidSeconds(matchLatSeconds); | ||
this.enforceValidLongitudeDegrees(matchLonDegree, false); | ||
this.enforceValidMinutes(matchLonMinutes); | ||
this.enforceValidSeconds(matchLonSeconds); | ||
|
||
const latDegree = Number.parseInt(matchLatDegree); | ||
const latMinutes = Number.parseInt(matchLatMinutes); | ||
const latSeconds = Number.parseFloat(matchLatSeconds); | ||
const lonDegree = Number.parseInt(matchLonDegree); | ||
const lonMinutes = Number.parseInt(matchLonMinutes); | ||
const lonSeconds = Number.parseFloat(matchLonSeconds); | ||
|
||
const decimalLat = this.dmsToDecimal({ | ||
degrees: Math.abs(latDegree), | ||
minutes: latMinutes, | ||
seconds: latSeconds, | ||
direction: matchLatDirection, | ||
}); | ||
const decimalLon = this.dmsToDecimal({ | ||
degrees: Math.abs(lonDegree), | ||
minutes: lonMinutes, | ||
seconds: lonSeconds, | ||
direction: matchLonDirection, | ||
}); | ||
|
||
const lat = decimalLat.toFixed(this.precision); | ||
const lon = decimalLon.toFixed(this.precision); | ||
|
||
return { | ||
latitude: parseFloat(lat), | ||
longitude: parseFloat(lon), | ||
}; | ||
} | ||
|
||
static canParse(coordinateString: string): boolean { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
return REGEX.test(coordinateString); | ||
} | ||
} |
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,81 @@ | ||
import { z } from 'zod'; | ||
import type { Coordinate } from '../types.js'; | ||
import { validateSchema } from '../validate-schema.js'; | ||
import { BaseFormat } from './base-format.js'; | ||
|
||
const REGEX = /^(\d+)°\s*(\d+)'\s*(\d+(?:\.\d+)?)\"\s*([NS])\s*,?\s*(\d+)°\s*(\d+)'\s*(\d+(?:\.\d+)?)\"\s*([EW])$/; | ||
|
||
/** | ||
* Parses coordinates strings in DMS signed suffixed hemisphere format. Coordinate ordering is | ||
* always latitude, longitude. | ||
* | ||
* Supported formats: | ||
* | ||
* 40°7'23"N 74°7'23"W | ||
* 40°7'23"N, 74°7'23"W | ||
* 40°7'23"N74°7'23"W | ||
* 40°7'23.123"N 74°7'23.123"W | ||
*/ | ||
export class DmsSignedSuffixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DmsSignedSuffixedHemisphereFormat.canParse(coordinateString) === false) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
// use the regex to parse the latitude and longitude | ||
const match = coordinateString.match(REGEX); | ||
if (match == null) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
const matchLatDegree = match[1]; | ||
const matchLatMinutes = match[2]; | ||
const matchLatSeconds = match[3]; | ||
const matchLatDirection = match[4]; | ||
const matchLonDegree = match[5]; | ||
const matchLonMinutes = match[6]; | ||
const matchLonSeconds = match[7]; | ||
const matchLonDirection = match[8]; | ||
|
||
this.enforceValidLatitudeDegrees(matchLatDegree, false); | ||
this.enforceValidMinutes(matchLatMinutes); | ||
this.enforceValidSeconds(matchLatSeconds); | ||
this.enforceValidLongitudeDegrees(matchLonDegree, false); | ||
this.enforceValidMinutes(matchLonMinutes); | ||
this.enforceValidSeconds(matchLonSeconds); | ||
|
||
const latDegree = Number.parseInt(matchLatDegree); | ||
const latMinutes = Number.parseInt(matchLatMinutes); | ||
const latSeconds = Number.parseFloat(matchLatSeconds); | ||
const lonDegree = Number.parseInt(matchLonDegree); | ||
const lonMinutes = Number.parseInt(matchLonMinutes); | ||
const lonSeconds = Number.parseFloat(matchLonSeconds); | ||
|
||
const decimalLat = this.dmsToDecimal({ | ||
degrees: Math.abs(latDegree), | ||
minutes: latMinutes, | ||
seconds: latSeconds, | ||
direction: matchLatDirection, | ||
}); | ||
const decimalLon = this.dmsToDecimal({ | ||
degrees: Math.abs(lonDegree), | ||
minutes: lonMinutes, | ||
seconds: lonSeconds, | ||
direction: matchLonDirection, | ||
}); | ||
|
||
const lat = decimalLat.toFixed(this.precision); | ||
const lon = decimalLon.toFixed(this.precision); | ||
|
||
return { | ||
latitude: parseFloat(lat), | ||
longitude: parseFloat(lon), | ||
}; | ||
} | ||
|
||
static canParse(coordinateString: string): boolean { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
return REGEX.test(coordinateString); | ||
} | ||
} |
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
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
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
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
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
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,65 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { DmsSignedPrefixedHemisphereFormat } from '../src/formats/dms-signed-prefixed-hemisphere-format.js'; | ||
|
||
describe('canParse', () => { | ||
it('returns true for known formats', () => { | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N40°7'23" W74°7'23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N40°7'23"W74°7'23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N 40° 7' 23" W 74° 7' 23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N40°7'23", W74°7'23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N40°7'23",W74°7'23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N40° 7' 23", W74° 7' 23"`)).toBe(true); | ||
expect(DmsSignedPrefixedHemisphereFormat.canParse(`N 40° 7' 23.9999", W 74° 7' 23.9999"`)).toBe(true); | ||
}); | ||
}); | ||
describe('parse', () => { | ||
it(`returns the correct latitude and longitude for N40°7'23" W74°7'23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N40°7'23" W74°7'23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N40°7'23"W74°7'23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N40°7'23"W74°7'23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N 40° 7' 23" W 74° 7' 23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N 40° 7' 23" W 74° 7' 23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N40°7'23", W74°7'23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N40°7'23", W74°7'23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N40°7'23",W74°7'23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N40°7'23",W74°7'23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N40° 7' 23", W74° 7' 23"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse(`N40° 7' 23", W74° 7' 23"`); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it(`returns the correct latitude and longitude for N 40° 7' 23.9999", W 74° 7' 23.9999"`, () => { | ||
const formatParser = new DmsSignedPrefixedHemisphereFormat({ precision: 5 }); | ||
const result = formatParser.parse(`N 40° 7' 23.9999", W 74° 7' 23.9999"`); | ||
|
||
expect(result.latitude).toBe(40.12333); | ||
expect(result.longitude).toBe(-74.12333); | ||
}); | ||
}); |
Oops, something went wrong.