# match-bracket

> Find the matching bracket's position for a bracket in a code

Latest version **1.0.0** (published 2015-10-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install match-bracket
pnpm add match-bracket
yarn add match-bracket
bun add match-bracket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2015-10-24 |
| First published | 2015-10-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sung Won Cho |
| Maintainers | sungwoncho |
| Keywords | brace, bracket, matcher |

## Links

- npm: https://www.npmjs.com/package/match-bracket
- Repository: https://github.com/sungwoncho/bracket-match
- Homepage: https://github.com/sungwoncho/bracket-match#readme
- Issues: https://github.com/sungwoncho/bracket-match/issues
- npm.io page: https://npm.io/package/match-bracket

## Recent versions

- 1.0.0 (latest) — 2015-10-24
- 0.1.0 — 2015-10-05

## README

# Match-Bracket

[![Build Status](https://travis-ci.org/sungwoncho/match-bracket.svg?branch=master)](https://travis-ci.org/sungwoncho/match-bracket)

Find the matching bracket's position for a bracket in a code.


## Install

    npm install match-bracket


## Usage

Given this code:

**sample.js**
```javascript
File.prototype.getExtension = function() {
  var re = /[A-Za-z]*(\.[a-z]+)$/g;
  var matched = re.exec(this.path);

  if (matched) {
    var ext = matched[1];
    return ext;
  } else {
    return 'none';
  }
};
```

We can find the matching bracket for the first `{` by doing:

```javascript
var matchBracket = require('match-bracket');

var code = require('fs').readFileSync('./sample.js');
var bracketPos = {line: 1, cursor: 42};

var result = matchBracket(code, bracketPos);
console.log(result);
// => {line: 11, cursor: 1}
```


## API

### matchBracket(code, bracketPos, extension)

Returns the position of the matching bracket of the bracket given by
`bracketPos` from the `code`.

`extension` is an optional argument. It allows match-bracket to recognize
comments in the code and get more accurate results.

Both `bracketPos` and `result` are in the format of:

```javascript
{
  line: // line number,
  cursor: // cursor number
}
```

`line` is the line number in which the bracket appears. `cursor` denotes order
in which the bracket appears in the line. Most IDEs display this.


## Rules

* Unmatched bracket returns results with null as properties.

```javascript
matchBracket('({)', {line: 1, cursor: 2});
// => {line: null, cursor: null}
```

* In ambiguous matches, brackets closest to each other are matched.

```javascript
matchBracket('(()', {line: 1, cursor: 1});
// => {line: null, cursor: null}

matchBracket('(()', {line: 1, cursor: 2});
// => {line: 1, cursor: 3}
```


## License

MIT

---
_Source: https://npm.io/package/match-bracket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
