Skip to content

Commit

Permalink
likhith/feat:incorporated string normalization to match non english c…
Browse files Browse the repository at this point in the history
…haracters (binary-com#7649)

* feat: ⚡ incorporated string normalization to match non english character

* fix: ensuring non english character match

* feat: resolved import issue

* feat: incorporated helper function

* feat: added testcases for helper function

* feat: incorporated helper function to all case

* feat: 🎨 refactored code

* feat: code refactor

---------

Co-authored-by: Matin shafiei <matin@deriv.com>
Co-authored-by: Carol Sachdeva <58209918+carol-deriv@users.noreply.github.com>
  • Loading branch information
3 people authored and mahdiyeh-deriv committed Mar 2, 2023
1 parent d32b4a4 commit 69c6a4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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)
);
};
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,
};

0 comments on commit 69c6a4c

Please sign in to comment.