Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- @Injectable({
- providedIn: 'root'
- })
- export class EscapeUnescapeQuotesService {
- constructor() {}
- escapeRule = function(sourceString) {
- const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
- return this.replaceMatches(sourceString, regex, this.escapeExpression);
- };
- unescapeRule = function(sourceString) {
- const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
- return this.replaceMatches(sourceString, regex, this.unescapeExpression);
- };
- escapeExpression = function(sourceString) {
- const regex = /([\w\$]+)\s*(?:\,|\))/mg; // will match 'someString,' and 'someString)', handles whitespace
- return this.replaceMatches(sourceString, regex, this.escapeWord);
- };
- unescapeExpression = function(sourceString) {
- const regex = /([\w\$\\\"]+)\s*(?:\,|\))/mg; // will match '\"someString\",' and '\"someString\")' , handles whitespace
- return this.replaceMatches(sourceString, regex, this.unescapeWord);
- };
- replaceMatches = function(expr, regex, replaceFunction) {
- let result = expr; // holds result of conversion
- let matches = this.extractMatches(expr, regex);
- matches.reverse(); // start replacing from the end so we don't screw up the indeces
- // apply replaceFunction to each match
- matches.forEach(m => {
- // console.log(`Found match, value: ${m.value} at ${m.index}`);
- result = this.spliceString(result, m.index, m.value.length, replaceFunction.bind(this)(m.value) );
- });
- return result;
- };
- extractMatches = function(expr, regex) {
- let matches = []; // {match, index} pairs
- let m;
- // tslint:disable-next-line: no-conditional-assignment
- while ((m = regex.exec(expr)) !== null) {
- // This is necessary to avoid infinite loops with zero-width matches
- if (m.index === regex.lastIndex) {
- regex.lastIndex++;
- }
- matches.push({value: m[1], index: m.index});
- }
- return matches;
- };
- spliceString = function(str, index, count, add) {
- // We cannot pass negative indexes directly to the 2nd slicing operation.
- if (index < 0) {
- index = str.length + index;
- if (index < 0) {
- index = 0;
- }
- }
- return str.slice(0, index) + (add || '') + str.slice(index + count);
- };
- escapeWord = function(str) {
- if (str == 'true' || str == 'false') {
- return str;
- }
- return '\\"' + str + '\\"';
- };
- unescapeWord = function(str) {
- let res = str;
- if (str.substring(0, 2) == '\\"' && str.substring(str.length - 2) == '\\"') {
- res = str.substring(2, str.length - 2); // strip 1st two and last two chars
- }
- return res;
- };
- /* replaceMatches(expr, regex, replaceFunction) {
- let result = expr; // holds result of conversion
- const matches = this.extractMatches(expr, regex);
- matches.reverse(); // start replacing from the end so we don't screw up the indeces
- // apply replaceFunction to each match
- matches.forEach(m => {
- // console.log(`Found match, value: ${m.value} at ${m.index}`);
- result = this.spliceString(result, m.index, m.value.length, replaceFunction(m.value) );
- });
- return result;
- }
- escapeRule(sourceString) {
- const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
- return this.replaceMatches(sourceString, regex, this.escapeExpression);
- }
- unescapeRule(sourceString) {
- const regex = /((?:measurement|evt|alarm|countMeasurements|countEvents|countAlarms)\s?\(.*?\))/mg;
- return this.replaceMatches(sourceString, regex, this.unescapeExpression);
- }
- escapeExpression(sourceString) {
- const regex = /([\w\$]+)\s*(?:\,|\))/mg; // will match 'someString,' and 'someString)', handles whitespace
- return this.replaceMatches(sourceString, regex, this.escapeWord);
- }
- unescapeExpression(sourceString) {
- const regex = /([\w\$\\\"]+)\s*(?:\,|\))/mg; // will match '\"someString\",' and '\"someString\")' , handles whitespace
- return this.replaceMatches(sourceString, regex, this.unescapeWord);
- }
- extractMatches(expr, regex) {
- const matches = []; // {match, index} pairs
- // while ((m = regex.exec(expr)) !== null) {
- let m;
- // tslint:disable-next-line: no-conditional-assignment
- while ((m = regex.exec(expr)) !== null) {
- // This is necessary to avoid infinite loops with zero-width matches
- if (m.index === regex.lastIndex) {
- regex.lastIndex++;
- }
- matches.push({value: m[1], index: m.index});
- }
- return matches;
- }
- spliceString(str, index, count, add) {
- // We cannot pass negative indexes directly to the 2nd slicing operation.
- if (index < 0) {
- index = str.length + index;
- if (index < 0) {
- index = 0;
- }
- }
- return str.slice(0, index) + (add || '') + str.slice(index + count);
- }
- escapeWord(str) {
- if (str === 'true' || str === 'false') {
- return str;
- }
- return '\\"' + str + '\\"';
- }
- unescapeWord(str) {
- let res = str;
- if (str.substring(0, 2) === '\\"' && str.substring(str.length - 2) === '\\"') {
- res = str.substring(2, str.length - 2); // strip 1st two and last two chars
- }
- return res;
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement