From 8ef584e59039969eec27d5b2cbfad1403946d6f4 Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 00:36:25 +0530 Subject: [PATCH 01/12] fix(legacy-plugin-chart-pivot-table): empty value for string aggregate --- .../src/PivotTable.js | 61 ++++++++----------- .../src/utils/formatCells.js | 38 ++++++++++++ .../test/PivotTable.test.tsx | 57 +++++++++++++++++ 3 files changed, 119 insertions(+), 37 deletions(-) create mode 100644 plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js create mode 100644 plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index b8bad848da..fdcd43e404 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -25,6 +25,7 @@ import { getTimeFormatterForGranularity, smartDateFormatter, } from '@superset-ui/core'; +import { replace, eachRow } from './utils/formatCells'; import fixTableHeight from './utils/fixTableHeight'; import 'datatables.net-bs/css/dataTables.bootstrap.css'; @@ -35,7 +36,7 @@ const $ = window.$ || dt.$; const propTypes = { data: PropTypes.shape({ - // TODO: replace this with raw data in SIP-6 + // TODO: replace this with raw data in SIP-69 html: PropTypes.string, columns: PropTypes.arrayOf( PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), @@ -77,49 +78,35 @@ function PivotTable(element, props) { container.innerHTML = html; const cols = Array.isArray(columns[0]) ? columns.map(col => col[0]) : columns; - // regex to parse dates const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; - // jQuery hack to set verbose names in headers - // eslint-disable-next-line func-name-matching - const replaceCell = function replace() { - const s = $(this)[0].textContent; - const regexMatch = dateRegex.exec(s); - let cellValue; - if (regexMatch) { - const date = new Date(parseFloat(regexMatch[1])); - cellValue = dateFormatter(date); - } else { - cellValue = verboseMap[s] || s; - } + $container.find('thead tr th').each(function () { + const cellValue = replace($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); $(this)[0].textContent = cellValue; - }; - $container.find('thead tr th').each(replaceCell); - $container.find('tbody tr th').each(replaceCell); + }); + + $container.find('tbody tr th').each(function () { + const cellValue = replace($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); + $(this)[0].textContent = cellValue; + }); - // jQuery hack to format number - $container.find('tbody tr').each(function eachRow() { + $container.find('tbody tr').each(function eachRo() { $(this) .find('td') - .each(function each(i) { - const metric = cols[i]; - const format = columnFormats[metric] || numberFormat || '.3s'; + .each(function eachTd(index) { + //metric = cols[index]; const tdText = $(this)[0].textContent; - const parsedValue = parseFloat(tdText); - if (Number.isNaN(parsedValue)) { - const regexMatch = dateRegex.exec(tdText); - if (regexMatch) { - const date = new Date(parseFloat(regexMatch[1])); - $(this)[0].textContent = dateFormatter(date); - $(this).attr('data-sort', date); - } else { - $(this)[0].textContent = ''; - $(this).attr('data-sort', Number.NEGATIVE_INFINITY); - } - } else { - $(this)[0].textContent = formatNumber(format, parsedValue); - $(this).attr('data-sort', parsedValue); - } + var { textContent, attr } = eachRow( + index, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + $(this)[0].textContent = textContent; + $(this).attr = attr; }); }); diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js new file mode 100644 index 0000000000..fdd29394d5 --- /dev/null +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js @@ -0,0 +1,38 @@ +import { formatNumber } from '@superset-ui/core'; + +function eachRow(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { + const metric = cols[i]; + const format = columnFormats[metric] || numberFormat || '.3s'; + const parsedValue = parseFloat(tdText); + var textContent, attr; + + if (Number.isNaN(parsedValue)) { + const regexMatch = dateRegex.exec(tdText); + if (regexMatch) { + const date = new Date(parseFloat(regexMatch[1])); + textContent = dateFormatter(date); + attr = ('data-sort', date); + } else { + textContent = tdText; + attr = ('data-sort', tdText); + } + } else { + textContent = formatNumber(format, parsedValue); + attr = ('data-sort', parsedValue); + } + return { textContent: textContent, attr: attr }; +} + +function replace(text, verboseMap, dateRegex, dateFormatter) { + const regexMatch = dateRegex.exec(text); + let cellValue; + if (regexMatch) { + const date = new Date(parseFloat(regexMatch[1])); + cellValue = dateFormatter(date); + } else { + cellValue = verboseMap[text] || text; + } + return cellValue; +} + +export { eachRow, replace }; diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx new file mode 100644 index 0000000000..10f5afbea6 --- /dev/null +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx @@ -0,0 +1,57 @@ +import { getTimeFormatter, getTimeFormatterForGranularity } from '@superset-ui/core'; +import { eachRow, replace } from '../src/utils/formatCells'; + +describe('pivot table plugin format cells', () => { + let i = 0; + let cols = ['SUM']; + let tdText = '2222222'; + let columnFormats = {}; + let numberFormat = 'SMART_NUMBER'; + let dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; + let dateFormatter = getTimeFormatterForGranularity('P1D'); + + it('render number', () => { + const { textContent, attr } = eachRow( + i, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + expect(textContent).toEqual('2.22M'); + expect(attr).toEqual(('data-sort', 2222222)); + }); + + it('render date', () => { + tdText = '__timestamp:-126230400000.0'; + + const { textContent, attr } = eachRow( + i, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + expect(textContent).toEqual('1966-01-01'); + }); + + it('render string', () => { + tdText = 'some-text'; + + const { textContent, attr } = eachRow( + i, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + expect(textContent).toEqual(tdText); + expect(attr).toEqual(('data-sort', tdText)); + }); +}); From 2df079556bf24a87c258a206545157b84eed9c0b Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 00:38:23 +0530 Subject: [PATCH 02/12] fix(plugin): bug fix --- plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index fdcd43e404..8d22081cb6 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -36,7 +36,7 @@ const $ = window.$ || dt.$; const propTypes = { data: PropTypes.shape({ - // TODO: replace this with raw data in SIP-69 + // TODO: replace this with raw data in SIP-6 html: PropTypes.string, columns: PropTypes.arrayOf( PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]), From be282eda532abdfaa4cef781c2b1b4db4dcbea93 Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 00:50:00 +0530 Subject: [PATCH 03/12] fix(plugin): fix test --- plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js | 8 ++++---- .../src/utils/formatCells.js | 6 +++--- .../test/PivotTable.test.tsx | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index 8d22081cb6..70ec1b4c41 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -25,7 +25,7 @@ import { getTimeFormatterForGranularity, smartDateFormatter, } from '@superset-ui/core'; -import { replace, eachRow } from './utils/formatCells'; +import { cellFormat, cellDateFormat } from './utils/formatCells'; import fixTableHeight from './utils/fixTableHeight'; import 'datatables.net-bs/css/dataTables.bootstrap.css'; @@ -81,12 +81,12 @@ function PivotTable(element, props) { const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; $container.find('thead tr th').each(function () { - const cellValue = replace($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); + const cellValue = cellDateFormat($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); $(this)[0].textContent = cellValue; }); $container.find('tbody tr th').each(function () { - const cellValue = replace($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); + const cellValue = cellDateFormat($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); $(this)[0].textContent = cellValue; }); @@ -96,7 +96,7 @@ function PivotTable(element, props) { .each(function eachTd(index) { //metric = cols[index]; const tdText = $(this)[0].textContent; - var { textContent, attr } = eachRow( + var { textContent, attr } = cellFormat( index, cols, tdText, diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js index fdd29394d5..baf8960fce 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js @@ -1,6 +1,6 @@ import { formatNumber } from '@superset-ui/core'; -function eachRow(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { +function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { const metric = cols[i]; const format = columnFormats[metric] || numberFormat || '.3s'; const parsedValue = parseFloat(tdText); @@ -23,7 +23,7 @@ function eachRow(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFo return { textContent: textContent, attr: attr }; } -function replace(text, verboseMap, dateRegex, dateFormatter) { +function cellDateFormat(text, verboseMap, dateRegex, dateFormatter) { const regexMatch = dateRegex.exec(text); let cellValue; if (regexMatch) { @@ -35,4 +35,4 @@ function replace(text, verboseMap, dateRegex, dateFormatter) { return cellValue; } -export { eachRow, replace }; +export { cellFormat, cellDateFormat }; diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx index 10f5afbea6..8b1f287443 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx @@ -1,5 +1,5 @@ import { getTimeFormatter, getTimeFormatterForGranularity } from '@superset-ui/core'; -import { eachRow, replace } from '../src/utils/formatCells'; +import { cellFormat } from '../src/utils/formatCells'; describe('pivot table plugin format cells', () => { let i = 0; @@ -11,7 +11,7 @@ describe('pivot table plugin format cells', () => { let dateFormatter = getTimeFormatterForGranularity('P1D'); it('render number', () => { - const { textContent, attr } = eachRow( + const { textContent, attr } = cellFormat( i, cols, tdText, @@ -27,7 +27,7 @@ describe('pivot table plugin format cells', () => { it('render date', () => { tdText = '__timestamp:-126230400000.0'; - const { textContent, attr } = eachRow( + const { textContent, attr } = cellFormat( i, cols, tdText, @@ -42,7 +42,7 @@ describe('pivot table plugin format cells', () => { it('render string', () => { tdText = 'some-text'; - const { textContent, attr } = eachRow( + const { textContent, attr } = cellFormat( i, cols, tdText, From dc4fe16a27d64f2114ec1afd4bf1c8240237d47c Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 00:51:26 +0530 Subject: [PATCH 04/12] fix(test): fixted function name --- plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index 70ec1b4c41..c4c01ac65d 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -90,7 +90,7 @@ function PivotTable(element, props) { $(this)[0].textContent = cellValue; }); - $container.find('tbody tr').each(function eachRo() { + $container.find('tbody tr').each(function eachRow() { $(this) .find('td') .each(function eachTd(index) { From d18453cc313b41ea2656b6522162ab823ab036b9 Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 01:30:40 +0530 Subject: [PATCH 05/12] lint fixed --- plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index c4c01ac65d..22eb99070a 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -20,7 +20,6 @@ import dt from 'datatables.net-bs'; import PropTypes from 'prop-types'; import { - formatNumber, getTimeFormatter, getTimeFormatterForGranularity, smartDateFormatter, @@ -94,9 +93,8 @@ function PivotTable(element, props) { $(this) .find('td') .each(function eachTd(index) { - //metric = cols[index]; const tdText = $(this)[0].textContent; - var { textContent, attr } = cellFormat( + const { textContent, attr } = cellFormat( index, cols, tdText, From 9b22902460d626f33d4f1ed28a622787ff9637d7 Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 12:35:10 +0530 Subject: [PATCH 06/12] lint --- .../src/utils/formatCells.js | 5 +++-- .../test/PivotTable.test.tsx | 16 ++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js index baf8960fce..7f4585f040 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js @@ -4,7 +4,8 @@ function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dat const metric = cols[i]; const format = columnFormats[metric] || numberFormat || '.3s'; const parsedValue = parseFloat(tdText); - var textContent, attr; + let textContent; + let attr; if (Number.isNaN(parsedValue)) { const regexMatch = dateRegex.exec(tdText); @@ -20,7 +21,7 @@ function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dat textContent = formatNumber(format, parsedValue); attr = ('data-sort', parsedValue); } - return { textContent: textContent, attr: attr }; + return { textContent, attr }; } function cellDateFormat(text, verboseMap, dateRegex, dateFormatter) { diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx index 8b1f287443..e895379bcd 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx @@ -1,14 +1,14 @@ -import { getTimeFormatter, getTimeFormatterForGranularity } from '@superset-ui/core'; +import { getTimeFormatterForGranularity } from '@superset-ui/core'; import { cellFormat } from '../src/utils/formatCells'; describe('pivot table plugin format cells', () => { - let i = 0; - let cols = ['SUM']; + const i = 0; + const cols = ['SUM']; let tdText = '2222222'; - let columnFormats = {}; - let numberFormat = 'SMART_NUMBER'; - let dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; - let dateFormatter = getTimeFormatterForGranularity('P1D'); + const columnFormats = {}; + const numberFormat = 'SMART_NUMBER'; + const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; + const dateFormatter = getTimeFormatterForGranularity('P1D'); it('render number', () => { const { textContent, attr } = cellFormat( @@ -27,7 +27,7 @@ describe('pivot table plugin format cells', () => { it('render date', () => { tdText = '__timestamp:-126230400000.0'; - const { textContent, attr } = cellFormat( + const { textContent } = cellFormat( i, cols, tdText, From 2ff977e34c0f714925561ce7c405a09b31694df9 Mon Sep 17 00:00:00 2001 From: mayur Date: Thu, 24 Dec 2020 15:00:10 +0530 Subject: [PATCH 07/12] handle null values --- .../src/utils/formatCells.js | 22 ++++++++++--------- .../test/PivotTable.test.tsx | 16 ++++++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js index 7f4585f040..84b773e66d 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js @@ -3,24 +3,26 @@ import { formatNumber } from '@superset-ui/core'; function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { const metric = cols[i]; const format = columnFormats[metric] || numberFormat || '.3s'; - const parsedValue = parseFloat(tdText); - let textContent; - let attr; + const tdTextType = parseFloat(tdText) ? 'number' : typeof tdText; + let textContent = tdText; + let attr = ('data-sort', tdText); - if (Number.isNaN(parsedValue)) { + if (tdTextType === 'number') { + const parsedValue = parseFloat(tdText); + textContent = formatNumber(format, parsedValue); + attr = ('data-sort', parsedValue); + } else if (tdTextType === 'string') { const regexMatch = dateRegex.exec(tdText); if (regexMatch) { const date = new Date(parseFloat(regexMatch[1])); textContent = dateFormatter(date); attr = ('data-sort', date); - } else { - textContent = tdText; - attr = ('data-sort', tdText); } - } else { - textContent = formatNumber(format, parsedValue); - attr = ('data-sort', parsedValue); + } else if (tdText === null) { + textContent = ''; + attr = ('data-sort', Number.NEGATIVE_INFINITY); } + return { textContent, attr }; } diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx index e895379bcd..045390e177 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx @@ -54,4 +54,20 @@ describe('pivot table plugin format cells', () => { expect(textContent).toEqual(tdText); expect(attr).toEqual(('data-sort', tdText)); }); + + it('render null', () => { + tdText = null; + + const { textContent, attr } = cellFormat( + i, + cols, + tdText, + columnFormats, + numberFormat, + dateRegex, + dateFormatter, + ); + expect(textContent).toEqual(''); + expect(attr).toEqual(('data-sort', Number.NEGATIVE_INFINITY)); + }); }); From 870c0afe4047d07f5ba02a24596b9c023dff4c58 Mon Sep 17 00:00:00 2001 From: mayur Date: Wed, 30 Dec 2020 18:26:49 +0530 Subject: [PATCH 08/12] nit picks --- .../src/PivotTable.js | 18 +++++++++--- .../src/utils/formatCells.js | 24 ++++++++++++++-- ...PivotTable.test.tsx => PivotTable.test.ts} | 28 +++++++++++++++---- 3 files changed, 58 insertions(+), 12 deletions(-) rename plugins/legacy-plugin-chart-pivot-table/test/{PivotTable.test.tsx => PivotTable.test.ts} (57%) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js index 22eb99070a..4aa7997416 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/PivotTable.js @@ -24,7 +24,7 @@ import { getTimeFormatterForGranularity, smartDateFormatter, } from '@superset-ui/core'; -import { cellFormat, cellDateFormat } from './utils/formatCells'; +import { formatCellValue, formatDateCellValue } from './utils/formatCells'; import fixTableHeight from './utils/fixTableHeight'; import 'datatables.net-bs/css/dataTables.bootstrap.css'; @@ -80,12 +80,22 @@ function PivotTable(element, props) { const dateRegex = /^__timestamp:(-?\d*\.?\d*)$/; $container.find('thead tr th').each(function () { - const cellValue = cellDateFormat($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); + const cellValue = formatDateCellValue( + $(this)[0].textContent, + verboseMap, + dateRegex, + dateFormatter, + ); $(this)[0].textContent = cellValue; }); $container.find('tbody tr th').each(function () { - const cellValue = cellDateFormat($(this)[0].textContent, verboseMap, dateRegex, dateFormatter); + const cellValue = formatDateCellValue( + $(this)[0].textContent, + verboseMap, + dateRegex, + dateFormatter, + ); $(this)[0].textContent = cellValue; }); @@ -94,7 +104,7 @@ function PivotTable(element, props) { .find('td') .each(function eachTd(index) { const tdText = $(this)[0].textContent; - const { textContent, attr } = cellFormat( + const { textContent, attr } = formatCellValue( index, cols, tdText, diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js index 84b773e66d..45ef9c2085 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js @@ -1,6 +1,24 @@ +/** + * 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 { formatNumber } from '@superset-ui/core'; -function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { +function formatCellValue(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { const metric = cols[i]; const format = columnFormats[metric] || numberFormat || '.3s'; const tdTextType = parseFloat(tdText) ? 'number' : typeof tdText; @@ -26,7 +44,7 @@ function cellFormat(i, cols, tdText, columnFormats, numberFormat, dateRegex, dat return { textContent, attr }; } -function cellDateFormat(text, verboseMap, dateRegex, dateFormatter) { +function formatDateCellValue(text, verboseMap, dateRegex, dateFormatter) { const regexMatch = dateRegex.exec(text); let cellValue; if (regexMatch) { @@ -38,4 +56,4 @@ function cellDateFormat(text, verboseMap, dateRegex, dateFormatter) { return cellValue; } -export { cellFormat, cellDateFormat }; +export { formatCellValue, formatDateCellValue }; diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts similarity index 57% rename from plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx rename to plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts index 045390e177..934990c306 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.tsx +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts @@ -1,5 +1,23 @@ +/** + * 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 { getTimeFormatterForGranularity } from '@superset-ui/core'; -import { cellFormat } from '../src/utils/formatCells'; +import { formatCellValue } from '../src/utils/formatCells'; describe('pivot table plugin format cells', () => { const i = 0; @@ -11,7 +29,7 @@ describe('pivot table plugin format cells', () => { const dateFormatter = getTimeFormatterForGranularity('P1D'); it('render number', () => { - const { textContent, attr } = cellFormat( + const { textContent, attr } = formatCellValue( i, cols, tdText, @@ -27,7 +45,7 @@ describe('pivot table plugin format cells', () => { it('render date', () => { tdText = '__timestamp:-126230400000.0'; - const { textContent } = cellFormat( + const { textContent } = formatCellValue( i, cols, tdText, @@ -42,7 +60,7 @@ describe('pivot table plugin format cells', () => { it('render string', () => { tdText = 'some-text'; - const { textContent, attr } = cellFormat( + const { textContent, attr } = formatCellValue( i, cols, tdText, @@ -58,7 +76,7 @@ describe('pivot table plugin format cells', () => { it('render null', () => { tdText = null; - const { textContent, attr } = cellFormat( + const { textContent, attr } = formatCellValue( i, cols, tdText, From 725a56a594b22e8e4e13405586296907b43aba57 Mon Sep 17 00:00:00 2001 From: mayur Date: Wed, 30 Dec 2020 18:35:51 +0530 Subject: [PATCH 09/12] js to ts --- .../src/utils/{formatCells.js => formatCells.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugins/legacy-plugin-chart-pivot-table/src/utils/{formatCells.js => formatCells.ts} (100%) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts similarity index 100% rename from plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.js rename to plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts From 015ba7f495ed38e2d7785af16202cddd9d671086 Mon Sep 17 00:00:00 2001 From: mayur Date: Wed, 30 Dec 2020 20:49:12 +0530 Subject: [PATCH 10/12] nit picks --- .../src/utils/formatCells.ts | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts index 45ef9c2085..712146679f 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts @@ -18,33 +18,44 @@ */ import { formatNumber } from '@superset-ui/core'; -function formatCellValue(i, cols, tdText, columnFormats, numberFormat, dateRegex, dateFormatter) { - const metric = cols[i]; - const format = columnFormats[metric] || numberFormat || '.3s'; +function formatCellValue( + i: number, + cols: string[], + tdText: string, + columnFormats: any, + numberFormat: string, + dateRegex: RegExp, + dateFormatter: any, +) { + const metric: string = cols[i]; + const format: string = columnFormats[metric] || numberFormat || '.3s'; const tdTextType = parseFloat(tdText) ? 'number' : typeof tdText; - let textContent = tdText; - let attr = ('data-sort', tdText); + let textContent: string = tdText; + let sortAttributeValue: any = tdText; if (tdTextType === 'number') { const parsedValue = parseFloat(tdText); textContent = formatNumber(format, parsedValue); - attr = ('data-sort', parsedValue); + sortAttributeValue = parsedValue; } else if (tdTextType === 'string') { const regexMatch = dateRegex.exec(tdText); if (regexMatch) { const date = new Date(parseFloat(regexMatch[1])); textContent = dateFormatter(date); - attr = ('data-sort', date); + sortAttributeValue = date; } } else if (tdText === null) { textContent = ''; - attr = ('data-sort', Number.NEGATIVE_INFINITY); + sortAttributeValue = Number.NEGATIVE_INFINITY; } + // @ts-ignore + const attr = ('data-sort', sortAttributeValue); + return { textContent, attr }; } -function formatDateCellValue(text, verboseMap, dateRegex, dateFormatter) { +function formatDateCellValue(text: string, verboseMap: any, dateRegex: RegExp, dateFormatter: any) { const regexMatch = dateRegex.exec(text); let cellValue; if (regexMatch) { From 95cd2610ce8fe83346848a705ebb89c63e13ee96 Mon Sep 17 00:00:00 2001 From: mayur Date: Fri, 8 Jan 2021 17:42:44 +0530 Subject: [PATCH 11/12] null values fixed --- .../src/utils/formatCells.ts | 12 +++++------- .../test/PivotTable.test.ts | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts index 712146679f..9f91881efd 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts @@ -29,26 +29,24 @@ function formatCellValue( ) { const metric: string = cols[i]; const format: string = columnFormats[metric] || numberFormat || '.3s'; - const tdTextType = parseFloat(tdText) ? 'number' : typeof tdText; let textContent: string = tdText; let sortAttributeValue: any = tdText; - if (tdTextType === 'number') { + if (parseFloat(tdText)) { const parsedValue = parseFloat(tdText); textContent = formatNumber(format, parsedValue); sortAttributeValue = parsedValue; - } else if (tdTextType === 'string') { + } else { const regexMatch = dateRegex.exec(tdText); if (regexMatch) { const date = new Date(parseFloat(regexMatch[1])); textContent = dateFormatter(date); sortAttributeValue = date; + } else if (tdText == 'null') { + textContent = ''; + sortAttributeValue = Number.NEGATIVE_INFINITY; } - } else if (tdText === null) { - textContent = ''; - sortAttributeValue = Number.NEGATIVE_INFINITY; } - // @ts-ignore const attr = ('data-sort', sortAttributeValue); diff --git a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts index 934990c306..04966a7920 100644 --- a/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts +++ b/plugins/legacy-plugin-chart-pivot-table/test/PivotTable.test.ts @@ -74,7 +74,7 @@ describe('pivot table plugin format cells', () => { }); it('render null', () => { - tdText = null; + tdText = 'null'; const { textContent, attr } = formatCellValue( i, From 786e2df7c5c06f901431231b90e2b845d7c0991e Mon Sep 17 00:00:00 2001 From: mayur Date: Fri, 8 Jan 2021 17:51:52 +0530 Subject: [PATCH 12/12] eq fixed --- .../legacy-plugin-chart-pivot-table/src/utils/formatCells.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts index 9f91881efd..1eac30de0a 100644 --- a/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts +++ b/plugins/legacy-plugin-chart-pivot-table/src/utils/formatCells.ts @@ -42,7 +42,7 @@ function formatCellValue( const date = new Date(parseFloat(regexMatch[1])); textContent = dateFormatter(date); sortAttributeValue = date; - } else if (tdText == 'null') { + } else if (tdText === 'null') { textContent = ''; sortAttributeValue = Number.NEGATIVE_INFINITY; }