-
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: add DmUnsignedPrefixedHemisphereFormat for parsing DM coordinat…
…es and enhance tests
- Loading branch information
Showing
7 changed files
with
173 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { z } from 'zod'; | ||
import type { Coordinate, DmsCoordinate } from '../types.js'; | ||
import { validateSchema } from '../validate-schema.js'; | ||
import { BaseFormat } from './base-format.js'; | ||
|
||
const REGEX = /^([NS]\s*\d{3,4}(\.\d+)?\s*)\s*([EW]\s*\d{3,5}(\.\d+)?\s*)$/; | ||
|
||
/** | ||
* Parses coordinates strings in DM format with decimal minutes. | ||
* | ||
* Supported formats: | ||
* | ||
* N4007 W7407 | ||
* N4007.38W7407.38 | ||
* N 4007.38 W 7407.38 | ||
* N4007.38 W7407.38 | ||
*/ | ||
export class DmUnsignedPrefixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DmUnsignedPrefixedHemisphereFormat.canParse(coordinateString) === false) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
this.enforceNoHyphen(coordinateString); | ||
// use the regex to parse the latitude and longitude | ||
const match = coordinateString.match(REGEX); | ||
if (match == null) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
const latitude = match[1]; | ||
const longitude = match[3]; | ||
// to DMS | ||
const dmsLat = this.toDms(latitude.replace(/\s/g, '')); | ||
const dmsLon = this.toDms(longitude.replace(/\s/g, '')); | ||
// DMS to decimal | ||
const decimalLat = this.dmsToDecimal(dmsLat); | ||
const decimalLon = this.dmsToDecimal(dmsLon); | ||
this.enforceValidLatitude(decimalLat); | ||
this.enforceValidLongitude(decimalLon); | ||
const lat = decimalLat.toFixed(this.precision); | ||
const lon = decimalLon.toFixed(this.precision); | ||
|
||
return { | ||
latitude: parseFloat(lat), | ||
longitude: parseFloat(lon), | ||
}; | ||
} | ||
|
||
/** | ||
* Converts a DMS notation coordinate like "4007.38N" to DMS parts. | ||
* | ||
* @param {string} value - The parsed DMS value, e.g. "4007.38N" or "74007.38W" | ||
* @return {import('../types').openaip.CoordinateParser.DmsCoordinate} | ||
*/ | ||
toDms(value: string): DmsCoordinate { | ||
validateSchema(value, z.string(), { assert: true, name: 'value' }); | ||
|
||
const match = value.match(/^([NSWE])(\d{2,3})(\d{2})(\.\d+)?$/); | ||
if (match) { | ||
return { | ||
degrees: parseInt(match[2], 10), | ||
minutes: parseInt(match[3], 10), | ||
seconds: Math.round(60 * parseFloat(`0${match[4]}`)), | ||
direction: match[1], | ||
}; | ||
} else { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { DmUnsignedPrefixedHemisphereFormat } from '../src/formats/dm-unsigned-prefixed-hemisphere-format.js'; | ||
|
||
describe('canParse', () => { | ||
it('returns true for known formats', () => { | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('N4007 W7407')).toBe(true); | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('N4007.38W7407.38')).toBe(true); | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('N 4007.38 W 7407.38')).toBe(true); | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('N4007.38 W7407.38')).toBe(true); | ||
}); | ||
|
||
it('returns false for unknown formats', () => { | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('4007.38N740738W')).toBe(false); | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('4007.38N7407.38P')).toBe(false); | ||
expect(DmUnsignedPrefixedHemisphereFormat.canParse('4007.38N7407.38 ')).toBe(false); | ||
}); | ||
}); | ||
describe('parse', () => { | ||
it("returns the correct latitude and longitude for 'N4007 W7407'", () => { | ||
const formatParser = new DmUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N4007 W7407'); | ||
expect(result.latitude).toBe(40.117); | ||
expect(result.longitude).toBe(-74.117); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N4007.38W7407.38'", () => { | ||
const formatParser = new DmUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N4007.38W7407.38'); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N 4007.38 W 7407.38'", () => { | ||
const formatParser = new DmUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 4007.38 W 7407.38'); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N4007.38 W7407.38'", () => { | ||
const formatParser = new DmUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N4007.38 W7407.38'); | ||
expect(result.latitude).toBe(40.123); | ||
expect(result.longitude).toBe(-74.123); | ||
}); | ||
}); |
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