GithubHelp home page GithubHelp logo

balanced-match's Introduction

balanced-match

Match balanced string pairs, like { and } or <b> and </b>. Supports regular expressions as well!

CI downloads

Example

Get the first matching pair of braces:

import balanced from 'balanced-match'

console.log(balanced('{', '}', 'pre{in{nested}}post'))
console.log(balanced('{', '}', 'pre{first}between{second}post'))
console.log(balanced(/\s+\{\s+/, /\s+\}\s+/, 'pre  {   in{nest}   }  post'))

The matches are:

$ node example.js
{ start: 3, end: 14, pre: 'pre', body: 'in{nested}', post: 'post' }
{ start: 3,
  end: 9,
  pre: 'pre',
  body: 'first',
  post: 'between{second}post' }
{ start: 3, end: 17, pre: 'pre', body: 'in{nest}', post: 'post' }

API

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:

  • start the index of the first match of a
  • end the index of the matching b
  • pre the preamble, a and b not included
  • body the match, a and b not included
  • post the postscript, a and b not included

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', '}'].

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> ].

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 [ 1, 3 ] and {a}} will match [0, 2].

Installation

With npm do:

npm install balanced-match

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

License

(MIT)

Copyright (c) 2013 Julian Gruber <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

balanced-match's People

Contributors

amilajack avatar dependabot[bot] avatar faizanu94 avatar greenkeeperio-bot avatar ianstormtaylor avatar isaacs avatar jamiebuilds avatar jeromedecoster avatar jimmywarting avatar juliangruber avatar kishorkunal-raj avatar lanodan avatar neytema avatar stevemao avatar truexpixells avatar whaaaley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

balanced-match's Issues

Error with new version launched today

Hi,

Since the launch of the new version today I get the following error:

/node_modules/balanced-match/index.js:7 const r = range(a, b, str) ^^^^^
SyntaxError: Use of const in strict mode.

What do I need to do?

Kind Regards

No body when delimiters match

I would expect matching delimiters to work. Ie: code fences in Markdown.

    ```js
    const foo = bar => baz
    ```

It seems imposible to balance matching delimiters with balanced-match.

Is this expected?

Example:

const balanced = require('balanced-match')

const contents = 'PRE ___BODY___ POST'
const result = balanced('___', '___', contents)

console.log(result)

Result:

(Where is the body?)

{ start: 11, end: 11, pre: 'PRE ___BODY', body: '', post: ' POST' }

Expected:

(I was thinking we should see this)

{ start: 4, end: 11, pre: 'PRE ', body: 'BODY', post: ' POST' }

Add separate ES5 & ES6 builds & update package.json

As of v2.0.0, many projects will inadvertently bundle ES6 source from balanced-match in their ES5 bundles. This is a result of 1) balanced-match v2.0.0 being written in ES6 but not offering a separate ES5 version as well, and 2) most build processes being configured to ignore files in the node_modules folder when transpiring to ES5 (e.g. babel).

To address this issue, this library should offer ES5 and ES6 versions (minified and unminified) to accommodate all JavaScript environments. This will allow projects to use balanced-match without having to worry about transpilation to ES5. The build process would be trivial (compared to larger apps / libraries) and would likely prevent a lot of frustration.

Happy to create a PR with proposed changes.

Thanks!

Bug or missunderstanding

Hi,

I'm using balanced-match to isolate the body of a function (all between brackets)
So, I assume during my test that a function is begining by the same pattern (scope function name params {) and finishing by a bracket (with positive lookahead to see if the next caracters are a new function)

Here is my two regex :
firstRegex : /(override)?\s?(public|private|protected)\s(?:function)\s(get)?\s?(\w*)\s?\((.*)\)\s?:?\s?(\w*)\s*{/
secondRegex : /}(\s)*(?=override|public|private|protected|(}\s*}))/

It's working fine except for the last function who is not followed by a new function but by two brackets (for the end of the class and the end of package)

I have tested my two regex on regex101 and it's working fine (https://regex101.com/r/A7jQDr/1) but when I test with balanced-match it didn't match the same way regex101 does.
Here is my test on RunKit : https://runkit.com/59c0dc7528f14c00126b4314/59c0dc7662f5730012fd1760

As you can see, the match.post contains a extra bracket ..

If someone can tell me what's wrong.

Skip over slicing

I'm creating a very lightweight module that can potentially run several millions of times per second.

Right now, I don't use this module because it slices the data automatically... But I would like to use this module because of it's efficient balanced matching.

When you slice the data over and over millions of times when I don't need it, the nanosecond tasks start adding up into several hundreds of millisecond delays, and possibly even seconds.

For my module, I am purely interested in the positions of the matches... I can develop my own highly efficient slicing pattern specific to the data instead of this module doing it automatically when it is not what I need.

v1.0.0 missing from NPM

npm ERR! notarget No compatible version found: balanced-match@^1.0.0
npm ERR! notarget Valid install targets:
npm ERR! notarget 0.4.2, 0.4.1, 0.4.0, 0.3.0, 0.2.1, 0.2.0, 0.1.0, 0.0.1, 0.0.0
npm ERR! notarget
npm ERR! notarget This is most likely not a problem with npm itself.
npm ERR! notarget In most cases you or one of your dependencies are requesting
npm ERR! notarget a package version that doesn't exist.
npm ERR! notarget
npm ERR! notarget It was specified as a dependency of 'brace-expansion'
npm ERR! notarget

As of a few hours ago, v1.0.0 of this no longer exists on NPM. Not sure if NPM issues or if someone deleted the version from NPM.

EDIT: This maybe a NPM proxy issues.. Will update I know for sure. Was nexus proxy issue.

errors in the readme?

My github build pipline is failing and giving me this error:

Is this in fact caused by your module? or is that a red herring?

The page build failed for the `master` branch with the following error:

The variable `{{a}` on line 50 in `site_generator/node_modules/balanced-match/README.md` was not properly closed with `}}`. For more information, see https://docs.github.com/github/working-with-github-pages/troubleshooting-jekyll-build-errors-for-github-pages-sites#tag-not-properly-terminated.

For information on troubleshooting Jekyll see:

  https://docs.github.com/articles/troubleshooting-jekyll-builds

Building Jekyll doesn't like the Readme.md

When attempting to build a Jekyll site on Github, I'm getting this error:
I'm getting this error Your site is having problems building: The variable {{a} on line 50 in node_modules/balanced-match/README.md was not properly closed with }}. For more information, see https://help.github.com/articles/page-build-failed-tag-not-properly-terminated/.

Create LICENSE file

I know you have the license as part of the Readme.md file, but would you please consider making it a separate file called LICENSE or LICENSE.md or LICENSE.txt? It's fairly standard practice to make the license a separate file so that it's easier for people (and packagers for distributions such as Fedora) to easily find the license.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.