Code Monkey home page Code Monkey logo

pairify's Introduction

Pairify

Finds balanced matches.

๐Ÿ‘‰ Playground

The library matches the following balanced pairs:

  • { and }
  • ( and )
  • [ and ]
  • < and >
  • ' and '
  • " and "
  • ` and `
  • /* and */
  • // and \n

Example

Consider the following code.js file:

function getAnswer(answer = { value: 42 }) {
  return "The answer is " + answer.value;
}
console.log(getAnswer({ value: 100 }));

and when we pass it to Pairify

const Pairify = require('pairify');
const code = fs.readFileSync('./code.js').toString('utf8');

console.log(Pairify.analyze(code))

We'll get the following result:

[
  {"type":"curly","from":[1,29],"to":[1,42],"body":[28,13]},
  {"type":"round","from":[1,19],"to":[1,43],"body":[18,24]},
  {"type":"double-quotes","from":[2,10],"to":[2,26],"body":[54,16]},
  {"type":"curly","from":[1,44],"to":[3,2],"body":[43,45]},
  {"type":"curly","from":[4,23],"to":[4,37],"body":[111,14]},
  {"type":"round","from":[4,22],"to":[4,38],"body":[110,16]},
  {"type":"round","from":[4,12],"to":[4,39],"body":[100,27]}
]

Installation

API

  • Pairify.analyze(code: string): Pair[] - parses all the code and returns all the balanced matches
  • Pairify.match(code: string, line:number, position: number): Pair[] - returns balanced matches based on the provided line and position.

where Pair is the following

{
  type: string; // curly, round, square, angle, single-quotes, double-quotes, template-literal, comment-single-line or comment-block
  from: number[]; // [line, position],
  to: number[]; // [line, position],
  body: number[] // [char, length]
}

pairify's People

Contributors

krasimir avatar

Stargazers

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

Watchers

 avatar  avatar

Forkers

hharzer

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.