Fix useless regex escapes (#150043)

This commit is contained in:
Thomas Watson 2023-02-07 17:58:47 +01:00 committed by GitHub
parent eef575add4
commit 6ad7ba3ddb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 5 deletions

View file

@ -380,7 +380,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if (!opts.axisLabelUseHtml &&
navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
var re = /MSIE ([0-9]{1,}[.0-9]{0,})/;
if (re.exec(ua) != null) {
rv = parseFloat(RegExp.$1);
}

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { escapeSearchQueryPhrase } from './saved_object';
import { escapeSearchQueryPhrase, normalizeKuery } from './saved_object';
describe('Saved object service', () => {
describe('escapeSearchQueryPhrase', () => {
@ -21,4 +21,24 @@ describe('Saved object service', () => {
expect(res).toEqual(`"test1\\"test2"`);
});
});
describe('normalizeKuery', () => {
it('without attributes postfix', () => {
const res = normalizeKuery('foo', 'foo.');
expect(res).toEqual('foo.attributes.');
});
it('with attributes postfix', () => {
const res = normalizeKuery('foo', 'foo.attributes.');
expect(res).toEqual('foo.attributes.');
});
it('only trigger on literal dots', () => {
const res = normalizeKuery('foo', 'foobar');
expect(res).toEqual('foobar');
});
});
});

View file

@ -25,7 +25,7 @@ export function escapeSearchQueryPhrase(val: string): string {
// filtering SOs
export const normalizeKuery = (savedObjectType: string, kuery: string): string => {
return kuery.replace(
new RegExp(`${savedObjectType}\.(?!attributes\.)`, 'g'),
new RegExp(`${savedObjectType}\\.(?!attributes\\.)`, 'g'),
`${savedObjectType}.attributes.`
);
};

View file

@ -10,5 +10,5 @@ export const resolvePathVariables = (
variables: { [K: string]: string | number }
): string =>
Object.keys(variables).reduce((acc, paramName) => {
return acc.replace(new RegExp(`\{${paramName}\}`, 'g'), String(variables[paramName]));
return acc.replace(new RegExp(`\\{${paramName}\\}`, 'g'), String(variables[paramName]));
}, path);

View file

@ -36,7 +36,7 @@ type FindExceptionListItemOptions = Parameters<ExceptionListClient['findExceptio
const FILTER_PROPERTY_PREFIX = 'exception-list-agnostic\\.attributes';
const FILTER_REGEXP = new RegExp(
`^${FILTER_PROPERTY_PREFIX}\.os_types:"([^"]+)"( and \\(${FILTER_PROPERTY_PREFIX}\.tags:"policy:all"( or ${FILTER_PROPERTY_PREFIX}\.tags:"policy:([^"]+)")?\\))?$`
`^${FILTER_PROPERTY_PREFIX}\\.os_types:"([^"]+)"( and \\(${FILTER_PROPERTY_PREFIX}\\.tags:"policy:all"( or ${FILTER_PROPERTY_PREFIX}\\.tags:"policy:([^"]+)")?\\))?$`
);
export const mockFindExceptionListItemResponses = (