Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Created October 9, 2019 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SergProduction/a29361ee6c9561bd6d64d0a0b17039a2 to your computer and use it in GitHub Desktop.
Save SergProduction/a29361ee6c9561bd6d64d0a0b17039a2 to your computer and use it in GitHub Desktop.
let text = `
Баланс и оплата
Как получить выйгрыш?
Деятельность приема от физичиских лиц ....
Как пополнить баланс?
Для того чтоб пополнить баланс, надо быть в
- банде Балансов
Идентификацияbeecoder
Где посмотреть точки?
Там и тут ...
Как идентифицироваться онлайн?
Для того чтобы идентифицироваться онлайн надо`;
const processText = (text) => {
const createNode = (value, offset = -1) => ({ value, children: [], offset });
const stack = [ createNode('__root__') ];
const getLast = () => stack[stack.length - 1];
text.replace(/\n\s*-/g, '').split('\n').filter(line => line.trim().length > 0).map(line => {
const offset = line.length - line.trimStart().length;
while (getLast().offset >= offset) stack.length--;
const node = createNode(line.trim(), offset);
getLast().children.push(node);
stack.push(node);
});
return stack[0];
};
console.log(JSON.stringify(processText(text), null, 2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment