Skip to content

Commit

Permalink
feat(experiential): internal code helpers
Browse files Browse the repository at this point in the history
#4 updates to the helper files [ci skip]
  • Loading branch information
Bugs5382 committed Dec 9, 2023
1 parent 8a5a53e commit 4a54b08
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/decorators/enum/delimiters.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @internal */
export enum Delimiters {
Segment,
Field,
Expand Down
1 change: 1 addition & 0 deletions src/decorators/interfaces/node.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @internal */
export interface Node {
name: string;
length: number;
Expand Down
22 changes: 11 additions & 11 deletions src/specification/2.7.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { HL7_MSH_MESSAGE_TYPE } from './generic.js'
import { HL7_SPEC_BASE } from './specification.js'

export const HL7_2_7_MSH_DEFAULT = {
msh: {}
}

/**
* HL7 2.7 MSH Specification
* @since 1.0.0
Expand All @@ -27,12 +23,20 @@ export interface HL7_2_7_MSH {
*/
export class HL7_2_7 extends HL7_SPEC_BASE {

static name: "2.7"
constructor() {
super();
this.name = "2.7"
}

/**
* Check MSH Header Properties for HL7 2.7
* @since 1.0.0
* @param msh
* @return boolean
*/
checkMSH (msh: HL7_2_7_MSH): boolean {
if (typeof msh.msh_9.msh_9_1 === 'undefined' ||
typeof msh.msh_9.msh_9_2 === 'undefined' ||
typeof msh.msh_9.msh_9_3 === 'undefined') {
typeof msh.msh_9.msh_9_2 === 'undefined') {
throw new Error('MSH.9.1 & MSH 9.2 & MSH 9.3 must be defined.')
}

Expand All @@ -44,10 +48,6 @@ export class HL7_2_7 extends HL7_SPEC_BASE {
throw new Error('MSH.9.2 must be 3 characters in length.')
}

if (msh.msh_9.msh_9_3.length !== 7) {
throw new Error('MSH.9.3 must be 7 characters in length.')
}

if (typeof msh.msh_10 === 'undefined') {
throw new Error('MSH.9.10 must be defined.')
}
Expand Down
2 changes: 0 additions & 2 deletions src/specification/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ export interface HL7_MSH_MESSAGE_TYPE {
msh_9_1: 'ACK' | 'ADR' | 'ADT'
/** Trigger Event */
msh_9_2: 'A01'
/** Message Structure */
msh_9_3: 'ADT_A01'
}

/** @internal */
Expand Down
10 changes: 10 additions & 0 deletions src/specification/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ export type MSH = HL7_2_7_MSH

/** @internal */
export interface HL7_SPEC {
/** Name of the HL7 Spec */
name: string;
/** Check the MSH Header for this Specification */
checkMSH: (options: MSH) => boolean
}

/** @internal */
export class HL7_SPEC_BASE implements HL7_SPEC {
/** @internal */
name = ""

/**
* Check MSH Header Properties
* @since 1.0.0
* @param _options
* @return boolean
*/
checkMSH (_options: MSH): boolean {
throw new Error('Not Implemented')
}
Expand Down
32 changes: 23 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,55 @@ export function assertNumber (props: Record<string, number>, name: string, min:
}

/** @internal */
export function isNumber(value: string | number): boolean {
export const isNumber = (value: string | number): boolean => {
value = typeof value === 'string' ? parseInt(value) : value
return isNaN(value) || !Number.isFinite(value);
}

/** @internal */
export const isString = (value: any) => typeof value === 'string';

/** @internal */
export function validIPv4 (ip: string): boolean {
/**
* Check if valid IPv4 Address Format
* @since 1.0.0
* @param ip
* @return boolean
*/
export const validIPv4 = (ip: string): boolean => {
const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/
if (ipv4Regex.test(ip)) {
return ip.split('.').every(part => parseInt(part) <= 255)
}
return false
}

/** @internal */
export function validIPv6 (ip: string): boolean {
/**
* Check if valid IPv6 Address Format
* @since 1.0.0
* @param ip
* @return boolean
*/
export const validIPv6 = (ip: string): boolean => {
const ipv6Regex = /^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i
if (ipv6Regex.test(ip)) {
return ip.split(':').every(part => part.length <= 4)
}
return false
}

export const createData = (date: Date) => {
return `${date.getFullYear()}${pad(date.getMonth() + 1, 2)}${pad(date.getDate(), 2)}${pad(date.getHours(), 2)}${pad(date.getMinutes(), 2)}${pad(date.getSeconds(), 2)}`
}

/**
* Converts a number to a string padded n characters.
* Converts a number to a string-padded n characters.
* From http://stackoverflow.com/questions/10073699/pad-a-number-with-leading-zeros-in-javascript.
* @param n The number to convert to a string.
* @param width The number of characters that should be in the resulting string.
* @param z Optional. The character to use for padding. Defaults to '0'.
* @since 1.0.0
*/
export function pad (n: number, width: number, z: string = '0'): string {
export const pad = (n: number, width: number, z: string = '0'): string => {
const s = n.toString()
return s.length >= width ? s : new Array(width - s.length + 1).join(z) + s
}
Expand All @@ -54,7 +68,7 @@ export function pad (n: number, width: number, z: string = '0'): string {
* @param value The string to escape
* @since 1.0.0
*/
export function escapeForRegExp (value: string): string {
export const escapeForRegExp = (value: string): string => {
return value.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}

Expand All @@ -63,7 +77,7 @@ export function escapeForRegExp (value: string): string {
* @param value
* @since 1.0.0
*/
export function decodeHexString (value: string): string {
export const decodeHexString = (value: string): string => {
const result = new Array(value.length / 2)
for (let i = 0; i < value.length; i += 2) {
result[i / 2] = String.fromCharCode(parseInt(value.slice(i, i + 2), 16))
Expand Down

0 comments on commit 4a54b08

Please sign in to comment.