fume-fhir-converter
Version:
FHIR-Utilized Mapping Engine - Community
97 lines • 3.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeComments = void 0;
const tslib_1 = require("tslib");
/**
* © Copyright Outburn Ltd. 2022-2024 All Rights Reserved
* Project name: FUME-COMMUNITY
*/
const thrower_1 = tslib_1.__importDefault(require("../thrower"));
const isUrlPart = (charIndex, expr) => {
// the minimum index for a url's // part is after the 'http(s):' part
// so undex lower than 7 means it's a comment and not a url
if (charIndex < 7)
return false;
const prevSevenChars = expr.substring(charIndex - 7, charIndex);
const prevSixChars = prevSevenChars.substring(1);
if (['https:', '[https:'].includes(prevSevenChars.trimStart()) || ['http:', '[http:'].includes(prevSixChars.trimStart())) {
return true;
}
else {
return false;
}
};
const removeComments = (expr) => {
const exprLen = expr.length;
if (exprLen === 0)
return expr;
let accExpr = '';
let currentChar = '';
let nextChar = '';
let prevChar = '';
let prevPrevChar = '';
let openedQuote = '';
let openedComment = '';
for (let i = 0; i < exprLen; i++) {
currentChar = expr.charAt(i);
nextChar = i < (exprLen - 1) ? expr.charAt(i + 1) : '';
prevChar = i > 0 ? expr.charAt(i - 1) : '';
prevPrevChar = i > 1 ? expr.charAt(i - 2) : '';
if (openedComment !== '') {
// inside a comment
const twoChars = prevChar + currentChar;
if (openedComment === '//' && (currentChar === '\r' || currentChar === '\n')) {
// this is the end of the // comment
openedComment = '';
accExpr += '\n';
}
else {
if (openedComment === '/*') {
if (twoChars === '*/' && prevPrevChar !== '/') {
// this is the end of the /* comment
openedComment = '';
}
else {
if (i === exprLen - 1) {
// end of expression
const msg = 'Comment has no closing tag';
thrower_1.default.throwParseError(msg);
}
}
}
}
;
continue;
}
;
// not inside a comment
if ((currentChar === '"' || currentChar === '\'') && prevChar !== '\\') {
// quote sign, unescaped
if (openedQuote === '') {
// it's an opening quote
openedQuote = currentChar;
}
else {
// it's a closing quote
if (openedQuote === currentChar) {
openedQuote = '';
}
}
}
else {
// not a quote sign
const twoChars = currentChar + nextChar;
const notUrl = !isUrlPart(i, expr);
if (openedQuote === '' && (twoChars === '/*' || (twoChars === '//' && notUrl))) {
// opening comment, not inside quotes
openedComment = twoChars;
continue;
}
}
accExpr += currentChar;
}
;
return accExpr;
};
exports.removeComments = removeComments;
//# sourceMappingURL=removeComments.js.map