yogeshs-utilities
Version:
417 lines • 16.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
class CobolHelpers {
static removeAllCommentedLines(allLines, comments) {
const mainBlock = [];
for (const line of allLines) {
const nl = line.modifiedLine.trim();
if (nl === "")
continue;
const isPresent = comments.some(x => nl.startsWith(x));
if (isPresent)
continue;
mainBlock.push(line);
}
return mainBlock;
}
static processBmsMapControls(allLines) {
let map = "";
let mapSet = "";
let nextLoop = 0;
let mapId = 0;
const regexMap = new RegExp("(.*)\\bDFHMSD\\b");
const regexMapSet = new RegExp("(.*)\\bDFHMDI\\b");
let mapMasters = [];
let mapData = {};
for (let i = 0; i < allLines.length; i++) {
const line = allLines[i].modifiedLine;
if (regexMap.test(line)) {
map = regexMap.exec(line)[1];
}
else {
if (regexMapSet.test(line)) {
mapSet = regexMapSet.exec(line)[1];
}
}
if (map === "" || mapSet === "")
continue;
mapData = { map, mapSet, bmsMapControls: [] };
nextLoop = i + 1;
break;
}
const regexControl = new RegExp("(.*)\\bDFHMDF\\b");
const regexPos = new RegExp("\\bPOS\\b=(.*)");
const regexLength = new RegExp("\\bLENGTH\\b=([0-9]+)");
const regexAttribute = new RegExp("\\bATTRB\\b=(.*)");
for (let i = nextLoop; i < allLines.length; i++) {
let controlName = "";
let xPos = "";
let yPos = "";
let length = "";
let color = "";
let initValue = "";
let isPort = "";
let attributeValues = "";
const statement = allLines[i].modifiedLine;
if (!statement)
continue;
if (regexControl.test(statement)) {
let cont = regexControl.exec(statement)[1].replace("'", "\"");
controlName = cont !== "" && cont !== "DFHMDF" ? "textbox" : "label";
if (regexPos.test(statement)) {
const posValue = regexPos.exec(statement)[1].replace(/[()]/g, "").trim();
[xPos, yPos] = posValue.split(',').map(part => part.trim());
}
}
if (controlName === "" || xPos === "" || yPos === "")
continue;
if (regexLength.test(statement))
length = regexLength.exec(statement)[1];
if (statement.includes("COLOR"))
color = statement.replace("COLOR=", "").split(",")[0].trim();
if (statement.includes("INITIAL"))
initValue = statement.replace("INITIAL=", "").replace("'", "").trim();
if (regexAttribute.test(statement)) {
attributeValues = regexAttribute.exec(statement)[1].replace(/[()]/g, "");
isPort = statement.includes("PROT") ? "Y" : "N";
}
if (controlName === "" || xPos === "" || yPos === "" || length === "" || isPort === "")
continue;
let initialValue = initValue.padEnd(parseInt(length));
if (!color)
color = "GRAY";
const mapControl = { controlName, xPos, yPos, length, color, initialValue, isPort, mapId, aAttributes: attributeValues };
mapData.bmsMapControls.push(mapControl);
}
mapMasters.push(mapData);
return mapMasters;
}
static splitLinesAfterDotForCobol(allLines) {
const mainBlockList = [];
const copyRegex = new RegExp("([\\+]+)?COPY\\s*(?<name>[\\w]+)", "i");
allLines.forEach(line => {
if (!copyRegex.test(line.modifiedLine)) {
mainBlockList.push(line);
return;
}
const currentLine = line.modifiedLine.split('.');
currentLine.forEach(cLine => {
if (!cLine)
return;
const nLine = `${cLine}.`;
mainBlockList.push({ originalLine: line.originalLine, modifiedLine: nLine, alternateName: line.alternateName, lineIndex: line.lineIndex, indicators: line.indicators });
});
});
return mainBlockList;
}
// previously function name in C# was getCopyStatementInWorkingStorageSection
static getCopyStatementsFromWorkingStorageSection(allLines) {
if (allLines.length <= 0)
return allLines;
const finalList = [];
allLines.forEach(line => {
const newLine = line.modifiedLine.trim();
const regex = /^\s|COPY\s/;
if (!regex.test(newLine))
return;
finalList.push({ lineIndex: line.lineIndex, originalLine: line.originalLine, modifiedLine: newLine, alternateName: line.alternateName, indicators: line.indicators });
});
return finalList;
}
static splitCopyAndReplacingStatement(allLines) {
const mainBlockList = [];
const cRegex = new RegExp(/^COPY\s+(.+?\s+)REPLACING/i);
const copyRegex = new RegExp(/([\+]+)?COPY\s(?<IncFile>\w+)?(?<statement>.*)/i);
for (const cLine of allLines) {
const line = cLine.modifiedLine.trim();
if (!line)
continue;
if (!cRegex.test(line)) {
mainBlockList.push(cLine);
continue;
}
const match = line.match(copyRegex);
if (!match)
continue;
const copyName = match.groups?.IncFile || '';
const otherPart = match.groups?.statement || '';
if (!copyName)
continue;
const spaceMatch = cLine.modifiedLine.match(/^\s+/);
const numberOfSpaces = spaceMatch ? spaceMatch[0].length : 0;
const newString = ' '.repeat(numberOfSpaces) + 'COPY ' + copyName;
mainBlockList.push({ ...cLine, modifiedLine: newString });
if (!otherPart)
continue;
mainBlockList.push({ ...cLine, modifiedLine: otherPart });
}
return mainBlockList;
}
static extractCursorsFromExecSql(allExecSql) {
const regexCursor = /EXEC\s+SQL\s+DECLARE\s+[A-Za-z0-9\-]+\s+CURSOR/i;
const lstCursor = [];
allExecSql.forEach((execSql) => {
if (!regexCursor.test(execSql.modifiedLine))
return;
const cursor = execSql.modifiedLine.replace(/EXEC\s+SQL/i, "").replace(/END-EXEC\./i, "").replace(/END-EXEC/i, "").trimStart().trimEnd();
const statement = {
lineIndex: execSql.lineIndex, originalLine: cursor, modifiedLine: cursor,
alternateName: execSql.alternateName, indicators: execSql.indicators
};
lstCursor.push(statement);
});
return lstCursor;
}
}
CobolHelpers.prepareCobolLineDetails = function (allLines) {
if (allLines.length === 0)
return [];
let lstStatements = [];
let index = 0;
for (const line of allLines) {
++index;
if (line.trim() === "")
continue;
let lineDetails = {
lineIndex: ++index, originalLine: line, indicators: [],
alternateName: '', modifiedLine: line.replace(/\\r|\\n/ig, "").trim()
};
lstStatements.push(lineDetails);
}
return lstStatements;
};
CobolHelpers.prepareJclSameLength = function (allLines, maxLen = 72, upto = 68) {
if (allLines.length <= 0)
return [];
let maxLength = Math.max(...allLines.map((line) => line.originalLine.length));
if (maxLength <= 72)
maxLength = maxLen;
for (const line of allLines) {
line.modifiedLine = line.originalLine.padEnd(maxLength);
}
for (const line of allLines.filter((d) => d.modifiedLine.length > 70)) {
line.modifiedLine = line.modifiedLine.substring(0, upto);
}
return allLines;
};
CobolHelpers.prepareSameLength = function (allLines) {
let maxLength = Math.max(...allLines.map((s) => s.originalLine.length));
if (maxLength <= 72)
maxLength = 72;
allLines.forEach((cl) => { cl.modifiedLine = cl.modifiedLine.padEnd(maxLength); });
return allLines;
};
CobolHelpers.removeCharacter = function (allLines, startPosition, length) {
const mainLines = [];
for (const ld of allLines) {
ld.modifiedLine = ld.modifiedLine.substring(startPosition, startPosition + length).trimEnd();
if ((0, lodash_1.isEmpty)(ld.modifiedLine))
continue;
mainLines.push(ld);
}
return mainLines;
};
CobolHelpers.removeStartCharacter = function (allLines, ...characters) {
if (allLines.length <= 0)
return [];
for (const cLine of allLines) {
const line = cLine.modifiedLine.trim();
const isPresent = characters.some((x) => line.startsWith(x));
if (!isPresent)
continue;
cLine.modifiedLine = characters.map(x => line.replace(new RegExp(x, "igm"), " ")).join().trim();
}
return allLines;
};
CobolHelpers.removeSpacesBetweenWords = function (allLines, regex = /[\s]{2,}/igm) {
allLines.forEach((line) => { line.modifiedLine = line.modifiedLine.trim().replace(regex, " "); });
return allLines;
};
CobolHelpers.combineAllBrokenLines = function (lineDetails, lineBreakElement, keepOrRemove = true) {
if (lineDetails.length === 0)
return lineDetails;
const StatementMaster = [];
let tempString = '';
let indexPosition = -1;
for (let i = 0; i < lineDetails.length; i++) {
indexPosition++;
let line = lineDetails[i].modifiedLine.trimEnd();
const regex = new RegExp(`[${lineBreakElement}\\s]$`);
if (regex.test(line)) {
if (line.trimStart().trimEnd().startsWith("'"))
continue;
for (let j = i; j < lineDetails.length; j++) {
line = lineDetails[j].modifiedLine.trimEnd();
if (regex.test(line)) {
if (keepOrRemove) {
tempString += line.slice(0, -1).trim() + ' ';
}
else {
tempString += line.trim() + ' ';
}
indexPosition++;
continue;
}
tempString += lineDetails[j].modifiedLine.trim();
lineDetails[j].modifiedLine = tempString;
StatementMaster.push(lineDetails[j]);
tempString = '';
break;
}
i = indexPosition;
}
else {
StatementMaster.push(lineDetails[i]);
}
}
return StatementMaster;
};
CobolHelpers.getStepsData = function (allLines) {
const stepsData = [];
const regex = /EXEC\s*/i;
for (let cnt = 0; cnt < allLines.length; cnt++) {
const line = allLines[cnt];
if (regex.test(line.modifiedLine)) {
const lstLines = [];
for (let j = cnt + 1; j < allLines.length; j++) {
if (j === allLines.length) {
stepsData.push({ counter: cnt, lines: lstLines, step: line.modifiedLine });
break;
}
const subLine = allLines[j].modifiedLine;
if (regex.test(subLine)) {
stepsData.push({ counter: cnt, lines: lstLines, step: line.modifiedLine });
break;
}
else {
if (subLine.includes(" DD ")) {
lstLines.push(subLine);
}
}
}
}
}
return stepsData;
};
CobolHelpers.removeComments = function (sourceCode, pattern = /(?:^\s*|\*|\b\/\/).*$/gm) {
// Define regular expression pattern to match COBOL comments
const commentPattern = typeof pattern === "string" ? new RegExp(pattern) : pattern;
// Remove all lines matching the comment pattern
return sourceCode.filter(line => !line.match(commentPattern)).join('\n');
};
CobolHelpers.extractLineComments = function (sourceCode, pattern = /^(?:\s*|\*|\/\/)\s*(.*)$/gm) {
// Define regular expression pattern to match COBOL line comments
const commentPattern = typeof pattern === "string" ? new RegExp(pattern) : pattern;
// Extract all line comments matching the comment pattern
const lineComments = [];
sourceCode.forEach((line, index) => {
const match = commentPattern.exec(line);
if (match && match[1]) {
lineComments.push({ index: index + 1, comment: match[1].trim() });
}
});
return lineComments;
};
CobolHelpers.removeEmptyLines = function (allLines) {
return allLines.filter(line => line.trim() !== "");
};
CobolHelpers.getDsnDataSets = function (allLines, options) {
const dspRegex = /DISP=([^ ]+)/i;
let cobolDataSets = [];
for (let i = 0; i < allLines.length; i++) {
const line = allLines[i].modifiedLine;
if (!line.includes("DSN="))
continue;
const statements = line.split("DSN=");
const leftPart = statements[0].trim();
const rightPart = statements[1].trim();
const parts = rightPart.split(/,(?![^()]*\))/);
const dsnValue = parts[0].trim();
let type = "";
for (const part of parts) {
if (!part.includes("DISP"))
continue;
if (!dspRegex.test(part))
continue;
const displayValue = dspRegex.exec(part)?.at(1);
type = (displayValue && (displayValue === "SHR" || displayValue === "OUT")) ? "INPUT" : "OUTPUT";
break;
}
if (!type) {
if (allLines.length - 1 !== i) {
const nextLine = allLines[i + 1].modifiedLine.trim();
if (dspRegex.test(nextLine)) {
const displayValue = dspRegex.exec(nextLine)?.at(1);
type = (displayValue && (displayValue === "SHR" || displayValue === "OUT")) ? "INPUT" : "OUTPUT";
}
}
}
if (type === "")
continue;
cobolDataSets.push({
fid: options.fid,
referenceFileId: 0,
calledObjectName: "",
leftVariable: leftPart.trim(),
rightVariable: rightPart.trim(),
pid: options.pid,
dsnValue: dsnValue,
type
});
}
return cobolDataSets;
};
CobolHelpers.concatBmsInitialValues = function (allLines) {
const lineDetails = [];
for (let i = 0; i < allLines.length; i++) {
let codeLine = allLines[i].modifiedLine;
if (!codeLine.includes("INITIAL=")) {
lineDetails.push(allLines[i]);
continue;
}
if (codeLine.includes("INITIAL=") && codeLine.endsWith("',")) {
lineDetails.push(allLines[i]);
continue;
}
let initial = codeLine;
for (let j = i + 1; j < allLines.length; j++) {
const thisLine = allLines[j];
initial += " " + thisLine;
if (initial.endsWith("',")) {
i = j;
allLines[i].modifiedLine = initial;
lineDetails.push(allLines[i]);
break;
}
}
}
return lineDetails;
};
// previously function name in C# was methodNames
CobolHelpers.removeDotFromMethodName = function removeDotFromMethodName(methodList) {
const methodNamesList = [];
for (const method of methodList) {
const line = method.trim();
if (!line.endsWith("."))
continue;
const methodNames = line.split('.') || [""];
const methodName = methodNames.shift()?.trim() || "";
if ((0, lodash_1.isEmpty)(methodName))
continue;
methodNamesList.push(methodName);
}
return methodNamesList;
};
CobolHelpers.extractKeyword = function extractKeyword(input) {
const callIndex = input.indexOf("CALL") + 4;
const remainingString = input.substring(callIndex).trimStart();
const spaceIndex = remainingString.indexOf(' ');
const keyword = spaceIndex !== -1 ? remainingString.substring(0, spaceIndex) : remainingString;
return keyword;
};
CobolHelpers.isInSingleQuotes = function isInSingleQuotes(input) {
return input.startsWith("'") && input.endsWith("'");
};
exports.default = CobolHelpers;
//# sourceMappingURL=cobol-helpers.js.map