Skip to content

Commit

Permalink
refactor: simplify IFormatParser interface and enhance options schema…
Browse files Browse the repository at this point in the history
… for format parsers
  • Loading branch information
reskume committed Dec 4, 2024
1 parent 7640437 commit e49e610
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
9 changes: 0 additions & 9 deletions src/formats/base-format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ export const OptionsSchema = z
export type Config = z.infer<typeof OptionsSchema>;

export interface IFormatParser {
precision: number;
longitude: number | undefined;
latitude: number | undefined;

parse(coordinateString: string): Coordinate;
enforceValidLongitude(lonValue: unknown): void;
enforceValidLatitude(latValue: unknown): void;
enforceNoHyphen(coordinateString: string): void;
dmsToDecimal(dms: DmsCoordinate): number;
reset(): void;
}

export class BaseFormat implements IFormatParser {
Expand Down
40 changes: 28 additions & 12 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ import { validateSchema } from './validate-schema.js';
export const OptionsSchema = z
.object({
precision: z.number().int().min(0).max(15),
extendFormatParsers: z.boolean().optional(),
formatParsers: z.array(z.any()).optional(),
})
.strict()
.optional()
.describe('OptionsSchema');
export type Options = z.infer<typeof OptionsSchema>;

export type Options = {
precision: number;
// if true, the given format parsers will be appended to the default parsers instead of replacing them
extendFormatParsers?: boolean;
formatParsers?: IFormatParser[];
};

export class Parser {
originalString: string;
Expand All @@ -28,20 +36,28 @@ export class Parser {
validateSchema(coordinateString, z.string(), { assert: true, name: 'coordinateString' });
validateSchema(options, OptionsSchema, { assert: true, name: 'options' });

const defaultOptions = { precision: 3 };
const opts = { ...defaultOptions, ...options };

const defaultOptions = {
precision: 3,
extendFormatParsers: false,
};
const { precision, extendFormatParsers } = { ...defaultOptions, ...options };
// set default format parsers to use if not provided
const defaultParsers = [
new DecimalFormat({ precision: precision }),
new DecimalHemiFormat({ precision: precision }),
new DecimalSexaFormat({ precision: precision }),
new DecimalSexaHemiFormat({ precision: precision }),
new DmsDecimalMinFormat({ precision: precision }),
];
let formatParsers = options?.formatParsers || defaultParsers;
if (formatParsers.length > 0 && extendFormatParsers === true) {
formatParsers = [...defaultParsers, ...formatParsers];
}
this.originalString = coordinateString;
this.opts = opts;
this.opts = { precision: precision };
this.latitude = undefined;
this.longitude = undefined;
this.parsers = [
new DecimalFormat(this.opts),
new DecimalHemiFormat(this.opts),
new DecimalSexaFormat(this.opts),
new DecimalSexaHemiFormat(this.opts),
new DmsDecimalMinFormat(this.opts),
];
this.parsers = formatParsers;

try {
const { longitude, latitude } = this.parse(coordinateString);
Expand Down

0 comments on commit e49e610

Please sign in to comment.