Skip to content

Commit

Permalink
refactor: rename DecimalSexaHemiFormat to DecimalSignedSuffixedHemisp…
Browse files Browse the repository at this point in the history
…hereFormat and DmHemiFormat to DmHemisphereFormat, update related imports and tests
  • Loading branch information
reskume committed Dec 4, 2024
1 parent 0977d89 commit 25d0bf9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const REGEX = /^(-?\d{1,2}(\.\d+)?)\s*(°)\s*([NS])\s*[, ]?\s*(-?\d{1,3}(\.\d+)?
* 1.234°N,5.678°E
* 1.234°N5.678°E
*/
export class DecimalSexaHemiFormat extends BaseFormat {
export class DecimalSignedSuffixedHemisphereFormat extends BaseFormat {
parse(coordinateString: string): Coordinate {
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });

if (DecimalSexaHemiFormat.canParse(coordinateString) === false) {
if (DecimalSignedSuffixedHemisphereFormat.canParse(coordinateString) === false) {
throw new Error('Invalid coordinate string');
}
// use the regex to parse the latitude and longitude
Expand All @@ -47,8 +47,7 @@ export class DecimalSexaHemiFormat extends BaseFormat {
}

static canParse(coordinateString: string): boolean {
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });

validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });

return REGEX.test(coordinateString);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 DmHemiFormat extends BaseFormat {
export class DmHemisphereFormat extends BaseFormat {
parse(coordinateString: string): Coordinate {
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });

if (DmHemiFormat.canParse(coordinateString) === false) {
if (DmHemisphereFormat.canParse(coordinateString) === false) {
throw new Error('Invalid coordinate string');
}
this.enforceNoHyphen(coordinateString);
Expand Down
8 changes: 4 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { IFormatParser } from './formats/base-format.js';
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 { DmHemiFormat } from './formats/dm-hemi-format.js';
import { DecimalSignedSuffixedHemisphereFormat } from './formats/decimal-signed-suffixed-hemisphere-format.js';
import { DmHemisphereFormat } from './formats/dm-hemisphere-format.js';
import type { Coordinate } from './types.js';
import { validateSchema } from './validate-schema.js';

Expand Down Expand Up @@ -46,8 +46,8 @@ export class Parser {
new DecimalFormat({ precision: precision }),
new DecimalHemiFormat({ precision: precision }),
new DecimalSignedFormat({ precision: precision }),
new DecimalSexaHemiFormat({ precision: precision }),
new DmHemiFormat({ precision: precision }),
new DecimalSignedSuffixedHemisphereFormat({ precision: precision }),
new DmHemisphereFormat({ precision: precision }),
];
let formatParsers = options?.formatParsers || defaultParsers;
if (formatParsers.length > 0 && extendFormatParsers === true) {
Expand Down
26 changes: 13 additions & 13 deletions tests/decimal-sexa-hemi-format.test.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import { describe, expect, it } from 'vitest';
import { DecimalSexaHemiFormat } from '../src/formats/decimal-signed-hemi-format.js';
import { DecimalSignedSuffixedHemisphereFormat } from '../src/formats/decimal-signed-suffixed-hemisphere-format.js';

describe('canParse', () => {
it('returns true for valid decimal format', () => {
expect(DecimalSexaHemiFormat.canParse('1.234° N 5.678° E')).toBe(true);
expect(DecimalSignedSuffixedHemisphereFormat.canParse('1.234° N 5.678° E')).toBe(true);
});

it("returns true for valid decimal format '1.234 ° N, 5.678 ° E' with comma", () => {
expect(DecimalSexaHemiFormat.canParse('1.234 ° N, 5.678 ° E')).toBe(true);
expect(DecimalSignedSuffixedHemisphereFormat.canParse('1.234 ° N, 5.678 ° E')).toBe(true);
});

it("returns false for invalid decimal format '1.234 5.678'", () => {
expect(DecimalSexaHemiFormat.canParse('1.234 5.678')).toBe(false);
expect(DecimalSignedSuffixedHemisphereFormat.canParse('1.234 5.678')).toBe(false);
});

it("returns false for invalid decimal format '1.234 N 5.678 P'", () => {
expect(DecimalSexaHemiFormat.canParse('1.234 N 5.678 P')).toBe(false);
expect(DecimalSignedSuffixedHemisphereFormat.canParse('1.234 N 5.678 P')).toBe(false);
});

it("returns false for invalid decimal format '1.234 N 5.678 '", () => {
expect(DecimalSexaHemiFormat.canParse('1.234 N 5.678 ')).toBe(false);
expect(DecimalSignedSuffixedHemisphereFormat.canParse('1.234 N 5.678 ')).toBe(false);
});
});
describe('parse', () => {
it("returns the correct latitude and longitude for '12° N 5° E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('12° N 5° E');
expect(result.latitude).toBe(12);
expect(result.longitude).toBe(5);
});

it("returns the correct latitude and longitude for '1.234° N 5.678° E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('1.234° N 5.678° E');
expect(result.latitude).toBe(1.234);
expect(result.longitude).toBe(5.678);
});

it("returns the correct latitude and longitude for '1.234° N 5.678° E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('1.234° N 5.678° E');
expect(result.latitude).toBe(1.234);
expect(result.longitude).toBe(5.678);
});

it("returns the correct latitude and longitude for '1.234 ° N 5.678 ° E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('1.234 ° N 5.678 ° E');
expect(result.latitude).toBe(1.234);
expect(result.longitude).toBe(5.678);
});

it("returns the correct latitude and longitude for '1.234°N,5.678°E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('1.234°N,5.678°E');
expect(result.latitude).toBe(1.234);
expect(result.longitude).toBe(5.678);
});

it("returns the correct latitude and longitude for '1.234°N5.678°E'", () => {
const formatParser = new DecimalSexaHemiFormat();
const formatParser = new DecimalSignedSuffixedHemisphereFormat();
const result = formatParser.parse('1.234°N5.678°E');
expect(result.latitude).toBe(1.234);
expect(result.longitude).toBe(5.678);
});

it("returns the correct latitude and longitude for '1.23412312°N 5.6782356°E' with precision 4", () => {
const formatParser = new DecimalSexaHemiFormat({ precision: 4 });
const formatParser = new DecimalSignedSuffixedHemisphereFormat({ precision: 4 });
const result = formatParser.parse('1.23412312°N 5.6782356°E');
expect(result.latitude).toBe(1.2341);
expect(result.longitude).toBe(5.6782);
Expand Down
22 changes: 11 additions & 11 deletions tests/dm-hemi-format.test.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
import { describe, expect, it } from 'vitest';
import { DmHemiFormat } from '../src/formats/dm-hemi-format.js';
import { DmHemisphereFormat } from '../src/formats/dm-hemisphere-format.js';

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

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

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

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

it("returns false for invalid decimal format '4007.38N7407.38 '", () => {
expect(DmHemiFormat.canParse('4007.38N7407.38 ')).toBe(false);
expect(DmHemisphereFormat.canParse('4007.38N7407.38 ')).toBe(false);
});
});
describe('parse', () => {
it("returns the correct latitude and longitude for '4007N 7407W'", () => {
const formatParser = new DmHemiFormat();
const formatParser = new DmHemisphereFormat();
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 DmHemiFormat();
const formatParser = new DmHemisphereFormat();
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 DmHemiFormat();
const formatParser = new DmHemisphereFormat();
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 DmHemiFormat();
const formatParser = new DmHemisphereFormat();
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 DmHemiFormat({ precision: 4 });
const formatParser = new DmHemisphereFormat({ 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 25d0bf9

Please sign in to comment.