Advertisement
mkostira

Untitled

Apr 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2.  
  3. @Injectable({
  4. providedIn: 'root'
  5. })
  6. export class EscapeUnescapeQuotesService {
  7.  
  8. constructor() {}
  9.  
  10. escapeRule = function(sourceString) {
  11. const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
  12.  
  13. return this.replaceMatches(sourceString, regex, this.escapeExpression);
  14. };
  15.  
  16. unescapeRule = function(sourceString) {
  17. const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
  18.  
  19. return this.replaceMatches(sourceString, regex, this.unescapeExpression);
  20. };
  21.  
  22. escapeExpression = function(sourceString) {
  23. const regex = /([\w\$]+)\s*(?:\,|\))/mg; // will match 'someString,' and 'someString)', handles whitespace
  24.  
  25. return this.replaceMatches(sourceString, regex, this.escapeWord);
  26. };
  27.  
  28. unescapeExpression = function(sourceString) {
  29. const regex = /([\w\$\\\"]+)\s*(?:\,|\))/mg; // will match '\"someString\",' and '\"someString\")' , handles whitespace
  30.  
  31. return this.replaceMatches(sourceString, regex, this.unescapeWord);
  32. };
  33.  
  34. replaceMatches = function(expr, regex, replaceFunction) {
  35.  
  36. let result = expr; // holds result of conversion
  37.  
  38. let matches = this.extractMatches(expr, regex);
  39.  
  40. matches.reverse(); // start replacing from the end so we don't screw up the indeces
  41.  
  42. // apply replaceFunction to each match
  43. matches.forEach(m => {
  44. // console.log(`Found match, value: ${m.value} at ${m.index}`);
  45. result = this.spliceString(result, m.index, m.value.length, replaceFunction.bind(this)(m.value) );
  46. });
  47.  
  48. return result;
  49. };
  50.  
  51. extractMatches = function(expr, regex) {
  52. let matches = []; // {match, index} pairs
  53. let m;
  54. // tslint:disable-next-line: no-conditional-assignment
  55. while ((m = regex.exec(expr)) !== null) {
  56. // This is necessary to avoid infinite loops with zero-width matches
  57. if (m.index === regex.lastIndex) {
  58. regex.lastIndex++;
  59. }
  60. matches.push({value: m[1], index: m.index});
  61. }
  62. return matches;
  63. };
  64.  
  65. spliceString = function(str, index, count, add) {
  66. // We cannot pass negative indexes directly to the 2nd slicing operation.
  67. if (index < 0) {
  68. index = str.length + index;
  69. if (index < 0) {
  70. index = 0;
  71. }
  72. }
  73.  
  74. return str.slice(0, index) + (add || '') + str.slice(index + count);
  75. };
  76.  
  77. escapeWord = function(str) {
  78. if (str == 'true' || str == 'false') {
  79. return str;
  80. }
  81. return '\\"' + str + '\\"';
  82. };
  83.  
  84. unescapeWord = function(str) {
  85. let res = str;
  86. if (str.substring(0, 2) == '\\"' && str.substring(str.length - 2) == '\\"') {
  87. res = str.substring(2, str.length - 2); // strip 1st two and last two chars
  88. }
  89. return res;
  90. };
  91.  
  92.  
  93.  
  94.  
  95. /* replaceMatches(expr, regex, replaceFunction) {
  96.  
  97. let result = expr; // holds result of conversion
  98.  
  99.  
  100. const matches = this.extractMatches(expr, regex);
  101.  
  102. matches.reverse(); // start replacing from the end so we don't screw up the indeces
  103.  
  104. // apply replaceFunction to each match
  105. matches.forEach(m => {
  106. // console.log(`Found match, value: ${m.value} at ${m.index}`);
  107. result = this.spliceString(result, m.index, m.value.length, replaceFunction(m.value) );
  108. });
  109.  
  110. return result;
  111. }
  112.  
  113. escapeRule(sourceString) {
  114. const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
  115.  
  116. return this.replaceMatches(sourceString, regex, this.escapeExpression);
  117. }
  118.  
  119. unescapeRule(sourceString) {
  120. const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
  121.  
  122. return this.replaceMatches(sourceString, regex, this.unescapeExpression);
  123. }
  124.  
  125.  
  126. escapeExpression(sourceString) {
  127. const regex = /([\w\$]+)\s*(?:\,|\))/mg; // will match 'someString,' and 'someString)', handles whitespace
  128.  
  129. return this.replaceMatches(sourceString, regex, this.escapeWord);
  130. }
  131.  
  132. unescapeExpression(sourceString) {
  133. const regex = /([\w\$\\\"]+)\s*(?:\,|\))/mg; // will match '\"someString\",' and '\"someString\")' , handles whitespace
  134.  
  135. return this.replaceMatches(sourceString, regex, this.unescapeWord);
  136. }
  137.  
  138.  
  139.  
  140. extractMatches(expr, regex) {
  141. const matches = []; // {match, index} pairs
  142. // while ((m = regex.exec(expr)) !== null) {
  143. let m;
  144. // tslint:disable-next-line: no-conditional-assignment
  145. while ((m = regex.exec(expr)) !== null) {
  146.  
  147. // This is necessary to avoid infinite loops with zero-width matches
  148. if (m.index === regex.lastIndex) {
  149. regex.lastIndex++;
  150. }
  151. matches.push({value: m[1], index: m.index});
  152. }
  153. return matches;
  154. }
  155.  
  156. spliceString(str, index, count, add) {
  157. // We cannot pass negative indexes directly to the 2nd slicing operation.
  158. if (index < 0) {
  159. index = str.length + index;
  160. if (index < 0) {
  161. index = 0;
  162. }
  163. }
  164.  
  165. return str.slice(0, index) + (add || '') + str.slice(index + count);
  166. }
  167.  
  168. escapeWord(str) {
  169. if (str === 'true' || str === 'false') {
  170. return str;
  171. }
  172. return '\\"' + str + '\\"';
  173. }
  174.  
  175. unescapeWord(str) {
  176. let res = str;
  177. if (str.substring(0, 2) === '\\"' && str.substring(str.length - 2) === '\\"') {
  178. res = str.substring(2, str.length - 2); // strip 1st two and last two chars
  179. }
  180. return res;
  181. }
  182. */
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement