Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.2KB

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