match-token
Given a string and a position, it finds the position of the token matching the one at the given position.
Last updated 8 years ago by thlorenz .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install match-token 
SYNC missed versions from official npm registry.

match-token build status

Given a string and a position, it finds the position of the token matching the one at the given position.

var matchToken = require('match-token');

matchToken('{1}', 0) // -> 2
matchToken('{1}', 2) // -> 0


//          012345678901234
matchToken('var foo = "bar"', 10) // -> 14 (quotes)

matchToken('{{1}}', 0) // -> 4 (outer brace)
matchToken('{{1}}', 1) // -> 3 (inner brace)

Installation

npm i match-token

Features

Matches the following tokens in a given string.

  • { with } and vice versa
  • ( with ) and vice versa
  • [ with ] and vice versa
  • quotes ' and "

Caveats

Not meant to be perfect, i.e. does no code parsing in order to be able to handle incomplete code.

Simply counts opening vs. closing tokens and therefore does nothing special if a token appears in a string for instance.

//          0123456789 
matchToken('{ a: "}" }', 0) // -> 6 (although it is in a string)

In order to disambiguate between quotes, i.e., when not sure if given quote is the opener or close, it uses the match that puts the least distance between opening an closing quotes.

Current Tags

  • 0.1.1                                ...           latest (8 years ago)

2 Versions

  • 0.1.1                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 2
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (1)

Copyright 2014 - 2017 © taobao.org |