@contrail/util 1.0.27 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function replaceAllWithFunction(string: string, regEx: RegExp, replacerFunction: (matchedString: string) => string): string;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.replaceAllWithFunction = void 0;
4
+ function replaceAllWithFunction(string, regEx, replacerFunction) {
5
+ let match;
6
+ let newString = '';
7
+ let startIndex = 0;
8
+ while ((match = regEx.exec(string)) !== null) {
9
+ let matchedString = match[0];
10
+ let index = match.index;
11
+ const newValue = replacerFunction(matchedString);
12
+ newString += string.substring(startIndex, index);
13
+ newString += newValue;
14
+ startIndex += index + matchedString.length;
15
+ }
16
+ return newString;
17
+ }
18
+ exports.replaceAllWithFunction = replaceAllWithFunction;
@@ -1,3 +1,4 @@
1
+ import { replaceAllWithFunction } from "./replaceAll/replaceAllWithFunction";
1
2
  export declare class StringUtil {
2
3
  static convertToHyphenCase(entityName: string, transformToLowerCase?: boolean): string;
3
4
  static parseVariables(variableString: string, data: any, quoteStrings?: boolean): string;
@@ -5,4 +6,5 @@ export declare class StringUtil {
5
6
  static convertToTitleCase(str: any): any;
6
7
  static isString(value: any): boolean;
7
8
  static isArray(value: any): boolean;
9
+ static replaceAllWithFunction: typeof replaceAllWithFunction;
8
10
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StringUtil = void 0;
4
+ const replaceAllWithFunction_1 = require("./replaceAll/replaceAllWithFunction");
4
5
  class StringUtil {
5
6
  static convertToHyphenCase(entityName, transformToLowerCase = true) {
6
7
  const slugName = entityName === null || entityName === void 0 ? void 0 : entityName.split(/(?=[A-Z])/).join('-');
@@ -10,19 +11,17 @@ class StringUtil {
10
11
  return slugName;
11
12
  }
12
13
  static parseVariables(variableString, data, quoteStrings = false) {
13
- const regExp = /\{([^}]+)\}/;
14
- let done = false;
15
- let currentVariableString = variableString;
16
- while (!done) {
17
- const match = regExp.exec(currentVariableString);
18
- if (!match) {
19
- done = true;
20
- break;
21
- }
22
- const variableLevels = match[1].split('.');
14
+ const regExp = /\{([^}]+)\}/g;
15
+ const replacedString = variableString.replace(regExp, (match, group) => {
16
+ const variableLevels = group.split('.');
23
17
  let replacementValue = data;
24
18
  for (let i = 0; i < variableLevels.length; i++) {
25
- replacementValue = replacementValue[variableLevels[i]];
19
+ if (replacementValue) {
20
+ replacementValue = replacementValue[variableLevels[i]];
21
+ }
22
+ else {
23
+ replacementValue = undefined;
24
+ }
26
25
  }
27
26
  if (quoteStrings && StringUtil.isString(replacementValue)) {
28
27
  replacementValue = JSON.stringify(replacementValue);
@@ -37,9 +36,12 @@ class StringUtil {
37
36
  }
38
37
  replacementValue = '[' + replacementValue + ']';
39
38
  }
40
- currentVariableString = currentVariableString.replace(match[0], replacementValue);
41
- }
42
- return currentVariableString;
39
+ if (Object.prototype.toString.call(replacementValue) === '[object Object]') {
40
+ replacementValue = JSON.stringify(replacementValue);
41
+ }
42
+ return replacementValue;
43
+ });
44
+ return replacedString;
43
45
  }
44
46
  static convertToCamelCase(str) {
45
47
  str = str.replace(/-/g, ' ');
@@ -60,3 +62,4 @@ class StringUtil {
60
62
  }
61
63
  }
62
64
  exports.StringUtil = StringUtil;
65
+ StringUtil.replaceAllWithFunction = replaceAllWithFunction_1.replaceAllWithFunction;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.0.27",
3
+ "version": "1.0.29",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",