-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(replaceParams): correctly substitute params with special content (#…
…29)
- Loading branch information
Showing
2 changed files
with
45 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}', | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |