next-char

0.0.0 • Public • Published

next-char

This is a utility for finding the next character in a string. No, not indexOf. It's to help you parse code.

Imagine you have some JS like this.

var foo = function () {
    if (bar) {
        baz();
    } else {
        quux();
    }
};

As a string, it would be.

'var foo = function () {\nif (bar) {\nbaz();\n} else {\nquux();\n}\n}'

You know the index of the first {, but how do you find its pair (and ignore all the other braces)?

var str = 'var foo = function () {\nif (bar) {\nbaz();\n} else {\nquux();\n}\n}';
 
var openingIndex = str.indexOf('{');
// 22
 
var scopeString = str.substr(openingIndex);
// '{\nif (bar) {\nbaz();\n} else {\nquux();\n}\n}'
 
var closingIndex = nextChar.findClosingPairIndex(scopeString);
// 39
 
scopeString = scopeString.substr(0, closingIndex);
// '{\nif (bar) {\nbaz();\n} else {\nquux();\n}\n}'

Readme

Keywords

none

Package Sidebar

Install

npm i next-char

Weekly Downloads

0

Version

0.0.0

License

ISC

Last publish

Collaborators

  • reergymerej