Skip to content

Commit

Permalink
fix(replaceParams): correctly substitute params with special content (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Jun 28, 2022
1 parent a09a6b2 commit 4f791c5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
26 changes: 26 additions & 0 deletions src/replace-params.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {replaceParams} from './replace-params';

describe('replaceParams', () => {
it('should substitute params', () => {
expect(replaceParams('{{test}}', {test: 'text'})).toBe('text');
expect(replaceParams('{{test}}{{test}}', {test: 'text'})).toBe('texttext');
expect(replaceParams('some {{test}}', {test: 'text'})).toBe('some text');
expect(replaceParams('some {{test}} text', {test: 'cool'})).toBe('some cool text');
expect(replaceParams('{{test}} text', {test: 'cool'})).toBe('cool text');
expect(replaceParams('some {{test}} text {{test2}} !!', {test: 'cool', test2: 'hey'})).toBe('some cool text hey !!');
});
it('should not replace missing params', () => {
expect(replaceParams('{{test}}', {})).toBe('{{test}}');
expect(replaceParams('some {{test}}', {})).toBe('some {{test}}');
expect(replaceParams('some {{test}} text', {})).toBe('some {{test}} text');
expect(replaceParams('{{test}} text', {})).toBe('{{test}} text');
expect(replaceParams('some {{test}} text {{test2}} !!', {test: 'cool'})).toBe('some cool text {{test2}} !!');
})
it('should correctly substitute content with specials', () => {
expect(replaceParams('{{test}}', {test: '$'})).toBe('$');
expect(replaceParams('{{test}}', {test: '$$'})).toBe('$$');
expect(replaceParams('{{test1}} {{test2}}', {test1: '{{test2}}', test2: '{{test3}}', test3: 'content'})).toBe(
'{{test2}} {{test3}}',
);
});
});
28 changes: 19 additions & 9 deletions src/replace-params.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import {Params} from './types';

const PARAM_REGEXP = /{{(.*?)}}/g;

export function replaceParams(keyValue: string, params: Params): string {
let result = keyValue;
let result = '';

Object.keys(params).forEach((param) => {
let replacer = params[param];
if (typeof replacer === 'string' && replacer.indexOf('$') > -1) {
// заменить все одиночные символы '$' на '$$'
replacer = replacer.replace(/(?:([^$])\$|^\$)(?!\$)/g, '$1$$$$');
let lastIndex = (PARAM_REGEXP.lastIndex = 0);
let match;
while ((match = PARAM_REGEXP.exec(keyValue))) {
if (lastIndex !== match.index) {
result += keyValue.slice(lastIndex, match.index);
}
lastIndex = PARAM_REGEXP.lastIndex;

// eslint-disable-next-line security/detect-non-literal-regexp
result = result.replace(new RegExp(`({{${param}}})`, 'g'), replacer)
});
const [all, key] = match;
if (Object.prototype.hasOwnProperty.call(params, key)) {
result += params[key];
} else {
result += all;
}
}
if (lastIndex < keyValue.length) {
result += keyValue.slice(lastIndex);
}

return result;
}

0 comments on commit 4f791c5

Please sign in to comment.