Skip to content

Commit

Permalink
Make resolve_locale() validate the resolved locale value
Browse files Browse the repository at this point in the history
  • Loading branch information
rich-iannone committed Jan 28, 2022
1 parent ea5e93f commit a7bc68b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
36 changes: 6 additions & 30 deletions R/format_data.R
Expand Up @@ -170,13 +170,9 @@ fmt_number <- function(data,
# Perform input object validation
stop_if_not_gt(data = data)

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
Expand Down Expand Up @@ -452,13 +448,9 @@ fmt_scientific <- function(data,
suffixing <- FALSE
use_seps <- TRUE

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
Expand Down Expand Up @@ -633,13 +625,9 @@ fmt_engineering <- function(data,
suffixing <- FALSE
use_seps <- TRUE

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
Expand Down Expand Up @@ -961,13 +949,9 @@ fmt_percent <- function(data,
# Perform input object validation
stop_if_not_gt(data = data)

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Stop function if any columns have data that is incompatible
# with this formatter
if (
Expand Down Expand Up @@ -1143,13 +1127,9 @@ fmt_currency <- function(data,
# Perform input object validation
stop_if_not_gt(data = data)

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Stop function if any columns have data that is incompatible
# with this formatter
if (
Expand Down Expand Up @@ -1291,13 +1271,9 @@ fmt_bytes <- function(data,

standard <- match.arg(standard)

# Resolve the `locale` by choosing the default (possibly set in `gt()`) or
# overriding with the `locale` set here
# Resolve the `locale` value here with the global locale value
locale <- resolve_locale(data = data, locale = locale)

# Stop function if `locale` does not have a valid value
validate_locale(locale = locale)

# Use locale-based marks if a locale ID is provided
sep_mark <- get_locale_sep_mark(locale, sep_mark, use_seps)
dec_mark <- get_locale_dec_mark(locale, dec_mark)
Expand Down
14 changes: 14 additions & 0 deletions R/utils_formatters.R
Expand Up @@ -125,12 +125,26 @@ get_locale_dec_mark <- function(locale = NULL,
filter_table_to_value(locales, dec_sep, base_locale_id == locale)
}

#' Resolve the locale in functions with a `locale` argument
#'
#' This performs locale resolution since the default locale (possibly set in
#' `gt()`) can be overridden with a `locale` set in a downstream function.
#' This performs a final validation of the resolved locale to ensure it has a
#' supported value.
#'
#' @param data The gt object.
#' @param locale The user-supplied `locale` value, found in several `fmt_*()`
#' functions. This is expected as `NULL` if not supplied by the user.
#'
#' @noRd
resolve_locale <- function(data, locale) {

if (is.null(locale)) {
locale <- dt_locale_get_value(data = data)
}

validate_locale(locale = locale)

locale
}

Expand Down

0 comments on commit a7bc68b

Please sign in to comment.