"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/balanced-match/index.js" (7 Feb 2017, 1160 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 module.exports = balanced;
    2 function balanced(a, b, str) {
    3   if (a instanceof RegExp) a = maybeMatch(a, str);
    4   if (b instanceof RegExp) b = maybeMatch(b, str);
    5 
    6   var r = range(a, b, str);
    7 
    8   return r && {
    9     start: r[0],
   10     end: r[1],
   11     pre: str.slice(0, r[0]),
   12     body: str.slice(r[0] + a.length, r[1]),
   13     post: str.slice(r[1] + b.length)
   14   };
   15 }
   16 
   17 function maybeMatch(reg, str) {
   18   var m = str.match(reg);
   19   return m ? m[0] : null;
   20 }
   21 
   22 balanced.range = range;
   23 function range(a, b, str) {
   24   var begs, beg, left, right, result;
   25   var ai = str.indexOf(a);
   26   var bi = str.indexOf(b, ai + 1);
   27   var i = ai;
   28 
   29   if (ai >= 0 && bi > 0) {
   30     begs = [];
   31     left = str.length;
   32 
   33     while (i >= 0 && !result) {
   34       if (i == ai) {
   35         begs.push(i);
   36         ai = str.indexOf(a, i + 1);
   37       } else if (begs.length == 1) {
   38         result = [ begs.pop(), bi ];
   39       } else {
   40         beg = begs.pop();
   41         if (beg < left) {
   42           left = beg;
   43           right = bi;
   44         }
   45 
   46         bi = str.indexOf(b, i + 1);
   47       }
   48 
   49       i = ai < bi && ai >= 0 ? ai : bi;
   50     }
   51 
   52     if (begs.length) {
   53       result = [ left, right ];
   54     }
   55   }
   56 
   57   return result;
   58 }