From feb4feeb49ccc43225bb57f2334f1b3d6f484e9c Mon Sep 17 00:00:00 2001 From: Vitor Avila Date: Mon, 2 Dec 2024 18:39:18 -0300 Subject: [PATCH 1/2] feat(Handlebars): formatNumber and group helpers --- superset-frontend/package-lock.json | 15 +++++++++++++++ .../plugins/plugin-chart-handlebars/package.json | 1 + .../components/Handlebars/HandlebarsViewer.tsx | 12 ++++++++++++ .../plugin-chart-handlebars/types/external.d.ts | 1 + 4 files changed, 29 insertions(+) diff --git a/superset-frontend/package-lock.json b/superset-frontend/package-lock.json index 93d801a70e728..219a70e5304de 100644 --- a/superset-frontend/package-lock.json +++ b/superset-frontend/package-lock.json @@ -28029,6 +28029,14 @@ "uglify-js": "^3.1.4" } }, + "node_modules/handlebars-group-by": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/handlebars-group-by/-/handlebars-group-by-1.0.1.tgz", + "integrity": "sha512-qwVVDVAJMBKdmnQU8jcEXGOu+4/2YJX1RP3pUw6Ee9t6gdkxt+dJEWDudSFTgqb35KXrktw/Nn/Dp3Rx5muHpg==", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/handlebars/node_modules/source-map": { "version": "0.6.1", "license": "BSD-3-Clause", @@ -58568,6 +58576,7 @@ "license": "Apache-2.0", "dependencies": { "handlebars": "^4.7.8", + "handlebars-group-by": "^1.0.1", "just-handlebars-helpers": "^1.0.19" }, "devDependencies": { @@ -69177,6 +69186,7 @@ "@types/jest": "^29.5.12", "@types/lodash": "^4.17.7", "handlebars": "^4.7.8", + "handlebars-group-by": "*", "jest": "^29.7.0", "just-handlebars-helpers": "^1.0.19" }, @@ -79829,6 +79839,11 @@ } } }, + "handlebars-group-by": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/handlebars-group-by/-/handlebars-group-by-1.0.1.tgz", + "integrity": "sha512-qwVVDVAJMBKdmnQU8jcEXGOu+4/2YJX1RP3pUw6Ee9t6gdkxt+dJEWDudSFTgqb35KXrktw/Nn/Dp3Rx5muHpg==" + }, "har-schema": { "version": "2.0.0", "dev": true diff --git a/superset-frontend/plugins/plugin-chart-handlebars/package.json b/superset-frontend/plugins/plugin-chart-handlebars/package.json index 3db9ff58e4f68..c432215852239 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/package.json +++ b/superset-frontend/plugins/plugin-chart-handlebars/package.json @@ -28,6 +28,7 @@ }, "dependencies": { "handlebars": "^4.7.8", + "handlebars-group-by": "^1.0.1", "just-handlebars-helpers": "^1.0.19" }, "peerDependencies": { diff --git a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx index 45a191757fd01..3061b3afa072e 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx +++ b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx @@ -22,6 +22,7 @@ import moment from 'moment'; import { useMemo, useState } from 'react'; import { isPlainObject } from 'lodash'; import Helpers from 'just-handlebars-helpers'; +import HandlebarsGroupBy from 'handlebars-group-by'; export interface HandlebarsViewerProps { templateSource: string; @@ -88,4 +89,15 @@ Handlebars.registerHelper('stringify', (obj: any, obj2: any) => { return isPlainObject(obj) ? JSON.stringify(obj) : String(obj); }); +Handlebars.registerHelper( + 'formatNumber', + function (number: any, locale: string = 'en-US') { + if (typeof number !== 'number') { + return number; + } + return number.toLocaleString(locale); + }, +); + Helpers.registerHelpers(Handlebars); +HandlebarsGroupBy.register(Handlebars); diff --git a/superset-frontend/plugins/plugin-chart-handlebars/types/external.d.ts b/superset-frontend/plugins/plugin-chart-handlebars/types/external.d.ts index ae61945f05350..cd32b201fc5be 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/types/external.d.ts +++ b/superset-frontend/plugins/plugin-chart-handlebars/types/external.d.ts @@ -22,3 +22,4 @@ declare module '*.png' { } declare module '*.jpg'; declare module 'just-handlebars-helpers'; +declare module 'handlebars-group-by'; From dacd2fad7abafc8e7a3ffa3600af499e0387e4d2 Mon Sep 17 00:00:00 2001 From: Vitor Avila Date: Mon, 2 Dec 2024 19:05:00 -0300 Subject: [PATCH 2/2] Linting fixes --- .../src/components/Handlebars/HandlebarsViewer.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx index 3061b3afa072e..737416e196751 100644 --- a/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx +++ b/superset-frontend/plugins/plugin-chart-handlebars/src/components/Handlebars/HandlebarsViewer.tsx @@ -91,7 +91,7 @@ Handlebars.registerHelper('stringify', (obj: any, obj2: any) => { Handlebars.registerHelper( 'formatNumber', - function (number: any, locale: string = 'en-US') { + function (number: any, locale = 'en-US') { if (typeof number !== 'number') { return number; }