-
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 DecimalUnsignedPrefixedHemisphereFormat for parsing unsigne…
…d prefixed hemisphere coordinates and enhance tests
- Loading branch information
Showing
5 changed files
with
400 additions
and
249 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/formats/decimal-unsigned-prefixed-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,55 @@ | ||
import { z } from 'zod'; | ||
import { isNumeric } from '../is-numeric.js'; | ||
import type { Coordinate } from '../types.js'; | ||
import { validateSchema } from '../validate-schema.js'; | ||
import { BaseFormat } from './base-format.js'; | ||
|
||
const REGEX = /^([NS])\s*(-?\d{1,2}(\.\d+)?)\s*[, ]?\s*([EW])\s*(-?\d{1,3}(\.\d+)?)\s*$/; | ||
|
||
/** | ||
* Parses coordinates strings in decimal format with hemisphere notations. Coordinate ordering is | ||
* always latitude, longitude. | ||
* | ||
* Supported formats: | ||
* | ||
* N12,E56 | ||
* N 12.234 E 56.678 | ||
* N 12.234, E 56.678 | ||
* N12.234,E56.678 | ||
* N12.234E56.678 | ||
*/ | ||
export class DecimalUnsignedPrefixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DecimalUnsignedPrefixedHemisphereFormat.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 latitude = match[2]; | ||
const longitude = match[5]; | ||
if (isNumeric(latitude) === false || isNumeric(longitude) === false) { | ||
throw new Error('Invalid coordinate string'); | ||
} | ||
|
||
this.enforceValidLatitude(latitude); | ||
this.enforceValidLongitude(longitude); | ||
const lat = parseFloat(latitude).toFixed(this.precision); | ||
const lon = parseFloat(longitude).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { DecimalUnsignedPrefixedHemisphereFormat } from '../src/formats/decimal-unsigned-prefixed-hemisphere-format.js'; | ||
|
||
describe('canParse', () => { | ||
it('returns true for known formats', () => { | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('N12 E56')).toBe(true); | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('N12,E56')).toBe(true); | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('N 12.234 E 56.678')).toBe(true); | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('N12.234,E56.678')).toBe(true); | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('N12.234E56.678')).toBe(true); | ||
}); | ||
|
||
it('returns false for unknown formats', () => { | ||
expect(DecimalUnsignedPrefixedHemisphereFormat.canParse('1.234 5.678')).toBe(false); | ||
}); | ||
}); | ||
describe('parse', () => { | ||
it("returns the correct latitude and longitude for 'N12 E56'", () => { | ||
const formatParser = new DecimalUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N12 E56'); | ||
expect(result.latitude).toBe(12); | ||
expect(result.longitude).toBe(56); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N12,E56'", () => { | ||
const formatParser = new DecimalUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N12,E56'); | ||
expect(result.latitude).toBe(12); | ||
expect(result.longitude).toBe(56); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N 12.234 E 56.678'", () => { | ||
const formatParser = new DecimalUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 12.234 E 56.678'); | ||
expect(result.latitude).toBe(12.234); | ||
expect(result.longitude).toBe(56.678); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N12.234,E56.678'", () => { | ||
const formatParser = new DecimalUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N12.234,E56.678'); | ||
expect(result.latitude).toBe(12.234); | ||
expect(result.longitude).toBe(56.678); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N12.234E56.678'", () => { | ||
const formatParser = new DecimalUnsignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N12.234E56.678'); | ||
expect(result.latitude).toBe(12.234); | ||
expect(result.longitude).toBe(56.678); | ||
}); | ||
}); |
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
Oops, something went wrong.