From caafea7b24f711c7e7d16c9f5da11cc973796e6c Mon Sep 17 00:00:00 2001 From: vitPinchuk Date: Fri, 13 Dec 2024 00:58:59 +0300 Subject: [PATCH 1/2] add normalize for boolean type --- package-lock.json | 4 +- .../BooleanCellRenderer.tsx | 10 ++++- src/utils/table.test.ts | 37 +++++++++++++++++++ src/utils/table.ts | 36 ++++++++++++++++++ 4 files changed, 83 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e815330..6bbca3bc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "business-table", - "version": "1.9.0", + "version": "2.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "business-table", - "version": "1.9.0", + "version": "2.0.0", "license": "Apache-2.0", "dependencies": { "@emotion/css": "^11.13.0", diff --git a/src/components/Table/components/CellRenderer/components/BooleanCellRenderer/BooleanCellRenderer.tsx b/src/components/Table/components/CellRenderer/components/BooleanCellRenderer/BooleanCellRenderer.tsx index db0b7ae9..4686e650 100644 --- a/src/components/Table/components/CellRenderer/components/BooleanCellRenderer/BooleanCellRenderer.tsx +++ b/src/components/Table/components/CellRenderer/components/BooleanCellRenderer/BooleanCellRenderer.tsx @@ -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 @@ -34,10 +35,15 @@ export const BooleanCellRenderer: React.FC = ({ value, bgColor }) => { */ const theme = useTheme2(); + /** + * Normalized value + */ + const normalizedValue = useMemo(() => normalizeBooleanCellValue(value), [value]); + return ( { 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); + }); + }); }); diff --git a/src/utils/table.ts b/src/utils/table.ts index e8d88e4d..0f0cf64b 100644 --- a/src/utils/table.ts +++ b/src/utils/table.ts @@ -478,3 +478,39 @@ export const convertTableToDataFrame = (table: TableInstance): Dat */ export const createColumnAccessorFn = (accessorKey: string) => (row: unknown) => (row as Record)[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; +}; From afc2234b7c1ebf2e1104766ed8be148c45add8e6 Mon Sep 17 00:00:00 2001 From: Mikhail Volkov Date: Sun, 15 Dec 2024 22:06:49 -0500 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35092265..f400317b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)