Skip to content

Commit

Permalink
Add support all options of collator sensitivity for filtering (#28017)
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodDayForSurf authored Sep 10, 2024
1 parent 858adfe commit 2df3c50
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
10 changes: 7 additions & 3 deletions packages/devextreme/js/core/utils/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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');
});
Expand Down

0 comments on commit 2df3c50

Please sign in to comment.