Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

Commit

Permalink
refactoring types -> schemas -> constants
Browse files Browse the repository at this point in the history
  • Loading branch information
crisconru committed Dec 5, 2023
1 parent 5fd77af commit 4ff9a94
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
18 changes: 15 additions & 3 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,28 @@ 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)
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
28 changes: 15 additions & 13 deletions src/schemas.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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({
Expand All @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,9 +30,11 @@ export type NMEAUknownSentence = z.infer<typeof NMEAUknownSentenceSchema>
export type NMEAKnownSentence = z.infer<typeof NMEAKnownSentenceSchema>
export type NMEASentence = z.infer<typeof NMEASentenceSchema>
// PARSER
export type ProtocolsInput = z.infer<typeof ProtocolsInputSchema>

export interface NMEAParser {
addProtocols(protocol: any): void,
addProtocols(protocols: ProtocolsInput): void,
getProtocols(): string[],
getSentences(): ParserSentences,
parseData(data: string): any[],
}
}

0 comments on commit 4ff9a94

Please sign in to comment.