New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

find-matching-bracket

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-matching-bracket

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

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

find-matching-bracket

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

/**
 * 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:

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

Complex ex:

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:

namestartendnode
lineComment//\n
multilineComment/**/
angle<>
peparentheses()
square[]
curly{}
double""
single''
jsTemplate``
cSharpTemplate$""
escapeJsTemaple${}only in jsTemplate
inCSharpTemaple{}only in cSharpTemplate

Keywords

FAQs

Package last updated on 26 Aug 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc