| 19 | import HighLevelEncoder from './HighLevelEncoder'; |
| 20 | |
| 21 | export class ASCIIEncoder implements Encoder { |
| 22 | public getEncodingMode() { |
| 23 | return ASCII_ENCODATION; |
| 24 | } |
| 25 | |
| 26 | public encode(context: EncoderContext) { |
| 27 | // step B |
| 28 | const n = HighLevelEncoder.determineConsecutiveDigitCount( |
| 29 | context.getMessage(), |
| 30 | context.pos |
| 31 | ); |
| 32 | if (n >= 2) { |
| 33 | context.writeCodeword( |
| 34 | this.encodeASCIIDigits( |
| 35 | context.getMessage().charCodeAt(context.pos), |
| 36 | context.getMessage().charCodeAt(context.pos + 1) |
| 37 | ) |
| 38 | ); |
| 39 | context.pos += 2; |
| 40 | } else { |
| 41 | const c = context.getCurrentChar(); |
| 42 | const newMode = HighLevelEncoder.lookAheadTest( |
| 43 | context.getMessage(), |
| 44 | context.pos, |
| 45 | this.getEncodingMode() |
| 46 | ); |
| 47 | if (newMode !== this.getEncodingMode()) { |
| 48 | switch (newMode) { |
| 49 | case BASE256_ENCODATION: |
| 50 | context.writeCodeword(LATCH_TO_BASE256); |
| 51 | context.signalEncoderChange(BASE256_ENCODATION); |
| 52 | return; |
| 53 | case C40_ENCODATION: |
| 54 | context.writeCodeword(LATCH_TO_C40); |
| 55 | context.signalEncoderChange(C40_ENCODATION); |
| 56 | return; |
| 57 | case X12_ENCODATION: |
| 58 | context.writeCodeword(LATCH_TO_ANSIX12); |
| 59 | context.signalEncoderChange(X12_ENCODATION); |
| 60 | break; |
| 61 | case TEXT_ENCODATION: |
| 62 | context.writeCodeword(LATCH_TO_TEXT); |
| 63 | context.signalEncoderChange(TEXT_ENCODATION); |
| 64 | break; |
| 65 | case EDIFACT_ENCODATION: |
| 66 | context.writeCodeword(LATCH_TO_EDIFACT); |
| 67 | context.signalEncoderChange(EDIFACT_ENCODATION); |
| 68 | break; |
| 69 | default: |
| 70 | throw new Error('Illegal mode: ' + newMode); |
| 71 | } |
| 72 | } else if (HighLevelEncoder.isExtendedASCII(c)) { |
| 73 | context.writeCodeword(UPPER_SHIFT); |
| 74 | context.writeCodeword(c - 128 + 1); |
| 75 | context.pos++; |
| 76 | } else { |
| 77 | context.writeCodeword(c + 1); |
| 78 | context.pos++; |
nothing calls this directly
no outgoing calls
no test coverage detected