-
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.
#build-minor: new format parsers added
- Loading branch information
Showing
7 changed files
with
178 additions
and
2 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
72 changes: 72 additions & 0 deletions
72
src/formats/dm-unsigned-delimited-suffixed-hemisphere-format.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,72 @@ | ||
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 = /^(\d+:\d+(\.\d+)?\s*[NS])\s*(\d+:\d+(\.\d+)?\s*[EW])$/; | ||
|
||
/** | ||
* Supported formats: | ||
* | ||
* 40:07N 74:07W | ||
* 40:07.38N74:07.38W | ||
* 40:07.38 N 74:07.38 W | ||
* 40:07.38N 74:07.38W | ||
*/ | ||
export class DmUnsignedDelimitedSuffixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DmUnsignedDelimitedSuffixedHemisphereFormat.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 DM notation coordinate like "4007.38N" to DMS parts. | ||
*/ | ||
toDms(value: string): DmsCoordinate { | ||
validateSchema(value, z.string(), { assert: true, name: 'value' }); | ||
|
||
const match = value.match(/^(\d{2,3}):(\d{2})(\.\d+)?\s*([NSWE])$/); | ||
if (match) { | ||
return { | ||
degrees: parseInt(match[1], 10), | ||
minutes: parseInt(match[2], 10), | ||
seconds: Math.round(60 * parseFloat(`0${match[3]}`)), | ||
direction: match[4], | ||
}; | ||
} 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
40 changes: 40 additions & 0 deletions
40
tests/dm-unsigned-delimited-suffixed-hemisphere-format.test.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,40 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { DmUnsignedDelimitedSuffixedHemisphereFormat } from '../src/formats/dm-unsigned-delimited-suffixed-hemisphere-format'; | ||
|
||
describe('canParse', () => { | ||
it('returns true for known formats', () => { | ||
expect(DmUnsignedDelimitedSuffixedHemisphereFormat.canParse('40:07N 74:07W')).toBe(true); | ||
expect(DmUnsignedDelimitedSuffixedHemisphereFormat.canParse('40:07.38N74:07.38W')).toBe(true); | ||
expect(DmUnsignedDelimitedSuffixedHemisphereFormat.canParse('40:07.38 N 74:07.38 W')).toBe(true); | ||
expect(DmUnsignedDelimitedSuffixedHemisphereFormat.canParse('40:07.38N 74:07.38W')).toBe(true); | ||
}); | ||
}); | ||
describe('parse', () => { | ||
it("returns the correct latitude and longitude for '40:07N 74:07W'", () => { | ||
const formatParser = new DmUnsignedDelimitedSuffixedHemisphereFormat(); | ||
const result = formatParser.parse('40:07N 74:07W'); | ||
expect(result.latitude).toBe(40.11667); | ||
expect(result.longitude).toBe(-74.11667); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for '40:07.38N74:07.38W'", () => { | ||
const formatParser = new DmUnsignedDelimitedSuffixedHemisphereFormat(); | ||
const result = formatParser.parse('40:07.38N74:07.38W'); | ||
expect(result.latitude).toBe(40.12306); | ||
expect(result.longitude).toBe(-74.12306); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for '40:07.38 N 74:07.38 W'", () => { | ||
const formatParser = new DmUnsignedDelimitedSuffixedHemisphereFormat(); | ||
const result = formatParser.parse('40:07.38 N 74:07.38 W'); | ||
expect(result.latitude).toBe(40.12306); | ||
expect(result.longitude).toBe(-74.12306); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for '40:07.38N 74:07.38W'", () => { | ||
const formatParser = new DmUnsignedDelimitedSuffixedHemisphereFormat(); | ||
const result = formatParser.parse('40:07.38N 74:07.38W'); | ||
expect(result.latitude).toBe(40.12306); | ||
expect(result.longitude).toBe(-74.12306); | ||
}); | ||
}); |
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