From 2df3c500e883c89ce1dcf0696708174c16778256 Mon Sep 17 00:00:00 2001 From: Aliullov Vlad <91639107+GoodDayForSurf@users.noreply.github.com> Date: Tue, 10 Sep 2024 14:38:21 +0400 Subject: [PATCH] Add support all options of collator sensitivity for filtering (#28017) --- packages/devextreme/js/core/utils/data.js | 10 +++-- .../tests/DevExpress.data/queryArray.tests.js | 45 ++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/packages/devextreme/js/core/utils/data.js b/packages/devextreme/js/core/utils/data.js index a672c46ea00f..0daeed1c41e4 100644 --- a/packages/devextreme/js/core/utils/data.js +++ b/packages/devextreme/js/core/utils/data.js @@ -198,13 +198,17 @@ export const toComparable = function(value, caseSensitive, options = {}) { if(value && value instanceof Class && value.valueOf) { value = value.valueOf(); - } else if(typeof value === 'string' && collatorSensitivity === 'base') { + } else if(typeof value === 'string' && (collatorSensitivity === 'base' || collatorSensitivity === 'case')) { const REMOVE_DIACRITICAL_MARKS_REGEXP = /[\u0300-\u036f]/g; - value = toLowerCase(value, options).normalize('NFD').replace(REMOVE_DIACRITICAL_MARKS_REGEXP, ''); + if(collatorSensitivity === 'base') { + value = toLowerCase(value, options); + } + + value = value.normalize('NFD').replace(REMOVE_DIACRITICAL_MARKS_REGEXP, ''); } - const isCaseSensitive = caseSensitive || collatorSensitivity === 'case'; + const isCaseSensitive = caseSensitive || collatorSensitivity === 'case' || collatorSensitivity === 'variant'; if(typeof value === 'string' && !isCaseSensitive) { const locale = options?.locale?.toLowerCase(); diff --git a/packages/devextreme/testing/tests/DevExpress.data/queryArray.tests.js b/packages/devextreme/testing/tests/DevExpress.data/queryArray.tests.js index e207925ef03f..3dcef8b21a72 100644 --- a/packages/devextreme/testing/tests/DevExpress.data/queryArray.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.data/queryArray.tests.js @@ -350,14 +350,33 @@ QUnit.test('filter with functional getter', function(assert) { }); }); -QUnit.test('filter with undefined "langParams"', function(assert) { - const input = [{ ID: 'AAA', Name: 'Name 2' }, { ID: 'aaa', Name: 'Name 3' }]; - const filterLength = QUERY(input).filter(['ID', '=', 'aaa']).toArray().length; - assert.equal(filterLength, 2); +QUnit.test('filter with undefined "langParams" (behaviour like collatorOptions.sensitivity set to "accent")', function(assert) { + const input = [{ ID: 'AAA' }, { ID: 'aaa' }, { ID: 'ááá' }, { ID: 'bbb' }]; + const array = QUERY(input).filter(['ID', '=', 'aaa']).toArray(); + + assert.equal(array.length, 2); + assert.equal(array[0].ID, 'AAA'); + assert.equal(array[1].ID, 'aaa'); +}); + +QUnit.test('filter with collatorOptions.sensitivity set to "accent"', function(assert) { + const input = [{ ID: 'AAA' }, { ID: 'aaa' }, { ID: 'ááá' }, { ID: 'bbb' }]; + + const array = QUERY(input, { + langParams: { + collatorOptions: { + sensitivity: 'accent' + } + } + }).filter(['ID', '=', 'aaa']).toArray(); + + assert.equal(array.length, 2); + assert.equal(array[0].ID, 'AAA'); + assert.equal(array[1].ID, 'aaa'); }); QUnit.test('filter with collatorOptions.sensitivity set to "case"', function(assert) { - const input = [{ ID: 'AAA', Name: 'Name 2' }, { ID: 'aaa', Name: 'Name 3' }]; + const input = [{ ID: 'AAA' }, { ID: 'aaa' }, { ID: 'ááá' }, { ID: 'bbb' }]; const array = QUERY(input, { langParams: { @@ -367,6 +386,22 @@ QUnit.test('filter with collatorOptions.sensitivity set to "case"', function(ass } }).filter(['ID', '=', 'aaa']).toArray(); + assert.equal(array.length, 2); + assert.equal(array[0].ID, 'aaa'); + assert.equal(array[1].ID, 'ááá'); +}); + +QUnit.test('filter with collatorOptions.sensitivity set to "variant"', function(assert) { + const input = [{ ID: 'AAA' }, { ID: 'aaa' }, { ID: 'ááá' }, { ID: 'bbb' }]; + + const array = QUERY(input, { + langParams: { + collatorOptions: { + sensitivity: 'variant' + } + } + }).filter(['ID', '=', 'aaa']).toArray(); + assert.equal(array.length, 1); assert.equal(array[0].ID, 'aaa'); });