File: format_order.R

package info (click to toggle)
r-cran-parameters 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,852 kB
  • sloc: sh: 16; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#' Order (first, second, ...) formatting
#'
#' Format order.
#'
#' @param order value or vector of orders.
#' @param textual Return number as words. If `FALSE`, will run [insight::format_value()].
#' @param ... Arguments to be passed to [insight::format_value()] if `textual` is `FALSE`.
#'
#' @return A formatted string.
#' @examples
#' format_order(2)
#' format_order(8)
#' format_order(25, textual = FALSE)
#' @export
format_order <- function(order, textual = TRUE, ...) {
  if (textual) {
    order <- insight::format_number(order)
    parts <- unlist(strsplit(order, " ", fixed = TRUE))
    parts[length(parts)] <- switch(utils::tail(parts, 1),
      one = "first",
      two = "second",
      three = "third",
      four = "fourth",
      five = "fifth",
      six = "sixth",
      seven = "seventh",
      eight = "eigth",
      nine = "ninth"
    )
    out <- paste(parts, collapse = " ")
  } else {
    number <- insight::format_value(order, digits = 0, ...)
    last <- substr(number, nchar(number), nchar(number))
    last_two <- substr(number, nchar(number) - 1, nchar(number))
    # exceptions
    if (last_two %in% c(11, 12, 13)) {
      out <- paste0(number, "th")
    } else {
      out <- paste0(
        number,
        switch(last,
          `1` = "st",
          `2` = "nd",
          `3` = "rd",
          `4` = "th",
          `5` = "th",
          `6` = "th",
          `7` = "th",
          `8` = "th",
          `9` = "th",
          `0` = "th"
        )
      )
    }
  }

  out
}