Copilot commented on code in PR #37980:
URL: https://github.com/apache/superset/pull/37980#discussion_r2848896061
##########
superset-frontend/plugins/plugin-chart-table/test/utils/formatValue.test.ts:
##########
@@ -130,6 +130,96 @@ test('formatColumnValue handles null values', () => {
expect(nullResult).toBe('N/A');
});
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is null', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is empty string', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: '' },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
config has no d3SmallNumberFormat', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: {},
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue uses default formatter for value exactly 1
(boundary)', () => {
+ const formatter = getNumberFormatter(',.2f');
+ const column: DataColumnMeta = {
+ key: 'val',
+ label: 'Value',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, 1);
+ expect(result).toBe('1.00');
+});
+
+test('formatColumnValue uses default formatter for value exactly -1
(boundary)', () => {
+ const formatter = getNumberFormatter(',.2f');
+ const column: DataColumnMeta = {
+ key: 'val',
+ label: 'Value',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, -1);
+ expect(result).toBe('-1.00');
+});
+
+test('formatColumnValue uses small number formatter for value 0', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
Review Comment:
This test sets `config.d3SmallNumberFormat` to `null`, but
`TableColumnConfig.d3SmallNumberFormat` is typed as `string | undefined`
(strictNullChecks is on). This will fail `tsc` unless the type is updated to
allow `null` (preferred to match JSON round-trips) or the test uses an explicit
cast.
##########
superset-frontend/plugins/plugin-chart-table/test/utils/formatValue.test.ts:
##########
@@ -130,6 +130,96 @@ test('formatColumnValue handles null values', () => {
expect(nullResult).toBe('N/A');
});
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is null', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
Review Comment:
This test sets `config.d3SmallNumberFormat` to `null`, but
`TableColumnConfig.d3SmallNumberFormat` is typed as `string | undefined`
(strictNullChecks is on). This will fail `tsc` unless the type is updated to
allow `null` (preferred to match JSON round-trips) or the test uses an explicit
cast.
```suggestion
config: { d3SmallNumberFormat: null as unknown as string },
```
##########
superset-frontend/packages/superset-ui-core/src/number-format/getSmallNumberFormatter.ts:
##########
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { CurrencyFormatter } from '../currency-format';
+import { Currency } from '../query';
+import NumberFormatter from './NumberFormatter';
+import { getNumberFormatter } from './NumberFormatterRegistrySingleton';
Review Comment:
`getSmallNumberFormatter` introduces a circular dependency: it imports
`CurrencyFormatter` from `currency-format`, while
`currency-format/CurrencyFormatter` imports from the `number-format` barrel
(`../number-format`). This cycle can cause fragile initialization/bundling
issues; consider breaking the cycle (e.g., have `CurrencyFormatter` import
`getNumberFormatter`/`NumberFormats` from their direct modules instead of the
barrel, or move this helper out of `number-format`).
##########
superset-frontend/plugins/plugin-chart-table/test/utils/formatValue.test.ts:
##########
@@ -130,6 +130,96 @@ test('formatColumnValue handles null values', () => {
expect(nullResult).toBe('N/A');
});
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is null', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is empty string', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: '' },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
config has no d3SmallNumberFormat', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: {},
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue uses default formatter for value exactly 1
(boundary)', () => {
+ const formatter = getNumberFormatter(',.2f');
+ const column: DataColumnMeta = {
+ key: 'val',
+ label: 'Value',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
Review Comment:
This test sets `config.d3SmallNumberFormat` to `null`, but
`TableColumnConfig.d3SmallNumberFormat` is typed as `string | undefined`
(strictNullChecks is on). This will fail `tsc` unless the type is updated to
allow `null` (preferred to match JSON round-trips) or the test uses an explicit
cast.
##########
superset-frontend/plugins/plugin-chart-table/test/utils/formatValue.test.ts:
##########
@@ -130,6 +130,96 @@ test('formatColumnValue handles null values', () => {
expect(nullResult).toBe('N/A');
});
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is null', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
d3SmallNumberFormat is empty string', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: '' },
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue preserves percentage format for small numbers when
config has no d3SmallNumberFormat', () => {
+ const formatter = getNumberFormatter('.8%');
+ const column: DataColumnMeta = {
+ key: 'pct',
+ label: 'Percentage',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: {},
+ };
+
+ const [, result] = formatColumnValue(column, -0.00001229);
+ expect(result).toBe('-0.00122900%');
+});
+
+test('formatColumnValue uses default formatter for value exactly 1
(boundary)', () => {
+ const formatter = getNumberFormatter(',.2f');
+ const column: DataColumnMeta = {
+ key: 'val',
+ label: 'Value',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
+
+ const [, result] = formatColumnValue(column, 1);
+ expect(result).toBe('1.00');
+});
+
+test('formatColumnValue uses default formatter for value exactly -1
(boundary)', () => {
+ const formatter = getNumberFormatter(',.2f');
+ const column: DataColumnMeta = {
+ key: 'val',
+ label: 'Value',
+ dataType: GenericDataType.Numeric,
+ formatter,
+ isNumeric: true,
+ config: { d3SmallNumberFormat: null },
+ };
Review Comment:
This test sets `config.d3SmallNumberFormat` to `null`, but
`TableColumnConfig.d3SmallNumberFormat` is typed as `string | undefined`
(strictNullChecks is on). This will fail `tsc` unless the type is updated to
allow `null` (preferred to match JSON round-trips) or the test uses an explicit
cast.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]