Skip to content

Commit

Permalink
Minor rework of Currency display options (#3298)
Browse files Browse the repository at this point in the history
* Update currency.lua

* Adjust existing usage

* Add testcase for new option

* code style :D
  • Loading branch information
iMarbot authored Sep 21, 2023
1 parent 1029615 commit 8e8cf4c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 19 deletions.
4 changes: 2 additions & 2 deletions components/prize_pool/commons/prize_pool_base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ BasePrizePool.prizeTypes = {
if data > 0 then
return TableCell{content = {
Currency.display(BASE_CURRENCY, data,
{formatValue = true, formatPrecision = headerData.roundPrecision, abbreviation = false})
{formatValue = true, formatPrecision = headerData.roundPrecision, displayCurrencyCode = false})
}}
end
end,
Expand Down Expand Up @@ -188,7 +188,7 @@ BasePrizePool.prizeTypes = {
if data > 0 then
return TableCell{content = {
Currency.display(headerData.currency, data,
{formatValue = true, formatPrecision = headerData.roundPrecision, abbreviation = false})
{formatValue = true, formatPrecision = headerData.roundPrecision, displayCurrencyCode = false})
}}
end
end,
Expand Down
2 changes: 1 addition & 1 deletion components/results_table/commons/results_table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function ResultsTable:buildRow(placement)
local useIndivPrize = self.config.useIndivPrize and self.config.queryType ~= Opponent.team
row:tag('td'):wikitext(Currency.display('USD',
useIndivPrize and placement.individualprizemoney or placement.prizemoney,
{dashIfZero = true, abbreviation = false, formatValue = true}
{dashIfZero = true, displayCurrencyCode = false, formatValue = true}
))

return row
Expand Down
2 changes: 1 addition & 1 deletion components/results_table/commons/results_table_award.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function AwardsTable:buildRow(placement)

row:tag('td'):wikitext(Currency.display('USD',
self.config.queryType ~= Opponent.team and placement.individualprizemoney or placement.prizemoney,
{dashIfZero = true, abbreviation = false, formatValue = true}
{dashIfZero = true, displayCurrencyCode = false, formatValue = true}
))

return row
Expand Down
2 changes: 1 addition & 1 deletion components/statistics/portal_statistics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ local ColumnName = Condition.ColumnName

local Count = Lua.import('Module:Count', {requireDevIfEnabled = true})

local CURRENCY_FORMAT_OPTIONS = {dashIfZero = true, abbreviation = false, formatValue = true}
local CURRENCY_FORMAT_OPTIONS = {dashIfZero = true, displayCurrencyCode = false, formatValue = true}
local CURRENT_YEAR = tonumber(os.date('%Y')) --[[@as integer]]
local DATE = os.date('%F') --[[@as string]]
local EPOCH_DATE = '1970-01-01'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function BaseTournamentsListing:_row(tournamentData)
if prizeValue > 0 then
priceCell
:wikitext(Currency.display('USD', prizeValue, {
dashIfZero = true, abbreviation = false, formatValue = true
dashIfZero = true, displayCurrencyCode = false, formatValue = true
}))
else
priceCell
Expand Down
31 changes: 18 additions & 13 deletions standard/currency/currency.lua
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,24 @@ function Currency.template(frame)
end

---@class currencyDisplayOptions
---@field abbreviation boolean?
---@field dashIfZero boolean?
---@field displaySymbol boolean?
---@field displayCurrencyCode boolean?
---@field forceRoundPrecision boolean?
---@field formatPrecision integer?
---@field formatValue boolean?
---@field setVariables boolean?
---@field symbol boolean?
---@field useHtmlStyling boolean?

---@param currencyCode string?
---@param prizeValue string|number|nil
---@param options currencyDisplayOptions?
---@return string?
function Currency.display(currencyCode, prizeValue, options)
options = options or {}
options.symbol = Logic.emptyOr(options.symbol, true)
options.abbreviation = Logic.emptyOr(options.abbreviation, true)
options.displaySymbol = Logic.emptyOr(options.displaySymbol, true)
options.displayCurrencyCode = Logic.emptyOr(options.displayCurrencyCode, true)
options.useHtmlStyling = Logic.emptyOr(options.useHtmlStyling, true)

if options.dashIfZero and tonumber(prizeValue) == 0 then
return DASH
Expand All @@ -77,28 +79,29 @@ function Currency.display(currencyCode, prizeValue, options)
return nil
end

local spaceString = options.useHtmlStyling and NON_BREAKING_SPACE or ' '

local currencyPrefix = ''
if currencyData.symbol.text and not currencyData.symbol.isAfter then
currencyPrefix = currencyData.symbol.text .. (currencyData.symbol.hasSpace and NON_BREAKING_SPACE or '')
currencyPrefix = currencyData.symbol.text .. (currencyData.symbol.hasSpace and spaceString or '')
end
local currencySuffix = ''
if currencyData.symbol.text and currencyData.symbol.isAfter then
currencySuffix = (currencyData.symbol.hasSpace and NON_BREAKING_SPACE or '') .. currencyData.symbol.text
currencySuffix = (currencyData.symbol.hasSpace and spaceString or '') .. currencyData.symbol.text
end
local currencyAbbreviation = Abbreviation.make(currencyData.code, currencyData.name)

if options.setVariables then
Variables.varDefine('localcurrencycode', currencyData.code or '')
Variables.varDefine('localcurrencysymbol', currencyPrefix)
Variables.varDefine('localcurrencysymbolafter', currencySuffix)
end

if options.abbreviation and currencyData.symbol.text == currencyData.code then
options.symbol = false
if options.displayCurrencyCode and currencyData.symbol.text == currencyData.code then
options.displaySymbol = false
end

local prizeDisplay = ''
if options.symbol then
if options.displaySymbol then
prizeDisplay = prizeDisplay .. currencyPrefix
end
if prizeValue then
Expand All @@ -107,11 +110,13 @@ function Currency.display(currencyCode, prizeValue, options)
end
prizeDisplay = prizeDisplay .. prizeValue
end
if options.symbol then
if options.displaySymbol then
prizeDisplay = prizeDisplay .. currencySuffix
end
if options.abbreviation then
prizeDisplay = prizeDisplay .. (prizeDisplay ~= '' and NON_BREAKING_SPACE or '') .. currencyAbbreviation
if options.displayCurrencyCode then
local currencyCodeDisplay = not options.useHtmlStyling and currencyData.code
or Abbreviation.make(currencyData.code, currencyData.name)
prizeDisplay = prizeDisplay .. (String.isNotEmpty(prizeDisplay) and spaceString or '') .. currencyCodeDisplay
end

return prizeDisplay
Expand Down
1 change: 1 addition & 0 deletions standard/test/currency_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function suite:testDisplay()
self:assertEquals('€1,200&nbsp;<abbr title="Euro">EUR</abbr>', Currency.display('EUR', 1200, {formatValue = true}))
self:assertEquals('€1200&nbsp;<abbr title="Euro">EUR</abbr>', Currency.display('EUR', 1200))
self:assertEquals('€&nbsp;<abbr title="Euro">EUR</abbr>', Currency.display('EUR'))
self:assertEquals('€ EUR', Currency.display('EUR', nil, {useHtmlStyling = false}))
end

return suite

0 comments on commit 8e8cf4c

Please sign in to comment.