Skip to content

Commit

Permalink
Add normalize for boolean type (#198)
Browse files Browse the repository at this point in the history
* add normalize for boolean type

* Update CHANGELOG.md

---------

Co-authored-by: Mikhail Volkov <[email protected]>
  • Loading branch information
vitPinchuk and mikhail-vl authored Dec 16, 2024
1 parent b118067 commit 7c44113
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Updated wrap column text by word (#195)
- Updated packages for Code Editor (#194)
- Added normalize for boolean type (#198)

## 1.9.0 (2024-12-01)

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Icon, useTheme2 } from '@grafana/ui';
import React from 'react';
import React, { useMemo } from 'react';

import { TEST_IDS } from '@/constants';
import { normalizeBooleanCellValue } from '@/utils';

/**
* Properties
Expand Down Expand Up @@ -34,10 +35,15 @@ export const BooleanCellRenderer: React.FC<Props> = ({ value, bgColor }) => {
*/
const theme = useTheme2();

/**
* Normalized value
*/
const normalizedValue = useMemo(() => normalizeBooleanCellValue(value), [value]);

return (
<Icon
{...TEST_IDS.booleanCellRenderer.root.apply()}
name={value ? 'check-circle' : 'circle'}
name={normalizedValue ? 'check-circle' : 'circle'}
size="lg"
style={{
color: bgColor ? theme.colors.getContrastText(bgColor) : 'inherit',
Expand Down
37 changes: 37 additions & 0 deletions src/utils/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import {

import {
columnFilter,
convertStringValueToBoolean,
convertTableToDataFrame,
createColumnAccessorFn,
getFilterWithNewType,
getFooterCell,
getSupportedFilterTypesForVariable,
getVariableColumnFilters,
mergeColumnFilters,
normalizeBooleanCellValue,
} from './table';
import {
createColumnConfig,
Expand Down Expand Up @@ -1235,4 +1237,39 @@ describe('Table utils', () => {
expect(createColumnAccessorFn('comment.info.name')({ 'comment.info.name': 'hello' })).toEqual('hello');
});
});

/**
* convertStringValueToBoolean
*/
describe('convertStringValueToBoolean', () => {
it.each([
['true', true],
['yes', true],
['1', true],
['false', false],
['no', false],
['random', false],
['', false],
['0', false],
])('Should return value as boolean', (input, expected) => {
expect(convertStringValueToBoolean(input)).toBe(expected);
});
});

/**
* normalizeBooleanCellValue
*/
describe('normalizeBooleanCellValue', () => {
it.each([
[true, true],
[false, false],
['true', true],
['false', false],
['random', false],
[null, false],
['fals', false],
])('Should convert value to boolean', (value, expected) => {
expect(normalizeBooleanCellValue(value)).toBe(expected);
});
});
});
36 changes: 36 additions & 0 deletions src/utils/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,39 @@ export const convertTableToDataFrame = <TData>(table: TableInstance<TData>): Dat
*/
export const createColumnAccessorFn = (accessorKey: string) => (row: unknown) =>
(row as Record<string, unknown>)[accessorKey];

/**
* Convert available string value to boolean
*/
export const convertStringValueToBoolean = (value: string): boolean => {
switch (value) {
case 'true':
case 'yes':
case '1': {
return true;
}
case 'false':
case 'no':
case '1': {
return false;
}
default: {
return false;
}
}
};

/**
* normalize Boolean Cell Value
*/
export const normalizeBooleanCellValue = (value: unknown): boolean => {
if (typeof value === 'boolean') {
return value;
}

if (typeof value === 'string') {
return convertStringValueToBoolean(value);
}

return false;
};

0 comments on commit 7c44113

Please sign in to comment.