bracket-balance - get the difference between opening and closing brackets in a string
Installation
npm install bracket-balance --save
Example
const bracketBalance = require('bracket-balance');
let exampleString = '{hello{{ ]}';
bracketBalance(exampleString, '{}');
//returns 2 because there are 2 more opening than closing brackets
Definition
Arguments
String
to search throughString
containing a pair of brackets ('{}'
,'()'
or'[]'
)
Returns
Number
representing the difference between opening and closing brackets
- bigger than 0 means there are more opening than closing brackets
- exactly 0 means that there is an closing one for each opening bracket
- smaller than 0 means there are more closing than opening brackets