1
- import * as spec from './nacha-spec.json' ;
1
+ /* eslint-disable @typescript-eslint/naming-convention */
2
+ import * as _records from './spec/record-spec.json' ;
3
+ import * as _entries from './spec/entry-spec.json' ;
4
+ import * as _addenda from './spec/addenda-spec.json' ;
5
+
6
+ const entries : any = _entries ;
7
+ const addenda : any = _addenda ;
2
8
3
9
// Reform the spec to be able to index into the correct record via TypeCode
4
- const indexedSpec = Object . entries ( spec ) . reduce ( ( acc : any , [ key , value ] ) => {
10
+ const recordsByTypeCode = Object . entries ( _records ) . reduce ( ( acc : any , [ key , value ] ) => {
5
11
acc [ value . TypeCode ] = {
6
- Id : key ,
7
- ...value // spread operator to keep other properties (Description, Fields, etc.)
12
+ Id : key ,
13
+ ...value // spread operator to keep other properties (Description, Fields, etc.)
8
14
} ;
9
15
return acc ;
10
- } , { } ) ;
16
+ } , { } ) ;
17
+
18
+ export interface Record {
19
+ Id : string ;
20
+ Name : string ;
21
+ TypeCode : string ;
22
+ Description : string ;
23
+ Fields : Field [ ] ;
24
+ Line : string ;
25
+ getField ( position : number ) : SpecField | null ;
26
+ }
11
27
12
- interface Field {
28
+ export interface Field {
13
29
Id : string ;
14
30
Name : string ;
15
31
StartPosition : number ;
16
32
EndPosition : number ;
17
33
Description : string ;
18
34
Value : string ;
35
+ Parent : Record ;
19
36
}
20
37
21
38
class FileControlPaddingField implements Field {
@@ -25,7 +42,9 @@ class FileControlPaddingField implements Field {
25
42
EndPosition : number ;
26
43
Description : string ;
27
44
Value : string ;
28
- constructor ( line : string ) {
45
+ Parent : Record ;
46
+ constructor ( parent : Record , line : string ) {
47
+ this . Parent = parent ;
29
48
this . Id = "FileControlPadding" ;
30
49
this . Name = "File Control Padding" ;
31
50
this . StartPosition = 0 ;
@@ -42,47 +61,71 @@ class SpecField implements Field {
42
61
EndPosition : number ;
43
62
Description : string ;
44
63
Value : string ;
45
- constructor ( parent : Record , fieldId : string ) {
46
- this . Id = fieldId ;
47
- this . Name = indexedSpec [ parent . TypeCode ] . Fields [ fieldId ] . Name ;
48
- this . StartPosition = indexedSpec [ parent . TypeCode ] . Fields [ fieldId ] . StartPosition - 1 ;
49
- this . EndPosition = indexedSpec [ parent . TypeCode ] . Fields [ fieldId ] . EndPosition ;
50
- this . Description = indexedSpec [ parent . TypeCode ] . Fields [ fieldId ] . Description ;
64
+ Parent : Record ;
65
+ constructor ( parent : Record , id : string , spec : any ) {
66
+ this . Parent = parent ;
67
+ this . Id = id ;
68
+ this . Name = spec . Name ;
69
+ this . StartPosition = spec . StartPosition - 1 ;
70
+ this . EndPosition = spec . EndPosition ;
71
+ this . Description = spec . Description ;
51
72
this . Value = parent . Line . substring ( this . StartPosition , this . EndPosition ) ;
52
73
}
53
74
}
54
75
55
- export class Record {
56
- Id : string ;
57
- Name : string ;
58
- TypeCode : string ;
59
- Description : string ;
76
+ class SpecRecord implements Record {
77
+ Id : string = "" ;
78
+ Name : string = "" ;
79
+ TypeCode : string = "" ;
80
+ Description : string = "" ;
60
81
Fields : Field [ ] = [ ] ;
61
- Line : string ;
62
-
63
- constructor ( line : string ) {
64
- this . Line = line ;
65
- this . TypeCode = line . substring ( 0 , 1 ) ;
66
- this . Name = indexedSpec [ this . TypeCode ] . Name ;
67
- this . Id = indexedSpec [ this . TypeCode ] . Id ;
68
- this . Description = indexedSpec [ this . TypeCode ] . Description ;
82
+ Line : string = "" ;
69
83
70
- if ( line . startsWith ( '99999' ) ) {
71
- this . Fields . push ( new FileControlPaddingField ( line ) ) ;
72
- }
73
-
74
- for ( const key in indexedSpec [ this . TypeCode ] . Fields ) {
75
- let field = new SpecField ( this , key ) ;
76
- this . Fields . push ( field ) ;
77
- }
78
- }
79
-
80
- public getField ( position : number ) : SpecField | null {
84
+ public getField ( position : number ) : SpecField | null {
81
85
for ( const field of this . Fields ) {
82
86
if ( position >= field . StartPosition && position <= field . EndPosition ) {
83
87
return field ;
84
88
}
85
89
}
86
90
return null ;
87
91
}
92
+ }
93
+
94
+ export function GetRecord ( line : string , secCode ?: string ) : Record {
95
+ const record = new SpecRecord ( ) ;
96
+ record . Line = line ;
97
+ record . TypeCode = line . substring ( 0 , 1 ) ;
98
+ record . Id = recordsByTypeCode [ record . TypeCode ] . Id ;
99
+ record . Name = recordsByTypeCode [ record . TypeCode ] . Name ;
100
+ record . Description = recordsByTypeCode [ record . TypeCode ] . Description ;
101
+
102
+ switch ( record . TypeCode ) {
103
+ case "6" :
104
+ if ( secCode === undefined || secCode === "" ) {
105
+ throw new Error ( "An SEC code is required to create an entry record." ) ;
106
+ }
107
+ for ( const key in entries [ secCode ] . Fields ) {
108
+ let field = new SpecField ( record , key , entries [ secCode ] . Fields [ key ] ) ;
109
+ record . Fields . push ( field ) ;
110
+ }
111
+ break ;
112
+ case "7" :
113
+ const addendaTypeCode = line . substring ( 1 , 3 ) ;
114
+ for ( const key in addenda [ addendaTypeCode ] . Fields ) {
115
+ let field = new SpecField ( record , key , addenda [ addendaTypeCode ] . Fields [ key ] ) ;
116
+ record . Fields . push ( field ) ;
117
+ }
118
+ break ;
119
+ default :
120
+ if ( line . startsWith ( '99999' ) ) {
121
+ record . Fields . push ( new FileControlPaddingField ( record , line ) ) ;
122
+ }
123
+ for ( const key in recordsByTypeCode [ record . TypeCode ] . Fields ) {
124
+ let field = new SpecField ( record , key , recordsByTypeCode [ record . TypeCode ] . Fields [ key ] ) ;
125
+ record . Fields . push ( field ) ;
126
+ }
127
+ break ;
128
+ }
129
+
130
+ return record ;
88
131
}
0 commit comments