Last active
July 20, 2022 11:02
-
-
Save andrejsharapov/583490af9e1c3376905af28867d8e563 to your computer and use it in GitHub Desktop.
Brackets Pair Viewer
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
<script> | |
export default { | |
data: () => ({ | |
editor: '', | |
}), | |
methods: { | |
bracketsReview() { | |
let chars = this.editor.split(''), | |
stack = [], | |
open = ['{', '(', '['], | |
close = ['}', ')', ']'], | |
closeIndex, | |
openIndex; | |
for (let i = 0, len = chars.length; i < len; i++) { | |
openIndex = open.indexOf(chars[i]); | |
if (openIndex !== -1) { | |
stack.push(openIndex); | |
continue; | |
} | |
closeIndex = close.indexOf(chars[i]); | |
if (closeIndex !== -1) { | |
openIndex = stack.pop(); | |
if (closeIndex !== openIndex) { | |
this.errorMessage = `Ошибка! Скобки не совпадают. Проверьте наличие "${open[closeIndex]}" скобки.`; | |
return false; | |
} | |
} | |
} | |
if (stack.length !== 0) { | |
if (close[openIndex] !== undefined) { | |
if (stack == 0) { | |
this.errorMessage = `Ошибка! Проверьте наличие "}" закрывающих скобок.`; | |
} | |
if (stack == 1) { | |
this.errorMessage = `Ошибка! Проверьте наличие ")" закрывающих скобок.`; | |
} | |
if (stack == 2) { | |
this.errorMessage = `Ошибка! Проверьте наличие "]" закрывающих скобок.`; | |
} | |
} else { | |
this.errorMessage = `Ошибка! Проверьте парность закрывающих скобок.`; | |
} | |
return false; | |
} | |
this.errorMessage = ''; | |
return true; | |
}, | |
}, | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment