mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
d409853683
commit
35a581a2ba
@ -51,11 +51,16 @@ const numericComparer = (a: number, b: number): number => {
|
|||||||
return a - b;
|
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 => {
|
const stringComparer = (a: string, b: string): number => {
|
||||||
if (!a || !b) {
|
if (!a || !b) {
|
||||||
return falsyComparer(a, b);
|
return falsyComparer(a, b);
|
||||||
}
|
}
|
||||||
return a.localeCompare(b);
|
|
||||||
|
return compare(String(a), String(b));
|
||||||
};
|
};
|
||||||
|
|
||||||
const booleanComparer = (a: boolean, b: boolean): number => {
|
const booleanComparer = (a: boolean, b: boolean): number => {
|
||||||
|
@ -31,7 +31,7 @@ describe('Grouping to Matrix', () => {
|
|||||||
const expected: Field[] = [
|
const expected: Field[] = [
|
||||||
{
|
{
|
||||||
name: 'Time\\Time',
|
name: 'Time\\Time',
|
||||||
type: FieldType.string,
|
type: FieldType.time,
|
||||||
values: [1000, 1001, 1002],
|
values: [1000, 1001, 1002],
|
||||||
config: {},
|
config: {},
|
||||||
},
|
},
|
||||||
@ -133,7 +133,7 @@ describe('Grouping to Matrix', () => {
|
|||||||
const expected: Field[] = [
|
const expected: Field[] = [
|
||||||
{
|
{
|
||||||
name: 'Time\\Time',
|
name: 'Time\\Time',
|
||||||
type: FieldType.string,
|
type: FieldType.time,
|
||||||
values: [1000, 1001],
|
values: [1000, 1001],
|
||||||
config: {},
|
config: {},
|
||||||
},
|
},
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
|
|
||||||
import { getFieldDisplayName } from '../../field/fieldState';
|
import { getFieldDisplayName } from '../../field/fieldState';
|
||||||
import { DataFrame, Field, FieldType } from '../../types/dataFrame';
|
import { DataFrame, Field } from '../../types/dataFrame';
|
||||||
import {
|
import {
|
||||||
SpecialValue,
|
SpecialValue,
|
||||||
DataTransformerInfo,
|
DataTransformerInfo,
|
||||||
@ -106,7 +106,7 @@ export const groupingToMatrixTransformer: DataTransformerInfo<GroupingToMatrixTr
|
|||||||
{
|
{
|
||||||
name: rowColumnField,
|
name: rowColumnField,
|
||||||
values: rowValues,
|
values: rowValues,
|
||||||
type: FieldType.string,
|
type: keyRowField.type,
|
||||||
config: {},
|
config: {},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
@ -145,12 +145,7 @@ export const groupingToMatrixTransformer: DataTransformerInfo<GroupingToMatrixTr
|
|||||||
};
|
};
|
||||||
|
|
||||||
function uniqueValues<T>(values: T[]): T[] {
|
function uniqueValues<T>(values: T[]): T[] {
|
||||||
const unique = new Set<T>();
|
const unique = new Set<T>(values);
|
||||||
|
|
||||||
for (let index = 0; index < values.length; index++) {
|
|
||||||
unique.add(values[index]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Array.from(unique);
|
return Array.from(unique);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user