Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add arabic encoding #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added dist/.DS_Store
Binary file not shown.
101 changes: 58 additions & 43 deletions dist/utils/Parser.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = void 0;
const parse = (input) => {
if (input.length > 0) {
let arr = input
.map((x, index) => (x.includes("\t") ? null : index))
.filter((x) => x !== null);
return arr
.map((item, index) => {
return index + 1 < arr.length
? [item, arr[index + 1]]
: [item, input.length];
})
.filter((x) => x !== undefined)
.map((range) => {
if (range) {
if (range[0] === range[1]) {
return {
name: input[range[0]],
children: [],
};
}
else {
return {
name: input[range[0]],
children: (0, exports.parse)(input
.slice(range[0] + 1, range[1])
.map((x) => x.substring(1))),
};
}
function removeCircularReferences(node) {
const visitedNodes = new Set();

function removeCircular(node) {
if (visitedNodes.has(node)) {
// Circular reference found, remove it
return null;
}

visitedNodes.add(node);

node.children = node.children.map(child => removeCircular(child)).filter(Boolean);

return node;
}

return removeCircular(node);
}

function parse(lines) {
let tree = null;
let currentParent = null;
let currentLevel = -1;
const stack = [];

lines.forEach(line => {
const level = line.search(/\S|$/); // Find the first non-whitespace character
const content = line.trim();
const node = { content: content, children: [] };

if (!tree) {
tree = node;
currentParent = tree;
} else if (level > currentLevel) {
stack.push({ level: currentLevel, node: currentParent });
currentParent = currentParent.children[currentParent.children.length - 1] || currentParent;
currentLevel = level;
} else if (level < currentLevel) {
while (stack.length > 0 && stack[stack.length - 1].level >= level) {
stack.pop();
}
else {
return {
name: "error",
children: [],
};
if (stack.length > 0) {
currentParent = stack[stack.length - 1].node;
currentLevel = stack[stack.length - 1].level;
}
});
}
else {
return [];
}
};
exports.parse = parse;
}

if (content !== "") {
currentParent.children.push(node);
stack.push({ level: level, node: node });
}
});

// Remove circular references
tree = removeCircularReferences(tree);

return tree;
}

module.exports = { parse };
Loading