diff --git a/CHANGELOG.md b/CHANGELOG.md index 3509226..f400317 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) diff --git a/package-lock.json b/package-lock.json index 3685355..17c57a8 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 db0b7ae..4686e65 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 e8d88e4..0f0cf64 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; +};