# find-matching-bracket

> Find matching bracket in Js/Ts/Cs code (including string Template literals interpolation)

Latest version **1.0.3** (published 2021-08-26) · ISC license · 0 weekly downloads

## Install

```sh
npm install find-matching-bracket
pnpm add find-matching-bracket
yarn add find-matching-bracket
bun add find-matching-bracket
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-08-26 |
| First published | 2021-08-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Alf Nielsen |
| Maintainers | alfnielsen |
| Keywords | match, brackets, js, ts, cs |

## Links

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

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.3 (latest) — 2021-08-26
- 1.0.2 — 2021-08-26
- 1.0.1 — 2021-08-25
- 1.0.0 — 2021-08-25

## README

# find-matching-bracket

Find matching bracket in Js/Ts/Cs code (including string Template literals interpolation)

```ts
/**
 * if throwErrors is true, it throw (stop) with info.
 * else it return matching position,
 * or:
 * -2 if startPostion don't match start bracket
 * -3 if end bracket dont match start bracket
 * -1 if cursor comes to end of code without finding maching bracket
 *
 * @param code
 * @param startPosition position for staing bracket
 * @param throwErrors default false - if true: throw error with information
 * @returns
 */
export default function matchBracket(code: string, startPosition: number, throwErrors = false): number;
```

**matchBracket example:**

```ts
const code = "{ }";
const endPosition = matchBracket(code, 0);
// endPosition = 2
```

Complex ex:

```ts
const code =
  "function foo(s: string) {\n" + // <- start pos 24
  "   var str = `text with repeat ${new Array(2).fill(s).map(s=>{ return s + '-{}-' }).join('')} ends here `;\n" +
  "    return { return: str }\n" +
  "}"; // <- return this position 160 (What we need to find!)

const endPosition = matchBracket(code, 24);

// endPosition = 160
const includeBoth = code.substring(24, 161); // 160+1 to include the end bracket
const excludeBoth = code.substring(25, 160); // 24+1 to exclude start bracket
```

Bracket that can be matchs:

`${` and `$"` can only be matched inside string templates (js an cs templates)

Matching bracket:
| name | start | end | node |
|:-----|:-----:|:---:|:-----|
| lineComment        | //  |  \n | |
| multilineComment   | /*  |  */ | |
| angle              | <   |  >  | |
| peparentheses      | (   |  )  | |
| square             | [   |  ]  | |
| curly              | {   |  }  | |
| double             | "   |  "  | |
| single             | '   |  '  | |
| jsTemplate         |  \` |  \` | |
| cSharpTemplate     | $"  |  "  | |
| escapeJsTemaple    | ${  |  }  | only in jsTemplate |
| inCSharpTemaple    | {   |  }  | only in cSharpTemplate |

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