File: camelCase.R

package info (click to toggle)
r-cran-googledrive 2.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,584 kB
  • sloc: sh: 13; makefile: 2
file content (42 lines) | stat: -rw-r--r-- 1,256 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
# camelCase() and toCamel() taken from
# https://github.com/r-dbi/bigrquery/blob/main/R/camelCase.R

# in theory, belongs in gargle
# but then we'd need to export it and I'm not sure it's worth it
# https://github.com/r-lib/gargle

# camelCase vs snake_case policy
# ** all arguments in functions exported by googledrive shall be snake_case**
#
# HOWEVER, the Drive API is camelCase
# both wrt parameter names and many of their string values
# examples: `pageSize`, `mimeType`, `corpora = "allDrives"`
# therefore, whenever we pass `...` through, we process with toCamel()
# this means user can say `page_size = 20` and we send `pageSize = 20`
#
# we do not trumpet this snake_case to camelCase conversion in the docs,
# because many of the strings/values we handle are camelCase and we ARE not
# going to alter them
# there's too much potential for confusion
#
# at this point, snake_case to camelCase is a very quiet feature
camelCase <- function(x) {
  gsub("_(.)", "\\U\\1", x, perl = TRUE)
}

toCamel <- function(x) {
  if (is.list(x)) {
    x[] <- lapply(x, toCamel)
  }

  if (!is.null(names(x))) {
    names(x) <- camelCase(names(x))
  }

  x
}

# added later
snake_case <- function(x) {
  gsub("([a-z0-9])([A-Z])", "\\1_\\L\\2", x, perl = TRUE)
}