Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

likhith/feat:incorporated string normalization to match non english characters #7649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Icon from '../icon';
import Input from '../input';
import DropdownList from '../dropdown-list';
import { useBlockScroll } from '../../hooks/use-blockscroll';
import { getEnglishCharacters } from '../../../utils/helper';

const KEY_CODE = {
ENTER: 13,
Expand All @@ -23,9 +24,10 @@ const getFilteredItems = (val, list, should_filter_by_char) => {
is_string_array ? matchStringByChar(item, val) : matchStringByChar(item.text, val)
);
}

return list.filter(item =>
is_string_array ? item.toLowerCase().includes(val) : item.text.toLowerCase().includes(val)
is_string_array
? getEnglishCharacters(item).toLowerCase().includes(val) || item.toLowerCase().includes(val)
: getEnglishCharacters(item.text).toLowerCase().includes(val) || item.text.toLowerCase().includes(val)
Copy link
Contributor

@amina-deriv amina-deriv Feb 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt we add the or condtion when is_string_array is true. will this work?


const getNormalisedValue = (item)=> getEnglishCharacters(item).toLowerCase().includes(val)|| item.toLowerCase().includes(val);
 return list.filter(item =>
        is_string_array
            ? getNormalisedValue(item):getNormalisedValue(item.text)

);
};
const Autocomplete = React.memo(props => {
Expand Down
17 changes: 17 additions & 0 deletions packages/components/utils/__tests__/helper.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { getEnglishCharacters } = require('../helper');

describe('Utility/Helper', () => {
it('should return english alphabets removing special characters', () => {
const test_input = 'Ai Cập';
const test_result = 'Ai Cap';

expect(getEnglishCharacters(test_input)).toMatch(test_result);
});

it('should return empty string if the text contains no english characters', () => {
const test_input = '埃及';
const test_result = '';

expect(getEnglishCharacters(test_input)).toMatch(test_result);
});
});
8 changes: 8 additions & 0 deletions packages/components/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,16 @@ const getPascalCase = str => {

const getFileNameFromPath = path => path.match(/([^/]*)\/*$/)[1].replace('.svg', '');

const getEnglishCharacters = input =>
input
.normalize('NFD')
.split('')
.filter(char => /^[a-z ]*$/i.test(char))
.join('');

module.exports = {
getPascalCase,
getFileNameFromPath,
getKebabCase,
getEnglishCharacters,
};