-
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: update test descriptions for known and unknown formats, and…
… enhance coverage in decimal formats
- Loading branch information
Showing
10 changed files
with
320 additions
and
180 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
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*[, ]?\s*([EW])\s*(-?\d{1,3}(\.\d+)?)\s*(°)\s*$/; | ||
|
||
/** | ||
* Parses coordinates strings in decimal format with sexagesimal/hemisphere notation. Coordinate ordering is | ||
* always latitude, longitude. | ||
* | ||
* Supported formats: | ||
* | ||
* N 12° E 5° | ||
* N 1.234° E 5.678° | ||
* N 1.234°, E 5.678° | ||
* N 1.234°,E5.678° | ||
* N1.234°E5.678° | ||
*/ | ||
export class DecimalSignedPrefixedHemisphereFormat extends BaseFormat { | ||
parse(coordinateString: string): Coordinate { | ||
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' }); | ||
|
||
if (DecimalSignedPrefixedHemisphereFormat.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[6]; | ||
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
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,54 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { DecimalSignedPrefixedHemisphereFormat } from '../src/formats/decimal-signed-prefixed-hemisphere-format.js'; | ||
|
||
describe('canParse', () => { | ||
it('returns true for known formats', () => { | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('N 12° E 5°')).toBe(true); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('N 1.234° E 5.678°')).toBe(true); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('N 1.234°, E 5.678°')).toBe(true); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('N 1.234°,E5.678°')).toBe(true); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('N1.234°E5.678°')).toBe(true); | ||
}); | ||
|
||
it('returns false for unknown formats', () => { | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('1.234 5.678')).toBe(false); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('1.234 N 5.678 P')).toBe(false); | ||
expect(DecimalSignedPrefixedHemisphereFormat.canParse('1.234 N 5.678 ')).toBe(false); | ||
}); | ||
}); | ||
describe('parse', () => { | ||
it("returns the correct latitude and longitude for 'N 12° E 5°'", () => { | ||
const formatParser = new DecimalSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 12° E 5°'); | ||
expect(result.latitude).toBe(12); | ||
expect(result.longitude).toBe(5); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N 1.234° E 5.678°'", () => { | ||
const formatParser = new DecimalSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 1.234° E 5.678°'); | ||
expect(result.latitude).toBe(1.234); | ||
expect(result.longitude).toBe(5.678); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N 1.234°, E 5.678°'", () => { | ||
const formatParser = new DecimalSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 1.234°, E 5.678°'); | ||
expect(result.latitude).toBe(1.234); | ||
expect(result.longitude).toBe(5.678); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N 1.234°,E5.678°'", () => { | ||
const formatParser = new DecimalSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N 1.234°,E5.678°'); | ||
expect(result.latitude).toBe(1.234); | ||
expect(result.longitude).toBe(5.678); | ||
}); | ||
|
||
it("returns the correct latitude and longitude for 'N1.234°E5.678°'", () => { | ||
const formatParser = new DecimalSignedPrefixedHemisphereFormat(); | ||
const result = formatParser.parse('N1.234°E5.678°'); | ||
expect(result.latitude).toBe(1.234); | ||
expect(result.longitude).toBe(5.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
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
Oops, something went wrong.