Newer
Older
CVSS_3.0_GUI / node_modules / nwjs-builder-phoenix / node_modules / globby / node_modules / glob / node_modules / minimatch / node_modules / brace-expansion / node_modules / balanced-match / index.js
root on 7 May 2019 1 KB Initial commit
  1. 'use strict';
  2. module.exports = balanced;
  3. function balanced(a, b, str) {
  4. if (a instanceof RegExp) a = maybeMatch(a, str);
  5. if (b instanceof RegExp) b = maybeMatch(b, str);
  6.  
  7. var r = range(a, b, str);
  8.  
  9. return r && {
  10. start: r[0],
  11. end: r[1],
  12. pre: str.slice(0, r[0]),
  13. body: str.slice(r[0] + a.length, r[1]),
  14. post: str.slice(r[1] + b.length)
  15. };
  16. }
  17.  
  18. function maybeMatch(reg, str) {
  19. var m = str.match(reg);
  20. return m ? m[0] : null;
  21. }
  22.  
  23. balanced.range = range;
  24. function range(a, b, str) {
  25. var begs, beg, left, right, result;
  26. var ai = str.indexOf(a);
  27. var bi = str.indexOf(b, ai + 1);
  28. var i = ai;
  29.  
  30. if (ai >= 0 && bi > 0) {
  31. begs = [];
  32. left = str.length;
  33.  
  34. while (i >= 0 && !result) {
  35. if (i == ai) {
  36. begs.push(i);
  37. ai = str.indexOf(a, i + 1);
  38. } else if (begs.length == 1) {
  39. result = [ begs.pop(), bi ];
  40. } else {
  41. beg = begs.pop();
  42. if (beg < left) {
  43. left = beg;
  44. right = bi;
  45. }
  46.  
  47. bi = str.indexOf(b, i + 1);
  48. }
  49.  
  50. i = ai < bi && ai >= 0 ? ai : bi;
  51. }
  52.  
  53. if (begs.length) {
  54. result = [ left, right ];
  55. }
  56. }
  57.  
  58. return result;
  59. }
Buy Me A Coffee