Skip to content

Commit

Permalink
Fix filtering issues for string in different locales (T1242756)
Browse files Browse the repository at this point in the history
+ refactor
  • Loading branch information
GoodDayForSurf committed Aug 30, 2024
1 parent 53707b8 commit 2431491
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
15 changes: 12 additions & 3 deletions packages/devextreme/js/core/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ function combineGetters(getters) {
};
}


function toLowerCase(value, options) {
return options?.locale ? value.toLocaleLowerCase(options.locale) : value.toLowerCase();
}

function toUpperCase(value, options) {
return options?.locale ? value.toLocaleUpperCase(options.locale) : value.toUpperCase();
}

const ensurePropValueDefined = function(obj, propName, value, options) {
if(isDefined(value)) {
return value;
Expand Down Expand Up @@ -181,7 +190,7 @@ export const compileSetter = function(expr) {
};
};

export const toComparable = function(value, caseSensitive, options = {}) {
export const toComparable = function(value, caseSensitive, langParams = {}, options = { useUpperCase: false }) {
if(value instanceof Date) {
return value.getTime();
}
Expand All @@ -191,13 +200,13 @@ export const toComparable = function(value, caseSensitive, options = {}) {
}

if(!caseSensitive && typeof value === 'string') {
if(options?.collatorOptions?.sensitivity === 'base') {
if(langParams?.collatorOptions?.sensitivity === 'base') {
const REMOVE_DIACRITICAL_MARKS_REGEXP = /[\u0300-\u036f]/g;

value = value.normalize('NFD').replace(REMOVE_DIACRITICAL_MARKS_REGEXP, '');
}

return options?.locale ? value.toLocaleLowerCase(options.locale) : value.toLowerCase();
return (options?.useUpperCase ? toUpperCase : toLowerCase)(value, langParams);
}

return value;
Expand Down
6 changes: 5 additions & 1 deletion packages/devextreme/js/data/array_query.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ const SortIterator = Iterator.inherit({

const compileCriteria = (function() {
let langParams = {};
let useUpperCase = false;

const _toComparable = (value) => toComparable(value, false, langParams);
const _toComparable = (value) => toComparable(value, false, langParams, { useUpperCase });

const compileUniformEqualsCriteria = (crit) => {
const getter = compileGetter(crit[0][0]);
Expand Down Expand Up @@ -384,6 +385,9 @@ const compileCriteria = (function() {

return function(crit, options) {
langParams = options || {};
const locale = langParams?.locale?.toLowerCase();

useUpperCase = locale && !!['hy', 'el'].find((code) => locale === code || locale.startsWith(`${code}-`));

if(isFunction(crit)) {
return crit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,69 @@ QUnit.test('filter with functional getter', function(assert) {
});
});

QUnit.test('filtering use correct case insensitivity equal', function(assert) {
const input = [{ ID: 1, Name: 'AΙΤΗΣ' }, { ID: 2, Name: 'aιτης' }, { ID: 3, Name: 'abcde' }];

const array = QUERY(input, {
langParams: {
locale: 'el-GR'
}
}).filter(['Name', '=', 'AΙΤΗΣ']).toArray();

assert.equal(array.length, 2);

const containsUnwantedValue = array.some(item => item.ID === 3);
assert.false(containsUnwantedValue);
});

QUnit.test('filtering use correct case insensitivity search', function(assert) {
const input = [
{ ID: 1, Name: 'AΙΤΗΣ' },
{ ID: 2, Name: 'aιτης' },
{ ID: 3, Name: 'aιτησa' },
{ ID: 4, Name: 'AΙΤΗΣΗ' },
{ ID: 5, Name: 'ΑBΤΗΣΗ' },
{ ID: 6, Name: 'BΑΙΤΗΣΗ' },
{ ID: 7, Name: 'baιτησa' },
];

const query = QUERY(input, {
langParams: {
locale: 'el-GR'
}
});

const arrayStartsWith = query.filter(['Name', 'startswith', 'AΙΤΗΣ']).toArray();

const arrayEndsWith = query.filter(['Name', 'endswith', 'ΙΤΗΣ']).toArray();

const arrayContains = query.filter(['Name', 'contains', 'ΙΤΗΣ']).toArray();

assert.equal(arrayStartsWith.length, 4);
assert.equal(arrayEndsWith.length, 2);
assert.equal(arrayContains.length, 6);

const containsUnwantedValue = arrayStartsWith.some(item => [5, 6, 7].includes(item.ID))
|| arrayEndsWith.some(item => [5, 4, 3, 6, 7].includes(item.ID))
|| arrayContains.some(item => [5].includes(item.ID));
assert.false(containsUnwantedValue);
});

QUnit.test('filtering use correct case insensitivity search for Armenian locale', function(assert) {
const input = [
{ ID: 1, Name: 'ԵՐԵՒԱՆ' },
{ ID: 2, Name: 'Երևան' },
];

const arrayStartsWith = QUERY(input, {
langParams: {
locale: 'hy'
}
}).filter(['Name', 'startswith', 'Երև']).toArray();

assert.equal(arrayStartsWith.length, 2);
});

QUnit.test('missing operation means equal', function(assert) {
assert.expect(1);

Expand Down

0 comments on commit 2431491

Please sign in to comment.