https://github.com/kaiwood/is-balanced
Check if a string/text has balanced character or word pairs, e.g. '(', ')' or 'if', 'end'
https://github.com/kaiwood/is-balanced
balanced javascript nodejs npm-module
Last synced: 5 months ago
JSON representation
Check if a string/text has balanced character or word pairs, e.g. '(', ')' or 'if', 'end'
- Host: GitHub
- URL: https://github.com/kaiwood/is-balanced
- Owner: kaiwood
- License: mit
- Created: 2017-06-01T18:58:33.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-02T10:51:19.000Z (about 9 years ago)
- Last Synced: 2024-08-09T13:12:14.496Z (almost 2 years ago)
- Topics: balanced, javascript, nodejs, npm-module
- Language: JavaScript
- Size: 41 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# is-balanced
## Installation
`npm install is-balanced`
## Usage
This module checks for balanced things in a given string, mainly for parsing programming languages, HTML, etc.
Without additional arguments, it defaults to searching for parentheses.
```javascript
const isBalanced = require("is-balanced");
let text = "((()))";
console.log(isBalanced(text)); // => true
text = "(()"
console.log(isBalanced(text)); // => false
```
For searching for e.g. curly braces:
```javascript
let text = "{{}}";
console.log(isBalanced(text, "{", "}");
```
My personal need is for parsing Ruby code to find balanced if..end or def..end statements, so the opening and closing arguments work with multiple opening/closing arguments as well with regular expressions:
```javascript
let text = `
if true
[1, 2, 3].each do |number|
puts number if number % 2 == 0
end
end
`
console.log(isBalanced(text, [/^\s*?if/, "do", "def"], ["end"])) // => true
```