Skip to content

Commit

Permalink
modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Oct 5, 2023
1 parent dfbd94f commit 436bcdd
Show file tree
Hide file tree
Showing 7 changed files with 1,169 additions and 528 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Match balanced string pairs, like `{` and `}` or `<b>` and `</b>`. Supports regu
Get the first matching pair of braces:

```js
var balanced = require('balanced-match')
import balanced from 'balanced-match'

console.log(balanced('{', '}', 'pre{in{nested}}post'))
console.log(balanced('{', '}', 'pre{first}between{second}post'))
Expand All @@ -32,7 +32,7 @@ $ node example.js
## API
### var m = balanced(a, b, str)
### const m = balanced(a, b, str)
For the first non-nested matching pair of `a` and `b` in `str`, return an
object with those keys:
Expand All @@ -47,7 +47,7 @@ If there's no match, `undefined` will be returned.
If the `str` contains more `a` than `b` / there are unmatched pairs, the first match that was closed will be used. For example, `{{a}` will match `['{', 'a', '']` and `{a}}` will match `['', 'a', '}']`.
### var r = balanced.range(a, b, str)
### const r = balanced.range(a, b, str)
For the first non-nested matching pair of `a` and `b` in `str`, return an
array with indexes: `[ <a index>, <b index> ]`.
Expand Down
10 changes: 2 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict'

/**
* @param {string | RegExp} a
* @param {string | RegExp} b
* @param {string} str
*/
function balanced (a, b, str) {
export default function balanced (a, b, str) {
if (a instanceof RegExp) a = maybeMatch(a, str)
if (b instanceof RegExp) b = maybeMatch(b, str)

Expand All @@ -31,14 +29,12 @@ function maybeMatch (reg, str) {
return m ? m[0] : null
}

balanced.range = range

/**
* @param {string} a
* @param {string} b
* @param {string} str
*/
function range (a, b, str) {
export function range (a, b, str) {
let begs, beg, left, right, result
let ai = str.indexOf(a)
let bi = str.indexOf(b, ai + 1)
Expand Down Expand Up @@ -77,5 +73,3 @@ function range (a, b, str) {

return result
}

module.exports = balanced
Loading

0 comments on commit 436bcdd

Please sign in to comment.