GroupingToMatrix: Type fix to resolve localeCompare error (#98710)

* draft: coerce types to string to prevent localCompare bug

* chore: update with comment for draft

* chore: cleanup and change fix

* fix: instantiate compare outside of functions scope

* chore: update tests!

* fix: make the linter happy again

* fix: remove unnecessary loop to generate array

* chore: revert forEach to for loops, other changes also

* chore: reduce diff

* chore: one more revert

---------

Co-authored-by: drew08t <drew08@gmail.com>
This commit is contained in:
Alex Spencer 2025-01-24 13:45:20 -07:00 committed by GitHub
parent d409853683
commit 35a581a2ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 11 deletions

View File

@ -51,11 +51,16 @@ const numericComparer = (a: number, b: number): number => {
return a - b;
};
// Using the Intl.Collator object compare method results in much faster
// string sorting than .localeCompare
const compare = new Intl.Collator('en', { sensitivity: 'base' }).compare;
const stringComparer = (a: string, b: string): number => {
if (!a || !b) {
return falsyComparer(a, b);
}
return a.localeCompare(b);
return compare(String(a), String(b));
};
const booleanComparer = (a: boolean, b: boolean): number => {

View File

@ -31,7 +31,7 @@ describe('Grouping to Matrix', () => {
const expected: Field[] = [
{
name: 'Time\\Time',
type: FieldType.string,
type: FieldType.time,
values: [1000, 1001, 1002],
config: {},
},
@ -133,7 +133,7 @@ describe('Grouping to Matrix', () => {
const expected: Field[] = [
{
name: 'Time\\Time',
type: FieldType.string,
type: FieldType.time,
values: [1000, 1001],
config: {},
},

View File

@ -1,7 +1,7 @@
import { map } from 'rxjs/operators';
import { getFieldDisplayName } from '../../field/fieldState';
import { DataFrame, Field, FieldType } from '../../types/dataFrame';
import { DataFrame, Field } from '../../types/dataFrame';
import {
SpecialValue,
DataTransformerInfo,
@ -106,7 +106,7 @@ export const groupingToMatrixTransformer: DataTransformerInfo<GroupingToMatrixTr
{
name: rowColumnField,
values: rowValues,
type: FieldType.string,
type: keyRowField.type,
config: {},
},
];
@ -145,12 +145,7 @@ export const groupingToMatrixTransformer: DataTransformerInfo<GroupingToMatrixTr
};
function uniqueValues<T>(values: T[]): T[] {
const unique = new Set<T>();
for (let index = 0; index < values.length; index++) {
unique.add(values[index]);
}
const unique = new Set<T>(values);
return Array.from(unique);
}