From 0977d89ba66c99fdf66e0367477030cbdcbd93d2 Mon Sep 17 00:00:00 2001 From: Stephan Besser Date: Wed, 4 Dec 2024 09:49:44 +0100 Subject: [PATCH] refactor: rename DmsDecimalMinFormat to DmHemiFormat and update related tests and parser --- ...ecimal-min-format.ts => dm-hemi-format.ts} | 6 ++--- src/parser.ts | 4 ++-- ...-format.test.ts => dm-hemi-format.test.ts} | 22 +++++++++---------- 3 files changed, 16 insertions(+), 16 deletions(-) rename src/formats/{dms-decimal-min-format.ts => dm-hemi-format.ts} (92%) rename tests/{dms-decmial-min-format.test.ts => dm-hemi-format.test.ts} (70%) diff --git a/src/formats/dms-decimal-min-format.ts b/src/formats/dm-hemi-format.ts similarity index 92% rename from src/formats/dms-decimal-min-format.ts rename to src/formats/dm-hemi-format.ts index bed63c9..bc157ee 100644 --- a/src/formats/dms-decimal-min-format.ts +++ b/src/formats/dm-hemi-format.ts @@ -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: * @@ -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); diff --git a/src/parser.ts b/src/parser.ts index 569f996..f9f7ec2 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -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'; @@ -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) { diff --git a/tests/dms-decmial-min-format.test.ts b/tests/dm-hemi-format.test.ts similarity index 70% rename from tests/dms-decmial-min-format.test.ts rename to tests/dm-hemi-format.test.ts index 8024f4c..7220155 100644 --- a/tests/dms-decmial-min-format.test.ts +++ b/tests/dm-hemi-format.test.ts @@ -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);