Repository: exadel-inc/esl
Pull request #393 (2021-08-17 15:51:18)

Feat/add new format

github-actions[bot]: ****CLA Assistant Lite bot**** All contributors have signed the CLA  ✍️ 
ala-n: Reqires squash to merge changes with the correct commit description

Patch:
diff --git a/src/modules/esl-utils/misc/format.ts b/src/modules/esl-utils/misc/format.ts
index 3793a64f1..a4dcc99f8 100644
--- a/src/modules/esl-utils/misc/format.ts
+++ b/src/modules/esl-utils/misc/format.ts
@@ -50,9 +50,13 @@ export function evaluate(str: string, defaultValue?: any): any {
   }
 }
 
+
+/** Default RegExp to match replacements in the string for the {@link format} function */
+export const DEF_FORMAT_MATCHER = /{[{%]?([\w.]+)[%}]?}/g;
+
 /** Replace `{key}` patterns in the string from the source object */
-export function format(str: string, source: Record<string, any>) {
-  return str.replace(/{([\w.]+)}/g, (match, key) => {
+export function format(str: string, source: Record<string, any>, matcher: RegExp = DEF_FORMAT_MATCHER) {
+  return str.replace(matcher, (match, key) => {
     const val = get(source, key);
     return val === undefined ? match : val;
   });


Test patch:
diff --git a/src/modules/esl-utils/misc/test/format.test.ts b/src/modules/esl-utils/misc/test/format.test.ts
index b53339a9c..dd72048a8 100644
--- a/src/modules/esl-utils/misc/test/format.test.ts
+++ b/src/modules/esl-utils/misc/test/format.test.ts
@@ -1,4 +1,4 @@
-import {toCamelCase, toKebabCase, unwrapParenthesis, parseAspectRatio, evaluate, format, parseNumber} from '../format';
+import { toCamelCase, toKebabCase, unwrapParenthesis, parseAspectRatio, evaluate, format, parseNumber } from '../format';
 
 describe('misc/format helper tests', () => {
   test('toKebabCase', () => {
@@ -44,7 +44,7 @@ describe('misc/format helper tests', () => {
   });
   test('evaluate', () => {
     let throwError = false;
-    jest.spyOn(console, 'warn').mockImplementation(() => {throwError = true;});
+    jest.spyOn(console, 'warn').mockImplementation(() => { throwError = true; });
     expect(evaluate('0')).toBe(0);
     expect(evaluate('true')).toBe(true);
     expect(evaluate('false')).toBe(false);
@@ -52,8 +52,8 @@ describe('misc/format helper tests', () => {
     expect(evaluate('"0"')).toBe('0');
     expect(evaluate('[1, 2]')).toEqual([1, 2]);
     expect(evaluate('{}')).toEqual({});
-    expect(evaluate('{a: 1}')).toEqual({a: 1});
-    expect(evaluate('{"a": "1"}')).toEqual({a: '1'});
+    expect(evaluate('{a: 1}')).toEqual({ a: 1 });
+    expect(evaluate('{"a": "1"}')).toEqual({ a: '1' });
     expect(throwError).toBe(false);
     expect(evaluate('{')).toBe(undefined);
     expect(throwError).toBe(true);
@@ -63,11 +63,12 @@ describe('misc/format helper tests', () => {
   describe('format', () => {
     test.each([
       ['abc', {}, 'abc'],
-      ['abc{val}', {val: 'd'}, 'abcd'],
-      ['{a.b.c}', {a: {b: {c: 'hi'}}}, 'hi'],
+      ['abc{val}', { val: 'd' }, 'abcd'],
+      ['{a.b.c}', { a: { b: { c: 'hi' } } }, 'hi'],
       ['{a}', {}, '{a}'],
-      ['{a} - {b} - {c}', {a: 1, b: 2, c: 3}, '1 - 2 - 3'],
-      ['{a.b}{b.c}', {a: {b: 'h'}, b: {c: 'i'}}, 'hi'],
+      ['{a} - {b} - {c}', { a: 1, b: 2, c: 3 }, '1 - 2 - 3'],
+      ['{a.b}{b.c}', { a: { b: 'h' }, b: { c: 'i' } }, 'hi'],
+      ['abc{%foo%}-{{bar}}', { foo: 1, bar: 5 }, 'abc1-5'],
     ])('\'%s\' using %p to \'%s\'', (tmp: string, source: any, res: string) => {
       expect(format(tmp, source)).toBe(res);
     });