-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kai Wood
committed
Jun 1, 2017
1 parent
bdeef7f
commit 5ceae67
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 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 | ||
``` |