| 7 | import { X12_ENCODATION, ASCII_ENCODATION, X12_UNLATCH } from './constants'; |
| 8 | |
| 9 | export class X12Encoder extends C40Encoder { |
| 10 | public getEncodingMode() { |
| 11 | return X12_ENCODATION; |
| 12 | } |
| 13 | |
| 14 | public encode(context: EncoderContext) { |
| 15 | // step C |
| 16 | const buffer = new StringBuilder(); |
| 17 | while (context.hasMoreCharacters()) { |
| 18 | const c = context.getCurrentChar(); |
| 19 | context.pos++; |
| 20 | |
| 21 | this.encodeChar(c, buffer); |
| 22 | |
| 23 | const count = buffer.length(); |
| 24 | if (count % 3 === 0) { |
| 25 | this.writeNextTriplet(context, buffer); |
| 26 | |
| 27 | const newMode = HighLevelEncoder.lookAheadTest( |
| 28 | context.getMessage(), |
| 29 | context.pos, |
| 30 | this.getEncodingMode() |
| 31 | ); |
| 32 | if (newMode !== this.getEncodingMode()) { |
| 33 | // Return to ASCII encodation, which will actually handle latch to new mode |
| 34 | context.signalEncoderChange(ASCII_ENCODATION); |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | this.handleEOD(context, buffer); |
| 40 | } |
| 41 | |
| 42 | encodeChar(c: char, sb: StringBuilder): number { |
| 43 | switch (c) { |
| 44 | case 13: // CR (Carriage return) |
| 45 | sb.append(0o0); |
| 46 | break; |
| 47 | case '*'.charCodeAt(0): |
| 48 | sb.append(0o1); |
| 49 | break; |
| 50 | case '>'.charCodeAt(0): |
| 51 | sb.append(0o2); |
| 52 | break; |
| 53 | case ' '.charCodeAt(0): |
| 54 | sb.append(0o3); |
| 55 | break; |
| 56 | default: |
| 57 | if (c >= '0'.charCodeAt(0) && c <= '9'.charCodeAt(0)) { |
| 58 | sb.append(c - 48 + 4); |
| 59 | } else if (c >= 'A'.charCodeAt(0) && c <= 'Z'.charCodeAt(0)) { |
| 60 | sb.append(c - 65 + 14); |
| 61 | } else { |
| 62 | HighLevelEncoder.illegalCharacter(StringUtils.getCharAt(c)); |
| 63 | } |
| 64 | break; |
| 65 | } |
| 66 | return 1; |
nothing calls this directly
no outgoing calls
no test coverage detected