feat(utils): improve normalizeFileName to support Unicode characters

- Update the `normalizeFileName` function to use a RegExp that supports Unicode characters
- Add tests for `normalizeFileName` with various Unicode languages
This commit is contained in:
luoxiaohei 2023-10-13 17:58:34 +08:00
parent 6b668aaebe
commit f21d78f887
2 changed files with 14 additions and 4 deletions

View File

@ -68,10 +68,9 @@ export const normalizeFileName = (name) => {
return name;
}
const validChars = /[^\w\s-]/g;
const formattedName = name.replace(validChars, '-');
return formattedName;
// Use Unicode character classes to match alphanumeric characters, spaces, hyphens, and underscores
const validChars = new RegExp('[^\\p{L}\\p{M}\\p{N}\\s_-]', 'gu');
return name.replace(validChars, '-');
};
export const getContentType = (headers) => {

View File

@ -15,5 +15,16 @@ describe('common utils', () => {
expect(normalizeFileName('foo/bar/')).toBe('foo-bar-');
expect(normalizeFileName('foo\\bar\\')).toBe('foo-bar-');
});
it('should support unicode', () => {
// more unicode languages
expect(normalizeFileName('你好世界!?@#$%^&*()')).toBe('你好世界-----------');
expect(normalizeFileName('こんにちは世界!?@#$%^&*()')).toBe('こんにちは世界-----------');
expect(normalizeFileName('안녕하세요 세계!?@#$%^&*()')).toBe('안녕하세요 세계-----------');
expect(normalizeFileName('مرحبا بالعالم!?@#$%^&*()')).toBe('مرحبا بالعالم-----------');
expect(normalizeFileName('Здравствуй мир!?@#$%^&*()')).toBe('Здравствуй мир-----------');
expect(normalizeFileName('नमस्ते दुनिया!?@#$%^&*()')).toBe('नमस्ते दुनिया-----------');
expect(normalizeFileName('สวัสดีชาวโลก!?@#$%^&*()')).toBe('สวัสดีชาวโลก-----------');
expect(normalizeFileName('γειά σου κόσμος!?@#$%^&*()')).toBe('γειά σου κόσμος-----------');
});
});
});