From 4ff9a94027ee938a4625cb41266593096afd6dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Contreras=20Rubio?= Date: Tue, 5 Dec 2023 13:50:30 +0100 Subject: [PATCH] refactoring types -> schemas -> constants --- src/constants.ts | 18 +++++++++++++++--- src/schemas.ts | 28 +++++++++++++++------------- src/types.ts | 8 +++++--- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index acb4437..8befa8e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -11,6 +11,7 @@ export const CHECKSUM_LENGTH = 2 export const END_FLAG_LENGTH = END_FLAG.length export const MINIMAL_LENGTH = START_FLAG_LENGTH + DELIMITER_LENGTH + CHECKSUM_LENGTH + END_FLAG_LENGTH +// GENERATE ASCII STRING export const CODE_A = 'A'.charCodeAt(0) export const CODE_Z = 'Z'.charCodeAt(0) export const CODE_a = 'a'.charCodeAt(0) @@ -18,9 +19,20 @@ export const CODE_z = 'z'.charCodeAt(0) export const CODE_0 = '0'.charCodeAt(0) export const CODE_9 = '9'.charCodeAt(0) +// GENERATE NUMBERS +export const UINT8_MAX = Uint8Array.from([0b1111_1111])[0] +export const UINT16_MAX = Uint16Array.from([0b1111_1111_1111_1111])[0] +export const UINT32_MAX = Uint32Array.from([0b1111_1111_1111_1111_1111_1111_1111_1111])[0] +// export const UINT64_MAX = Uint64Array.from([0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111])[0] + +export const [INT8_MIN, INT8_MAX] = Int8Array.from([0b1000_0000, 0b0111_1111]) +export const [INT16_MIN, INT16_MAX] = Int16Array.from([0b1000_0000_0000_0000, 0b0111_1111_1111_1111]) +export const [INT32_MIN, INT32_MAX] = Int32Array.from([0b1000_0000_0000_0000_0000_0000_0000_0000, 0b0111_1111_1111_1111_1111_1111_1111_1111]) +// export const [INT64_MIN, INT64_MAX] = Int64Array.from([0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, 0b01111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111]) + +export const MAX_FLOAT = 999999999999999 +export const MIN_FLOAT = -999999999999999 + // PARSER export const MAX_CHARACTERS = 1024 export const MAX_NMEA_CHARACTERS = 82 - -export const MAX_FLOAT = 999999999999999 -export const MIN_FLOAT = -999999999999999 \ No newline at end of file diff --git a/src/schemas.ts b/src/schemas.ts index c897c31..1dfa74a 100644 --- a/src/schemas.ts +++ b/src/schemas.ts @@ -1,5 +1,5 @@ import { z } from 'zod' -import { DELIMITER, END_FLAG, SEPARATOR, START_FLAG } from './constants' +import { DELIMITER, END_FLAG, INT16_MAX, INT16_MIN, INT32_MAX, INT32_MIN, INT8_MAX, INT8_MIN, SEPARATOR, START_FLAG, UINT16_MAX, UINT32_MAX, UINT8_MAX } from './constants' export const StringSchema = z.string() export const StringArraySchema = z.array(StringSchema) @@ -8,17 +8,14 @@ export const NumberSchema = z.number() export const IntegerSchema = NumberSchema.int() export const NaturalSchema = IntegerSchema.positive() -export const Uint8Schema = NaturalSchema.max(0b1111_1111) -export const Uint16Schema = NaturalSchema.max(0b1111_1111_1111_1111) -export const Uint32Schema = NaturalSchema.max(0b1111_1111_1111_1111_1111_1111_1111_1111) -// export const Uint64Schema = NaturalSchema.max(0b11111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111) -const [int8min, int8max] = Int8Array.from([0b1000_0000, 0b0111_1111]) -export const Int8Schema = IntegerSchema.min(int8min).max(int8max) -const [int16min, int16max] = Int16Array.from([0b1000_0000_0000_0000, 0b0111_1111_1111_1111]) -export const Int16Schema = IntegerSchema.min(int16min).max(int16max) -const [int32min, int32max] = Int32Array.from([0b1000_0000_0000_0000_0000_0000_0000_0000, 0b0111_1111_1111_1111_1111_1111_1111_1111]) -export const Int32Schema = IntegerSchema.min(int32min).max(int32max) -// export const Int64Schema = IntegerSchema.min(0b10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000).max(0b01111111_11111111_11111111_11111111_11111111_11111111_11111111_11111111) +export const Uint8Schema = NaturalSchema.max(UINT8_MAX) +export const Uint16Schema = NaturalSchema.max(UINT16_MAX) +export const Uint32Schema = NaturalSchema.max(UINT32_MAX) +// export const Uint64Schema = NaturalSchema.max(UINT64_MAX) +export const Int8Schema = IntegerSchema.min(INT8_MIN).max(INT8_MAX) +export const Int16Schema = IntegerSchema.min(INT16_MIN).max(INT16_MAX) +export const Int32Schema = IntegerSchema.min(INT32_MIN).max(INT32_MAX) +// export const Int64Schema = IntegerSchema.min(INT64_MIN).max(INT64_MAX) export const BigIntegerSchema = z.bigint() export const BigNaturalSchema = BigIntegerSchema.positive() @@ -75,6 +72,12 @@ export const ProtocolSchema = z.object({ export const ProtocolsFileSchema = z.object({ protocols: z.array(ProtocolSchema) }) +export const ProtocolsInputSchema = z.object({ + file: StringSchema.optional(), + content: ProtocolsFileSchema.optional(), + protocols: z.array(ProtocolSchema).optional() +}) + export const StoredSentenceSchema = z.object({ sentence: StringSchema, protocol: z.object({ @@ -92,7 +95,6 @@ export const JSONSchemaInputSchema = z.object({ path: StringSchema.default(__dirname), filename: StringSchema.default('nmea_protocols_schema.json') }) - // SENTENCES export const NMEALikeSchema = StringSchema .startsWith(START_FLAG) diff --git a/src/types.ts b/src/types.ts index 1d07f23..c82b4bc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -5,7 +5,7 @@ import { VersionSchema, JSONSchemaInputSchema, StoredSentenceSchema, StoredSentencesSchema, NMEALikeSchema, NMEAUnparsedSentenceSchema, NMEAPreParsedSentenceSchema, - DataSchema, FieldParsedSchema, NMEASentenceSchema, NMEAUknownSentenceSchema, NMEAKnownSentenceSchema, + DataSchema, FieldParsedSchema, NMEASentenceSchema, NMEAUknownSentenceSchema, NMEAKnownSentenceSchema, ProtocolsInputSchema, } from './schemas' // PROTOCOLS @@ -30,9 +30,11 @@ export type NMEAUknownSentence = z.infer export type NMEAKnownSentence = z.infer export type NMEASentence = z.infer // PARSER +export type ProtocolsInput = z.infer + export interface NMEAParser { - addProtocols(protocol: any): void, + addProtocols(protocols: ProtocolsInput): void, getProtocols(): string[], getSentences(): ParserSentences, parseData(data: string): any[], -} \ No newline at end of file +}