@makakwastaken/ts-edifact
Version:
Edifact parser library
63 lines • 1.87 kB
JavaScript
/**
* @author Stefan Partheymüller
* @copyright 2021 Stefan Partheymüller
* @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 { StateMachine } from '@initics/tsm';
import { Parser } from 'htmlparser2';
import { isDefined } from '../util';
import { UNECEDomHandler } from './uneceDomHandler';
export class UNECEPageParser {
sm;
_spec;
constructor(smdef) {
this.sm = new StateMachine(smdef);
}
get spec() {
if (!this._spec) {
throw new Error('EdifactMessageSpecification not defined');
}
return this._spec;
}
parse(page) {
const parser = new Parser(this.setupHandler());
parser.write(page);
parser.end();
}
setupHandler() {
return new (class extends UNECEDomHandler {
onOpenTag() {
//
}
onText() {
//
}
})();
}
extractTextValue(text, regex, index = 0) {
const arr = regex.exec(text);
if (isDefined(arr)) {
return arr[index];
}
return '';
}
throwInvalidParserState(state) {
throw new Error(`Invalid parser state: ${state}`);
}
throwCouldNotParsePage() {
throw new Error('Could not parse page');
}
}
//# sourceMappingURL=unecePageParser.js.map