UNPKG

@makakwastaken/ts-edifact

Version:
116 lines 3.91 kB
/** * @author Roman Vottner * @copyright 2020 Roman Vottner * @license Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ValidatorImpl } from './validator'; const DEFAULT_CONFIG = { segmentTerminator: 39, dataElementSeparator: 43, componentDataSeparator: 58, decimalMark: 46, releaseCharacter: 63, lineFeed: 10, carriageReturn: 13, endOfTag: 4, validator: new ValidatorImpl(), }; export class Configuration { config; charset; validator; mergeWithDefault(config) { const conf = { ...DEFAULT_CONFIG }; if (config) { if (config.segmentTerminator) { conf.segmentTerminator = config.segmentTerminator; } if (config.dataElementSeparator) { conf.dataElementSeparator = config.dataElementSeparator; } if (config.componentDataSeparator) { conf.componentDataSeparator = config.componentDataSeparator; } if (config.decimalMark) { conf.decimalMark = config.decimalMark; } if (config.releaseCharacter) { conf.releaseCharacter = config.releaseCharacter; } if (config.lineFeed) { conf.lineFeed = config.lineFeed; } if (config.carriageReturn) { conf.carriageReturn = config.carriageReturn; } if (config.endOfTag) { conf.endOfTag = config.endOfTag; } if (config.validator) { conf.validator = config.validator; } } return conf; } constructor(config) { this.config = this.mergeWithDefault(config); if (config?.charset) { this.charset = config.charset; } else { this.charset = 'UNOA'; } this.validator = this.config.validator; } /** * Return a sorted array containing the code points of the control characters * used by this configuration. */ delimiters() { const compareAndSwap = (array, a, b) => { if (array[a] > array[b]) { array[a] = array[a] ^ array[b]; array[b] = array[a] ^ array[b]; array[a] = array[a] ^ array[b]; } }; const exclude = [ this.config.segmentTerminator, this.config.dataElementSeparator, this.config.componentDataSeparator, this.config.releaseCharacter, ]; // Sort the array of excluded characters using a sorting network. compareAndSwap(exclude, 1, 2); compareAndSwap(exclude, 3, 4); compareAndSwap(exclude, 1, 3); compareAndSwap(exclude, 0, 2); compareAndSwap(exclude, 2, 4); compareAndSwap(exclude, 0, 3); compareAndSwap(exclude, 0, 1); compareAndSwap(exclude, 2, 3); compareAndSwap(exclude, 1, 2); return exclude; } toString() { let result = this.charset; result += String.fromCharCode(this.config.componentDataSeparator, this.config.decimalMark, this.config.releaseCharacter, this.config.segmentTerminator); return result; } updateCharset(charset) { this.charset = charset; } } //# sourceMappingURL=configuration.js.map