|
1 | | -import { dashToPascalCase, isDef, mergeIntoWith, toCamelCase, toDashCase } from '../helpers'; |
| 1 | +import { dashToPascalCase, escapeWithPattern, isDef, mergeIntoWith, toCamelCase, toDashCase } from '../helpers'; |
2 | 2 |
|
3 | 3 | describe('util helpers', () => { |
4 | 4 | describe('dashToPascalCase', () => { |
@@ -107,4 +107,63 @@ describe('util helpers', () => { |
107 | 107 | ]); |
108 | 108 | }); |
109 | 109 | }); |
| 110 | + |
| 111 | + describe('escapeWithPattern', () => { |
| 112 | + it('replaces all occurrences of a string pattern by default', () => { |
| 113 | + const text = 'foo/bar foo/bar foo/bar'; |
| 114 | + const pattern = '/'; |
| 115 | + const replacement = '\\/'; |
| 116 | + expect(escapeWithPattern(text, pattern, replacement)).toBe('foo\\/bar foo\\/bar foo\\/bar'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('replaces only first occurrence if replaceAll is false', () => { |
| 120 | + const text = 'foo/bar foo/bar foo/bar'; |
| 121 | + const pattern = '/'; |
| 122 | + const replacement = '\\/'; |
| 123 | + expect(escapeWithPattern(text, pattern, replacement, false)).toBe('foo\\/bar foo/bar foo/bar'); |
| 124 | + }); |
| 125 | + |
| 126 | + it('replaces all occurrences using a RegExp pattern with no g flag by default', () => { |
| 127 | + const text = 'a+b+c+a+b+c'; |
| 128 | + const pattern = /\+/; // no 'g' flag |
| 129 | + const replacement = '-'; |
| 130 | + expect(escapeWithPattern(text, pattern, replacement)).toBe('a-b-c-a-b-c'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('replaces only first occurrence if replaceAll is false with RegExp', () => { |
| 134 | + const text = 'a+b+c+a+b+c'; |
| 135 | + const pattern = /\+/; |
| 136 | + const replacement = '-'; |
| 137 | + expect(escapeWithPattern(text, pattern, replacement, false)).toBe('a-b+c+a+b+c'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('respects the g flag if RegExp already has it and replaceAll true', () => { |
| 141 | + const text = 'x*y*z*x*y*z'; |
| 142 | + const pattern = /\*/g; |
| 143 | + const replacement = '-'; |
| 144 | + expect(escapeWithPattern(text, pattern, replacement, true)).toBe('x-y-z-x-y-z'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('removes the g flag if replaceAll is false', () => { |
| 148 | + const text = 'x*y*z*x*y*z'; |
| 149 | + const pattern = /\*/g; |
| 150 | + const replacement = '-'; |
| 151 | + expect(escapeWithPattern(text, pattern, replacement, false)).toBe('x-y*z*x*y*z'); |
| 152 | + }); |
| 153 | + |
| 154 | + it('escapes special RegExp chars in string pattern', () => { |
| 155 | + const text = 'foo.*+?^${}()|[]\\bar'; |
| 156 | + const pattern = '.*+?^${}()|[]\\'; |
| 157 | + const replacement = '-ESCAPED-'; |
| 158 | + expect(escapeWithPattern(text, pattern, replacement)).toBe('foo-ESCAPED-bar'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('works with empty string input', () => { |
| 162 | + expect(escapeWithPattern('', 'a', 'b')).toBe(''); |
| 163 | + }); |
| 164 | + |
| 165 | + it('works with empty replacement', () => { |
| 166 | + expect(escapeWithPattern('abcabc', 'a', '')).toBe('bcbc'); |
| 167 | + }); |
| 168 | + }); |
110 | 169 | }); |
0 commit comments