-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathbase-provider.R
218 lines (200 loc) · 6.47 KB
/
base-provider.R
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# modified from the python library faker:
# https://github.com/joke2k/faker/blob/master/faker/providers/__init__.py
#' BaseProvider
#'
#' @export
#' @param allowed_locales fetch the allowed locales for this provider
#' @note You cannot instantiate the Parent providers. You must use one of
#' the localized one.
#' @examples
#' (x <- BaseProvider$new())
#'
#' x$numerify("#%%asdf221?")
#' x$lexify("#%%asdf221?")
#' x$bothify("#%%asdf221?")
BaseProvider <- R6::R6Class(
"BaseProvider",
inherit = BareProvider,
public = list(
#' @description check a locale to see if it exists, if not, stop with
#' error message
#' @param x a locale name, e.g, 'bg_BG'
#' @return returns nothing if locale is supported; stops w/ message if not
check_locale = function(x) check_locale_(x),
#' @description fetch the allowed locales for this provider
allowed_locales = function() private$locales,
#' @description Create a new Provider object
#' @return A new object
initialize = function() {
if (self$provider != "BaseProvider") {
if (is.null(self$locale)) raise_class(self$provider)
}
},
#' @description Print method for provider
#' @param ... ignored by this method
print = function(...) {
cat("<", self$provider, ">\n", sep = " ")
cat("locale: ", self$locale, sep = "")
}
),
active = list(
#' @field locale (character) locale of this Provider.
locale = function() private$locale_
),
private = list(
locales = c(
"en_US"
),
locale_ = NULL,
provider_ = "BaseProvider"
)
)
#' A NonLocalized Provider that contains all the selection
#' and creation elements, but not the locales.
#' That way we can still inherit an do useful stuff
#' for providers that have no locale.
BareProvider <- R6::R6Class(
"BareProvider",
public = list(
#' @description pick a random element from vector/list
#' @param x vector or list
#' @return a single element from x
random_element = function(x) {
if (length(x) == 0) {
return("")
}
if (inherits(x, "character")) {
if (!any(nzchar(x))) {
return("")
}
}
x[sample.int(n = length(x), size = 1)]
},
#' @description pick a random element with probability from vector/list
#' @param x vector or list
random_element_prob = function(x) {
if (length(x) == 0) {
return("")
}
if (inherits(x, "character")) {
if (!any(nzchar(x))) {
return("")
}
}
choices <- names(x)
probs <- unname(unlist(x))
sample(choices, size = 1, prob = probs)
},
#' @description any number of random integers from a min, max
#' @param min the minimum value. default: 0
#' @param max the maximum value. default: 9999
#' @param size number of values to return. default: 1
#' @return random integer
random_int = function(min = 0, max = 9999, size = 1) {
stopifnot(max >= min)
num <- max - min + 1
sample.int(n = num, size = size, replace = TRUE) + (min - 1)
},
#' @description random integer between 0 and 9
random_digit = function() {
self$random_element(0:9)
},
#' @description random integer between 1 and 9
random_digit_not_zero = function() {
self$random_element(1:9)
},
#' @description random integer between 0 and 9 or empty character string
random_digit_or_empty = function() {
self$random_element(c(0:9, ""))
},
#' @description random integer between 1 and 9 or empty character string
random_digit_not_zero_or_empty = function() {
self$random_element(c(1:9, ""))
},
#' @description random letter
random_letter = function() {
# Returns a random letter (between a-z and A-Z)
self$random_element(c(letters, LETTERS))
},
#' @description replace a template with numbers
#' @param text (character) a string
numerify = function(text = "###") {
text <- do_match(text, "#", self$random_digit)
text <- do_match(text, "%", self$random_digit_not_zero)
text <- do_match(text, "!", self$random_digit_or_empty)
text <- do_match(text, "@", self$random_digit_not_zero_or_empty)
return(text)
},
#' @description replace a template with letters
#' @param text (character) a string
lexify = function(text = "????") {
# Replaces all question mark ('?') occurrences with a random letter
do_match(text, "?", self$random_letter)
},
#' @description both numerify and lexify together
#' @param text (character) a string
bothify = function(text = "## ??") {
# Replaces all placeholders with random numbers and letters.
self$lexify(self$numerify(text))
},
#' @description Returns a random value near number
#' @param number value to which the result must be near
#' @param le result must be lower or equal to number
#' @param ge result must be greater or equal to number
#' @param min the minimum value. default: `NULL`
#' @param max the maximum value. default: `NULL`
#' @return a random int near number
randomize_nb_elements = function(number = 10, le = FALSE, ge = FALSE,
min = NULL, max = NULL) {
if (le && ge) {
return(number)
}
"_min" <- if (ge) 100 else 60
"_max" <- if (le) 100 else 140
nb <- as.integer(number * self$random_int(`_min`, `_max`) / 100)
if (!is.null(min) && nb < min) nb <- min
if (!is.null(max) && nb > min) nb <- max
return(nb)
},
#' @description Print method for provider
#' @param ... ignored by this method
print = function(...) {
cat("<", self$provider, ">\n", sep = " ")
}
),
active = list(
#' @field provider Display the provider name.
provider = function() private$provider_
),
private = list(
provider_ = "BareProvider"
)
)
check_locale_ <- function(x, z = available_locales) {
if (!x %in% z) {
stop(x, " not in set of available locales", call. = FALSE)
}
}
n_matches <- function(text, pattern) {
tmp <- gregexpr(paste0("\\", pattern), text)[[1]]
if (length(tmp) == 1) {
if (tmp == -1) 0 else tmp
} else {
length(tmp)
}
}
replace_loop <- function(x, pattern, repl) {
for (i in seq_along(repl)) {
x <- sub(paste0("\\", pattern), repl[i], x)
}
return(x)
}
do_match <- function(text, pattern, fun) {
nm <- n_matches(text, pattern)
if (nm > 0) {
pat <- replicate(nm, eval(fun)())
replace_loop(text, pattern, pat)
} else {
return(text)
}
}