Skip to content

Commit

Permalink
refactor: rename DmsDecimalMinFormat to DmHemiFormat and update relat…
Browse files Browse the repository at this point in the history
…ed tests and parser
  • Loading branch information
reskume committed Dec 4, 2024
1 parent e53b04a commit 0977d89
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BaseFormat } from './base-format.js';
const REGEX = /^(\d{3,4}(\.\d+)?\s*[NS])\s*(\d{3,5}(\.\d+)?\s*[EW])$/;

/**
* Parses coordinates strings in DMS format with decimal minutes.
* Parses coordinates strings in DM format with decimal minutes.
*
* Supported formats:
*
Expand All @@ -15,11 +15,11 @@ const REGEX = /^(\d{3,4}(\.\d+)?\s*[NS])\s*(\d{3,5}(\.\d+)?\s*[EW])$/;
* 4007.38 N 7407.38 W
* 4007.38N 7407.38W
*/
export class DmsDecimalMinFormat extends BaseFormat {
export class DmHemiFormat extends BaseFormat {
parse(coordinateString: string): Coordinate {
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });

if (DmsDecimalMinFormat.canParse(coordinateString) === false) {
if (DmHemiFormat.canParse(coordinateString) === false) {
throw new Error('Invalid coordinate string');
}
this.enforceNoHyphen(coordinateString);
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DecimalFormat } from './formats/decimal-format.js';
import { DecimalHemiFormat } from './formats/decimal-hemi-format.js';
import { DecimalSignedFormat } from './formats/decimal-signed-format.js';
import { DecimalSexaHemiFormat } from './formats/decimal-signed-hemi-format.js';
import { DmsDecimalMinFormat } from './formats/dms-decimal-min-format.js';
import { DmHemiFormat } from './formats/dm-hemi-format.js';
import type { Coordinate } from './types.js';
import { validateSchema } from './validate-schema.js';

Expand Down Expand Up @@ -47,7 +47,7 @@ export class Parser {
new DecimalHemiFormat({ precision: precision }),
new DecimalSignedFormat({ precision: precision }),
new DecimalSexaHemiFormat({ precision: precision }),
new DmsDecimalMinFormat({ precision: precision }),
new DmHemiFormat({ precision: precision }),
];
let formatParsers = options?.formatParsers || defaultParsers;
if (formatParsers.length > 0 && extendFormatParsers === true) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import { describe, expect, it } from 'vitest';
import { DmsDecimalMinFormat } from '../src/formats/dms-decimal-min-format.js';
import { DmHemiFormat } from '../src/formats/dm-hemi-format.js';

describe('canParse', () => {
it('returns true for valid decimal format', () => {
expect(DmsDecimalMinFormat.canParse('4007.38N7407.38W')).toBe(true);
expect(DmHemiFormat.canParse('4007.38N7407.38W')).toBe(true);
});

it("returns true for valid decimal format '4007.38N 7407.38W' with space", () => {
expect(DmsDecimalMinFormat.canParse('4007.38N 7407.38W')).toBe(true);
expect(DmHemiFormat.canParse('4007.38N 7407.38W')).toBe(true);
});

it("returns false for invalid decimal format '4007.38N740738W'", () => {
expect(DmsDecimalMinFormat.canParse('4007.38N740738W')).toBe(false);
expect(DmHemiFormat.canParse('4007.38N740738W')).toBe(false);
});

it("returns false for invalid decimal format '4007.38N7407.38P'", () => {
expect(DmsDecimalMinFormat.canParse('4007.38N7407.38P')).toBe(false);
expect(DmHemiFormat.canParse('4007.38N7407.38P')).toBe(false);
});

it("returns false for invalid decimal format '4007.38N7407.38 '", () => {
expect(DmsDecimalMinFormat.canParse('4007.38N7407.38 ')).toBe(false);
expect(DmHemiFormat.canParse('4007.38N7407.38 ')).toBe(false);
});
});
describe('parse', () => {
it("returns the correct latitude and longitude for '4007N 7407W'", () => {
const formatParser = new DmsDecimalMinFormat();
const formatParser = new DmHemiFormat();
const result = formatParser.parse('4007N 7407W');
expect(result.latitude).toBe(40.117);
expect(result.longitude).toBe(-74.117);
});

it("returns the correct latitude and longitude for '4007.38N 7407.38W'", () => {
const formatParser = new DmsDecimalMinFormat();
const formatParser = new DmHemiFormat();
const result = formatParser.parse('4007.38N 7407.38W');
expect(result.latitude).toBe(40.123);
expect(result.longitude).toBe(-74.123);
});

it("returns the correct latitude and longitude for '4007.38N7407.38W'", () => {
const formatParser = new DmsDecimalMinFormat();
const formatParser = new DmHemiFormat();
const result = formatParser.parse('4007.38N7407.38W');
expect(result.latitude).toBe(40.123);
expect(result.longitude).toBe(-74.123);
});

it("returns the correct latitude and longitude for '4007.38 N 7407.38 W'", () => {
const formatParser = new DmsDecimalMinFormat();
const formatParser = new DmHemiFormat();
const result = formatParser.parse('4007.38 N 7407.38 W');
expect(result.latitude).toBe(40.123);
expect(result.longitude).toBe(-74.123);
});

it("returns the correct latitude and longitude for '4007.3812342N 7407.38123W' with precision 4", () => {
const formatParser = new DmsDecimalMinFormat({ precision: 4 });
const formatParser = new DmHemiFormat({ precision: 4 });
const result = formatParser.parse('4007.3812342N 7407.38123W');
expect(result.latitude).toBe(40.1231);
expect(result.longitude).toBe(-74.1231);
Expand Down

0 comments on commit 0977d89

Please sign in to comment.