Skip to content

Commit 2ad4f71

Browse files
committed
fix: fix and improvemets
1 parent ff0ae32 commit 2ad4f71

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

R/goodbye.R

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#'
66
#' @param name Character string. The name to bid farewell to. Default is "world".
77
#' @param language Character string. The language for the farewell.
8-
#' Supported languages: "english" (default), "spanish", "french", "portuguese".
8+
#' Supported languages: "english" (default), "spanish", "french", "portuguese", "german", "italian".
99
#' @param exclamation Logical. Whether to add an exclamation mark. Default is TRUE.
10+
#' @param capitalize Logical. Whether to capitalize the name. Default is FALSE.
1011
#'
1112
#' @return A character string containing the farewell message.
1213
#' @export
@@ -16,9 +17,10 @@
1617
#' goodbye("R Users")
1718
#' goodbye("amigos", language = "spanish")
1819
#' goodbye("mes amis", language = "french", exclamation = FALSE)
20+
#' goodbye("world", capitalize = TRUE)
1921
#'
2022
#' @seealso \code{\link{hello}} for a greeting function
21-
goodbye <- function(name = "world", language = "english", exclamation = TRUE) {
23+
goodbye <- function(name = "world", language = "english", exclamation = TRUE, capitalize = FALSE) {
2224
# Input validation
2325
if (!is.character(name) || length(name) != 1) {
2426
stop("'name' must be a single character string")
@@ -28,18 +30,43 @@ goodbye <- function(name = "world", language = "english", exclamation = TRUE) {
2830
stop("'language' must be a single character string")
2931
}
3032

33+
# Validate language parameter
34+
supported_languages <- c("english", "spanish", "french", "portuguese", "german", "italian")
35+
language <- tolower(language)
36+
if (!language %in% supported_languages) {
37+
warning(
38+
sprintf(
39+
"Language '%s' not supported. Using English instead. Supported languages: %s",
40+
language,
41+
paste(supported_languages, collapse = ", ")
42+
)
43+
)
44+
language <- "english"
45+
}
46+
3147
if (!is.logical(exclamation) || length(exclamation) != 1) {
3248
stop("'exclamation' must be a single logical value")
3349
}
3450

51+
if (!is.logical(capitalize) || length(capitalize) != 1) {
52+
stop("'capitalize' must be a single logical value")
53+
}
54+
55+
# Capitalize name if requested
56+
if (capitalize) {
57+
name <- paste0(toupper(substr(name, 1, 1)), substr(name, 2, nchar(name)))
58+
}
59+
3560
# Select farewell based on language
3661
farewell <- switch(
37-
tolower(language),
62+
language,
3863
english = "Goodbye",
3964
spanish = "Adi\u00f3s", # Using Unicode escape for 'ó'
4065
french = "Au revoir",
4166
portuguese = "Adeus",
42-
# Default to English if language not supported
67+
german = "Auf Wiedersehen",
68+
italian = "Arrivederci",
69+
# Default to English if language not supported (shouldn't happen due to validation)
4370
"Goodbye"
4471
)
4572

R/hello.R

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#'
66
#' @param name Character string. The name to greet. Default is "world".
77
#' @param language Character string. The language for the greeting.
8-
#' Supported languages: "english" (default), "spanish", "french", "portuguese".
8+
#' Supported languages: "english" (default), "spanish", "french", "portuguese", "german", "italian".
99
#' @param exclamation Logical. Whether to add an exclamation mark. Default is TRUE.
10+
#' @param capitalize Logical. Whether to capitalize the name. Default is FALSE.
1011
#'
1112
#' @return A character string containing the greeting.
1213
#' @export
@@ -16,9 +17,10 @@
1617
#' hello("R Users")
1718
#' hello("amigos", language = "spanish")
1819
#' hello("mes amis", language = "french", exclamation = FALSE)
20+
#' hello("world", capitalize = TRUE)
1921
#'
2022
#' @seealso \code{\link{goodbye}} for a farewell function
21-
hello <- function(name = "world", language = "english", exclamation = TRUE) {
23+
hello <- function(name = "world", language = "english", exclamation = TRUE, capitalize = FALSE) {
2224
# Input validation
2325
if (!is.character(name) || length(name) != 1) {
2426
stop("'name' must be a single character string")
@@ -28,18 +30,43 @@ hello <- function(name = "world", language = "english", exclamation = TRUE) {
2830
stop("'language' must be a single character string")
2931
}
3032

33+
# Validate language parameter
34+
supported_languages <- c("english", "spanish", "french", "portuguese", "german", "italian")
35+
language <- tolower(language)
36+
if (!language %in% supported_languages) {
37+
warning(
38+
sprintf(
39+
"Language '%s' not supported. Using English instead. Supported languages: %s",
40+
language,
41+
paste(supported_languages, collapse = ", ")
42+
)
43+
)
44+
language <- "english"
45+
}
46+
3147
if (!is.logical(exclamation) || length(exclamation) != 1) {
3248
stop("'exclamation' must be a single logical value")
3349
}
3450

51+
if (!is.logical(capitalize) || length(capitalize) != 1) {
52+
stop("'capitalize' must be a single logical value")
53+
}
54+
55+
# Capitalize name if requested
56+
if (capitalize) {
57+
name <- paste0(toupper(substr(name, 1, 1)), substr(name, 2, nchar(name)))
58+
}
59+
3560
# Select greeting based on language
3661
greeting <- switch(
37-
tolower(language),
62+
language,
3863
english = "Hello",
3964
spanish = "Hola",
4065
french = "Bonjour",
4166
portuguese = "Ol\u00e1", # Using Unicode escape for 'á'
42-
# Default to English if language not supported
67+
german = "Hallo",
68+
italian = "Ciao",
69+
# Default to English if language not supported (shouldn't happen due to validation)
4370
"Hello"
4471
)
4572

R/utils.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Internal utility functions
2+
3+
#' Get supported languages
4+
#'
5+
#' Returns a vector of languages supported by greeting functions
6+
#'
7+
#' @return Character vector of supported language names
8+
#' @keywords internal
9+
get_supported_languages <- function() {
10+
c("english", "spanish", "french", "portuguese", "german", "italian")
11+
}
12+
13+
#' Capitalize first letter
14+
#'
15+
#' Capitalizes the first letter of a string
16+
#'
17+
#' @param x Character string to capitalize
18+
#' @return Character string with first letter capitalized
19+
#' @keywords internal
20+
capitalize_first <- function(x) {
21+
if (nchar(x) == 0) return(x)
22+
paste0(toupper(substr(x, 1, 1)), substr(x, 2, nchar(x)))
23+
}

_pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ reference:
4848
- title: "Package Information"
4949
desc: "Documentation about the package itself"
5050
contents:
51-
- myrpackage-package
51+
- "myrpackage-package" # Use quotes to ensure it's treated as a literal string
5252
- myrpackage-lifecycle
5353

5454
home:

0 commit comments

Comments
 (0)