[FIX] SanitizeLikeString util crashes for empty strings (#2471)

This commit is contained in:
Diego Mello 2020-09-16 16:08:25 -03:00 committed by GitHub
parent 7b67ae4757
commit fb3e8bc203
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

View File

@ -2,6 +2,6 @@ import XRegExp from 'xregexp';
// Matches letters from any alphabet and numbers // Matches letters from any alphabet and numbers
const likeStringRegex = new XRegExp('[^\\p{L}\\p{Nd}]', 'g'); const likeStringRegex = new XRegExp('[^\\p{L}\\p{Nd}]', 'g');
export const sanitizeLikeString = str => str.replace(likeStringRegex, '_'); export const sanitizeLikeString = str => str?.replace(likeStringRegex, '_');
export const sanitizer = r => r; export const sanitizer = r => r;

View File

@ -4,9 +4,14 @@ import * as utils from './utils';
describe('sanitizeLikeStringTester', () => { describe('sanitizeLikeStringTester', () => {
// example chars that shouldn't return // example chars that shouldn't return
const disallowedChars = ',./;[]!@#$%^&*()_-=+~'; const disallowedChars = ',./;[]!@#$%^&*()_-=+~';
const sanitizeLikeStringTester = str => expect(utils.sanitizeLikeString(`${ str }${ disallowedChars }`)).toBe(`${ str }${ '_'.repeat(disallowedChars.length) }`); const sanitizeLikeStringTester = str => expect(utils.sanitizeLikeString(`${ str }${ disallowedChars }`)).toBe(`${ str }${ '_'.repeat(disallowedChars.length) }`);
test('render empty', () => {
expect(utils.sanitizeLikeString(null)).toBe(undefined);
expect(utils.sanitizeLikeString('')).toBe('');
expect(utils.sanitizeLikeString(undefined)).toBe(undefined);
});
// Testing a couple of different alphabets // Testing a couple of different alphabets
test('render test (latin)', () => { test('render test (latin)', () => {
sanitizeLikeStringTester('test123'); sanitizeLikeStringTester('test123');