Skip to content

Commit

Permalink
feat: added date into options
Browse files Browse the repository at this point in the history
* this way it can be set in the class if the user wants to.
* it can be overridden after the class has been created, but why would you? :)
  • Loading branch information
Bugs5382 committed Dec 20, 2023
1 parent 1c77630 commit 7f1399a
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/builder/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Batch extends RootBase {
if (typeof opt.text !== 'undefined' && opt.parsing === true && opt.text !== '') {
this._lines = this.split(opt.text).filter(line => line.startsWith('MSH'))
} else {
this.set('BHS.7', createHL7Date(new Date()))
this.set('BHS.7', createHL7Date(new Date(), this._opt.date))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/builder/fileBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class FileBatch extends RootBase {
if (typeof opt.text !== 'undefined' && opt.parsing === true && opt.text !== '') {
this._lines = this.split(opt.text).filter(line => line.startsWith('MSH'))
} else {
this.set('FHS.7', createHL7Date(new Date()))
this.set('FHS.7', createHL7Date(new Date(), this._opt.date))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/specification/2.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class HL7_2_1 extends HL7_SPEC_BASE {
*/
buildMSH (mshHeader: HL7_2_1_MSH, message: Message): void {
if (typeof mshHeader !== 'undefined') {
message.set('MSH.7', createHL7Date(new Date()))
message.set('MSH.7', createHL7Date(new Date(), message._opt.date))
message.set('MSH.9', mshHeader.msh_9.toString())
// if control ID is blank, then randomize it.
if (typeof mshHeader.msh_10 === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion src/specification/2.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class HL7_2_2 extends HL7_SPEC_BASE {
*/
buildMSH (mshHeader: HL7_2_2_MSH, message: Message): void {
if (typeof mshHeader !== 'undefined') {
message.set('MSH.7', createHL7Date(new Date()))
message.set('MSH.7', createHL7Date(new Date(), message._opt.date))
message.set('MSH.9.1', mshHeader.msh_9_1.toString())
message.set('MSH.9.2', mshHeader.msh_9_2.toString())
// if control ID is blank, then randomize it.
Expand Down
2 changes: 1 addition & 1 deletion src/specification/2.3.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class HL7_2_3_1 extends HL7_SPEC_BASE {
*/
buildMSH (mshHeader: HL7_2_3_1_MSH, message: Message): void {
if (typeof mshHeader !== 'undefined') {
message.set('MSH.7', createHL7Date(new Date()))
message.set('MSH.7', createHL7Date(new Date(), message._opt.date))
message.set('MSH.9.1', mshHeader.msh_9_1.toString())
message.set('MSH.9.2', mshHeader.msh_9_2.toString())
// if control ID is blank, then randomize it.
Expand Down
2 changes: 1 addition & 1 deletion src/specification/2.3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class HL7_2_3 extends HL7_SPEC_BASE {
*/
buildMSH (mshHeader: HL7_2_3_MSH, message: Message): void {
if (typeof mshHeader !== 'undefined') {
message.set('MSH.7', createHL7Date(new Date()))
message.set('MSH.7', createHL7Date(new Date(), message._opt.date))
message.set('MSH.9.1', mshHeader.msh_9_1.toString())
message.set('MSH.9.2', mshHeader.msh_9_2.toString())
// if control ID is blank, then randomize it.
Expand Down
18 changes: 18 additions & 0 deletions src/utils/normalizedBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MSH } from '../specification/specification'
import { ParserPlan } from './parserPlan.js'

const DEFAULT_CLIENT_BUILDER_OPTS = {
date: "14",
newLine: '\r',
parsing: false,
separatorComponent: '^',
Expand All @@ -29,6 +30,11 @@ const DEFAULT_CLIENT_FILE_OPTS = {
* @since 1.0.0
*/
export interface ClientBuilderOptions {
/** The date type for the date field. Usually generated at the time of the class being initialized.
* @since 1.0.0
* @default 14
*/
date?: string
/** At the end of each line, add this as the new line character.
* @since 1.0.0
* @default \r */
Expand Down Expand Up @@ -114,6 +120,10 @@ export function normalizedClientMessageBuilderOptions (raw?: ClientBuilderMessag
throw new Error('newLine must be \r or \n')
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
}

if (props.text === '') {
props.text = `MSH${props.separatorField}${props.separatorComponent}${props.separatorRepetition}${props.separatorEscape}${props.separatorSubComponent}`
} else if (typeof props.text !== 'undefined') {
Expand Down Expand Up @@ -144,6 +154,10 @@ export function normalizedClientBatchBuilderOptions (raw?: ClientBuilderOptions)
throw new Error('newLine must be \r or \n')
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
}

if (props.text === '') {
props.text = `BHS${props.separatorField}${props.separatorComponent}${props.separatorRepetition}${props.separatorEscape}${props.separatorSubComponent}`
} else if (typeof props.text !== 'undefined') {
Expand Down Expand Up @@ -180,6 +194,10 @@ export function normalizedClientFileBuilderOptions (raw?: ClientBuilderFileOptio
throw new Error('You can not have specified a file path and a buffer. Please choose one or the other.')
}

if (props.date !== '8' && props.date !== '12' && props.date !== '14') {
props.date = "14"
}

const regex = /\n/mg
const subst = '\\r'
if (typeof props.fullFilePath !== 'undefined' && typeof props.fileBuffer === 'undefined') {
Expand Down
11 changes: 6 additions & 5 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ export const assertNumber = (props: Record<string, number>, name: string, min: n
* @param date
* @param length
*/
export const createHL7Date = (date: Date, length: '8' | '12' | '14' = '14'): string => {
export const createHL7Date = (date: Date, length?: string): string => {
switch (length) {
case '14':
return `${date.getFullYear()}${padHL7Date(date.getMonth() + 1, 2)}${padHL7Date(date.getDate(), 2)}${padHL7Date(date.getHours(), 2)}${padHL7Date(date.getMinutes(), 2)}${padHL7Date(date.getSeconds(), 2)}`
case '12':
return `${date.getFullYear()}${padHL7Date(date.getMonth() + 1, 2)}${padHL7Date(date.getDate(), 2)}${padHL7Date(date.getHours(), 2)}${padHL7Date(date.getMinutes(), 2)}`
case '8':
return `${date.getFullYear()}${padHL7Date(date.getMonth() + 1, 2)}${padHL7Date(date.getDate(), 2)}`
case '12':
return `${date.getFullYear()}${padHL7Date(date.getMonth() + 1, 2)}${padHL7Date(date.getDate(), 2)}${padHL7Date(date.getHours(), 2)}${padHL7Date(date.getMinutes(), 2)}`
case '14':
default:
return `${date.getFullYear()}${padHL7Date(date.getMonth() + 1, 2)}${padHL7Date(date.getDate(), 2)}${padHL7Date(date.getHours(), 2)}${padHL7Date(date.getMinutes(), 2)}${padHL7Date(date.getSeconds(), 2)}`
}
}

Expand Down

0 comments on commit 7f1399a

Please sign in to comment.