UNPKG

@makakwastaken/ts-edifact

Version:
119 lines (118 loc) 4.86 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. */ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ import * as fs from 'node:fs'; import { UNECEMessageStructureParser } from './edi/messageStructureParser'; export function isDefined(value) { return value !== undefined && value !== null; } function stringify(data, pretty) { const ordered = !Array.isArray(data) // Only order if assoc array ? Object.keys(data).sort().reduce((obj, key)=>{ obj[key] = data[key]; return obj; }, {}) : data; if (pretty) { return JSON.stringify(ordered, null, 2); } return JSON.stringify(ordered); } export function persist(data, path, pretty, defaultVersion = false) { const messageStructDef = stringify(data.messageStructureDefinition, pretty); const messageStructDefFileName = `${(defaultVersion ? '' : `${data.version + data.release}_`) + data.messageType}.struct.json`; const segments = stringify(data.segmentTable.entries); const segmentsFileName = `${(defaultVersion ? '' : `${data.version + data.release}_`) + data.messageType}.segments.json`; const components = stringify(data.componentValueTable.entries); const componentsFileName = `${(defaultVersion ? '' : `${data.version + data.release}_`) + data.messageType}.components.json`; let p = path; if (!p.endsWith('/')) { p += '/'; } fs.writeFileSync(p + messageStructDefFileName, messageStructDef); fs.writeFileSync(p + segmentsFileName, segments); fs.writeFileSync(p + componentsFileName, components); } export const formatComponents = (elements, segmentId, decimalSeparator)=>{ const result = {}; result.tag = segmentId; for (const element of elements){ if (element.name === '__proto__' || element.name === 'constructor' || element.name === 'prototype') { continue; } for (const component of element.components){ if (component.name === '__proto__' || component.name === 'constructor' || component.name === 'prototype') { continue; } if (element.components.length <= 1) { result[element.name] = component.value; } else { if (component.value) { if (!result[element.name]) { result[element.name] = {}; } if (typeof result[element.name] === 'object') { // Codes does not have any decimal separators if (decimalSeparator && typeof component.value === 'string') { // If decimal seperator is defined replace instances result[element.name][component.name] = component.value.replace(decimalSeparator, '.'); } else { result[element.name][component.name] = component.value; } } } } } } return Object(result); }; export function storeAllDefaultSpecs(version, location) { const types = [ 'APERAK', 'INVOIC', 'AUTHOR', 'BALANC', 'DESADV', 'GENRAL', 'IFTMIN', 'INVOIC', 'INVRPT', 'ORDERS', 'OSTENQ', 'OSTRPT', 'PARTIN', 'TAXCON', 'VATDEC' ]; // Update INVOIC D07A const structParser = new UNECEMessageStructureParser('d07a', 'INVOIC'); structParser.loadTypeSpec().then((result)=>{ persist(result, location, false, true); }).then(()=>{}).catch((_error)=>{}); // Update all other types for (const typeName of types){ const structParser = new UNECEMessageStructureParser(version, typeName); structParser.loadTypeSpec().then((result)=>{ persist(result, location, false, true); }).then(()=>{}).catch((_error)=>{}); } } export const findElement = (elements, id)=>{ const element = elements?.find((elementEntry)=>elementEntry.id === id); return element; } // Run with: npx ts-node src/util.ts // storeAllDefaultSpecs('d01b', './src/messageSpec') ; //# sourceMappingURL=util.js.map