Created
October 9, 2019 03:09
-
-
Save SergProduction/a29361ee6c9561bd6d64d0a0b17039a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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