forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement currencies formatter for saved metrics (apache#24517)
- Loading branch information
Showing
61 changed files
with
906 additions
and
75 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
superset-frontend/packages/superset-ui-core/src/currency-format/CurrencyFormatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* 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 { ExtensibleFunction } from '../models'; | ||
import { getNumberFormatter, NumberFormats } from '../number-format'; | ||
import { Currency } from '../query'; | ||
|
||
interface CurrencyFormatterConfig { | ||
d3Format?: string; | ||
currency: Currency; | ||
locale?: string; | ||
} | ||
|
||
interface CurrencyFormatter { | ||
(value: number | null | undefined): string; | ||
} | ||
|
||
export const getCurrencySymbol = (currency: Currency) => | ||
new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency: currency.symbol, | ||
}) | ||
.formatToParts(1) | ||
.find(x => x.type === 'currency')?.value; | ||
|
||
class CurrencyFormatter extends ExtensibleFunction { | ||
d3Format: string; | ||
|
||
locale: string; | ||
|
||
currency: Currency; | ||
|
||
constructor(config: CurrencyFormatterConfig) { | ||
super((value: number) => this.format(value)); | ||
this.d3Format = config.d3Format || NumberFormats.SMART_NUMBER; | ||
this.currency = config.currency; | ||
this.locale = config.locale || 'en-US'; | ||
} | ||
|
||
hasValidCurrency() { | ||
return Boolean(this.currency?.symbol); | ||
} | ||
|
||
getNormalizedD3Format() { | ||
return this.d3Format.replace(/\$|%/g, ''); | ||
} | ||
|
||
format(value: number) { | ||
const formattedValue = getNumberFormatter(this.getNormalizedD3Format())( | ||
value, | ||
); | ||
if (!this.hasValidCurrency()) { | ||
return formattedValue as string; | ||
} | ||
|
||
if (this.currency.symbolPosition === 'prefix') { | ||
return `${getCurrencySymbol(this.currency)} ${formattedValue}`; | ||
} | ||
return `${formattedValue} ${getCurrencySymbol(this.currency)}`; | ||
} | ||
} | ||
|
||
export default CurrencyFormatter; |
21 changes: 21 additions & 0 deletions
21
superset-frontend/packages/superset-ui-core/src/currency-format/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { default as CurrencyFormatter } from './CurrencyFormatter'; | ||
export * from './CurrencyFormatter'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
superset-frontend/packages/superset-ui-core/test/currency-format/CurrencyFormatter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* 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, | ||
getCurrencySymbol, | ||
NumberFormats, | ||
} from '@superset-ui/core'; | ||
|
||
it('getCurrencySymbol', () => { | ||
expect( | ||
getCurrencySymbol({ symbol: 'PLN', symbolPosition: 'prefix' }), | ||
).toEqual('PLN'); | ||
expect( | ||
getCurrencySymbol({ symbol: 'USD', symbolPosition: 'prefix' }), | ||
).toEqual('$'); | ||
|
||
expect(() => | ||
getCurrencySymbol({ symbol: 'INVALID_CODE', symbolPosition: 'prefix' }), | ||
).toThrow(RangeError); | ||
}); | ||
|
||
it('CurrencyFormatter object fields', () => { | ||
const defaultCurrencyFormatter = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
}); | ||
expect(defaultCurrencyFormatter.d3Format).toEqual(NumberFormats.SMART_NUMBER); | ||
expect(defaultCurrencyFormatter.locale).toEqual('en-US'); | ||
expect(defaultCurrencyFormatter.currency).toEqual({ | ||
symbol: 'USD', | ||
symbolPosition: 'prefix', | ||
}); | ||
|
||
const currencyFormatter = new CurrencyFormatter({ | ||
currency: { symbol: 'PLN', symbolPosition: 'suffix' }, | ||
locale: 'pl-PL', | ||
d3Format: ',.1f', | ||
}); | ||
expect(currencyFormatter.d3Format).toEqual(',.1f'); | ||
expect(currencyFormatter.locale).toEqual('pl-PL'); | ||
expect(currencyFormatter.currency).toEqual({ | ||
symbol: 'PLN', | ||
symbolPosition: 'suffix', | ||
}); | ||
}); | ||
|
||
it('CurrencyFormatter:hasValidCurrency', () => { | ||
const currencyFormatter = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
}); | ||
expect(currencyFormatter.hasValidCurrency()).toBe(true); | ||
|
||
const currencyFormatterWithoutPosition = new CurrencyFormatter({ | ||
// @ts-ignore | ||
currency: { symbol: 'USD' }, | ||
}); | ||
expect(currencyFormatterWithoutPosition.hasValidCurrency()).toBe(true); | ||
|
||
const currencyFormatterWithoutSymbol = new CurrencyFormatter({ | ||
// @ts-ignore | ||
currency: { symbolPosition: 'prefix' }, | ||
}); | ||
expect(currencyFormatterWithoutSymbol.hasValidCurrency()).toBe(false); | ||
|
||
// @ts-ignore | ||
const currencyFormatterWithoutCurrency = new CurrencyFormatter({}); | ||
expect(currencyFormatterWithoutCurrency.hasValidCurrency()).toBe(false); | ||
}); | ||
|
||
it('CurrencyFormatter:getNormalizedD3Format', () => { | ||
const currencyFormatter = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
}); | ||
expect(currencyFormatter.getNormalizedD3Format()).toEqual( | ||
currencyFormatter.d3Format, | ||
); | ||
|
||
const currencyFormatter2 = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
d3Format: ',.1f', | ||
}); | ||
expect(currencyFormatter2.getNormalizedD3Format()).toEqual( | ||
currencyFormatter2.d3Format, | ||
); | ||
|
||
const currencyFormatter3 = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
d3Format: '$,.1f', | ||
}); | ||
expect(currencyFormatter3.getNormalizedD3Format()).toEqual(',.1f'); | ||
|
||
const currencyFormatter4 = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
d3Format: ',.1%', | ||
}); | ||
expect(currencyFormatter4.getNormalizedD3Format()).toEqual(',.1'); | ||
}); | ||
|
||
it('CurrencyFormatter:format', () => { | ||
const VALUE = 56100057; | ||
const currencyFormatterWithPrefix = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
}); | ||
|
||
expect(currencyFormatterWithPrefix(VALUE)).toEqual( | ||
currencyFormatterWithPrefix.format(VALUE), | ||
); | ||
expect(currencyFormatterWithPrefix(VALUE)).toEqual('$ 56.1M'); | ||
|
||
const currencyFormatterWithSuffix = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'suffix' }, | ||
}); | ||
expect(currencyFormatterWithSuffix(VALUE)).toEqual('56.1M $'); | ||
|
||
const currencyFormatterWithoutPosition = new CurrencyFormatter({ | ||
// @ts-ignore | ||
currency: { symbol: 'USD' }, | ||
}); | ||
expect(currencyFormatterWithoutPosition(VALUE)).toEqual('56.1M $'); | ||
|
||
// @ts-ignore | ||
const currencyFormatterWithoutCurrency = new CurrencyFormatter({}); | ||
expect(currencyFormatterWithoutCurrency(VALUE)).toEqual('56.1M'); | ||
|
||
const currencyFormatterWithCustomD3 = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
d3Format: ',.1f', | ||
}); | ||
expect(currencyFormatterWithCustomD3(VALUE)).toEqual('$ 56,100,057.0'); | ||
|
||
const currencyFormatterWithPercentD3 = new CurrencyFormatter({ | ||
currency: { symbol: 'USD', symbolPosition: 'prefix' }, | ||
d3Format: ',.1f%', | ||
}); | ||
expect(currencyFormatterWithPercentD3(VALUE)).toEqual('$ 56,100,057.0'); | ||
|
||
const currencyFormatterWithCurrencyD3 = new CurrencyFormatter({ | ||
currency: { symbol: 'PLN', symbolPosition: 'suffix' }, | ||
d3Format: '$,.1f', | ||
}); | ||
expect(currencyFormatterWithCurrencyD3(VALUE)).toEqual('56,100,057.0 PLN'); | ||
}); |
Oops, something went wrong.