Skip to content

Commit

Permalink
Update of format_date
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuyanagimoto committed Apr 26, 2024
1 parent 5ebcc12 commit e2c1739
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: typstcv
Type: Package
Title: CV for Quarto & Typst
Version: 0.0.0.9001
Version: 0.0.0.9002
Authors@R:
person(given = "Kazuharu",
family = "Yanagimoto",
Expand Down
35 changes: 23 additions & 12 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#'
#' @param data Data frame for resume entry
#' @param start Start date column name. Default is "start"
#' @param end End date column name. Default is "end"
#' @param sep Separator between start and end date. Default is "-"
#' @param format Date format. Default is "%M %Y"
#' @param end Optional. End date column name.
#' @param sep Separator between start and end date. Default is " - "
#' @param date_format Date format. Default is "%B %Y"
#' @param colname_date Name for the generated date column. Default is "date"
#' @param replace_na Value to replace NA values. Default is "Present"
#' @param sort_by Sort by "none", "start" or "end". Default is "none"
Expand All @@ -15,30 +15,41 @@
#'
#' @examples
#' work |>
#' format_date(format = "%Y", sort_by = "start") |>
#' format_date(end = "end", date_format = "%Y", sort_by = "start") |>
#' resume_entry()
#'
format_date <- function(data, start = "start", end = "end", sep = "-",
format = "%M %Y",

format_date <- function(data,
start = "start",
end = NULL,
sep = " - ",
date_format = "%B %Y",
colname_date = "date",
replace_na = "Present",
sort_by = "none",
decreasing = TRUE) {
data[[start]] <- format(data[[start]], format)
data[[end]] <- format(data[[end]], format)

# Replace NA values
data[[start]] <- format(data[[start]], date_format)
data[[start]][is.na(data[[start]])] <- replace_na
data[[end]][is.na(data[[end]])] <- replace_na

# Create date column
data[[colname_date]] <- paste(data[[start]], data[[end]], sep = sep)
if (is.null(end)) {
data[[colname_date]] <- data[[start]]
} else {
data[[end]] <- format(data[[end]], date_format)
data[[end]][is.na(data[[end]])] <- replace_na
data[[colname_date]] <- paste(data[[start]], data[[end]], sep = sep)
}

# Sort
if (sort_by == "start") {
data <- data[order(data[[start]], decreasing = decreasing), ]
} else if (sort_by == "end") {
} else if (sort_by == "end" && !is.null(end)) {
data <- data[order(data[[end]], decreasing = decreasing), ]
} else if (sort_by == "none") {
# Do nothing
} else {
stop("Invalid sort_by value. Use 'none', 'start' or 'end'")
}

return(data)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ work |>
format_date(
start = "date_start",
end = "date_end",
format = "%Y",
date_format = "%Y",
sep = "->",
sort_by = "start"
) |>
Expand Down
2 changes: 1 addition & 1 deletion README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ work |>
format_date(
start = "date_start",
end = "date_end",
format = "%Y",
date_format = "%Y",
sep = "->",
sort_by = "start"
) |>
Expand Down
14 changes: 7 additions & 7 deletions man/format_date.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/awesomecv/awesomecv.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ resume_entry(pub)
```{r}
#| output: asis
work |>
format_date(format = "%Y", sort_by = "start") |>
format_date(end = "end", date_format = "%Y", sort_by = "start") |>
resume_entry()
```

Expand Down

0 comments on commit e2c1739

Please sign in to comment.