From 2eac975aaeac823859d924a6da399f84d36c53f4 Mon Sep 17 00:00:00 2001 From: Krist Wongsuphasawat Date: Fri, 30 Nov 2018 22:57:40 -0800 Subject: [PATCH] allow leading and trailing space when looking up formatter (#46) feat: allow leading and trailing space when looking up formatter --- .../src/NumberFormatterRegistry.js | 2 +- .../test/NumberFormatterRegistry.test.js | 11 +++++++++++ .../src/TimeFormatterRegistry.js | 2 +- .../test/TimeFormatterRegistry.test.js | 5 +++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/src/NumberFormatterRegistry.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/src/NumberFormatterRegistry.js index 13a477911dcaf..b45a30e2d3e3e 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/src/NumberFormatterRegistry.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/src/NumberFormatterRegistry.js @@ -11,7 +11,7 @@ export default class NumberFormatterRegistry extends RegistryWithDefaultKey { } get(formatterId) { - const targetFormat = formatterId || this.defaultKey; + const targetFormat = (formatterId || this.defaultKey).trim(); if (this.has(targetFormat)) { return super.get(targetFormat); diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/test/NumberFormatterRegistry.test.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/test/NumberFormatterRegistry.test.js index 0c006fe1f99d8..77b10b5f72ee0 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/test/NumberFormatterRegistry.test.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-number-format/test/NumberFormatterRegistry.test.js @@ -22,6 +22,17 @@ describe('NumberFormatterRegistry', () => { const formatter = registry.get(); expect(formatter.format(100)).toEqual('100.0'); }); + it('removes leading and trailing spaces from format', () => { + const formatter = registry.get(' .2f'); + expect(formatter).toBeInstanceOf(NumberFormatter); + expect(formatter.format(100)).toEqual('100.00'); + const formatter2 = registry.get('.2f '); + expect(formatter2).toBeInstanceOf(NumberFormatter); + expect(formatter2.format(100)).toEqual('100.00'); + const formatter3 = registry.get(' .2f '); + expect(formatter3).toBeInstanceOf(NumberFormatter); + expect(formatter3.format(100)).toEqual('100.00'); + }); }); describe('.format(format, value)', () => { it('return the value with the specified format', () => { diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/src/TimeFormatterRegistry.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/src/TimeFormatterRegistry.js index e9bcafcb605f9..1249b6d22d501 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/src/TimeFormatterRegistry.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/src/TimeFormatterRegistry.js @@ -11,7 +11,7 @@ export default class TimeFormatterRegistry extends RegistryWithDefaultKey { } get(format) { - const targetFormat = format || this.defaultKey; + const targetFormat = (format || this.defaultKey).trim(); if (this.has(targetFormat)) { return super.get(targetFormat); diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/test/TimeFormatterRegistry.test.js b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/test/TimeFormatterRegistry.test.js index 39d7b06dc8666..50b84f1a123f4 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/test/TimeFormatterRegistry.test.js +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-time-format/test/TimeFormatterRegistry.test.js @@ -22,6 +22,11 @@ describe('TimeFormatterRegistry', () => { const formatter = registry.get(); expect(formatter.format(PREVIEW_TIME)).toEqual('14/02/2017'); }); + it('removes leading and trailing spaces from format', () => { + const formatter = registry.get(' %Y '); + expect(formatter).toBeInstanceOf(TimeFormatter); + expect(formatter.format(PREVIEW_TIME)).toEqual('2017'); + }); }); describe('.format(format, value)', () => { it('return the value with the specified format', () => {