|
| 1 | +# server-side processing for the SearchBuilder extension |
| 2 | +# https://datatables.net/extensions/searchbuilder/ |
| 3 | + |
| 4 | +sbEvaluateSearch = function(search, data) { |
| 5 | + # https://datatables.net/reference/option/searchBuilder.preDefined |
| 6 | + stopifnot(search$logic %in% c('AND', 'OR')) |
| 7 | + Reduce( |
| 8 | + switch(search$logic, AND = `&`, OR = `|`), |
| 9 | + lapply(search$criteria, sbEvaluateCriteria, data) |
| 10 | + ) |
| 11 | +} |
| 12 | + |
| 13 | +sbEvaluateCriteria = function(criteria, data) { |
| 14 | + # https://datatables.net/reference/option/searchBuilder.preDefined.criteria |
| 15 | + if ('logic' %in% names(criteria)) { |
| 16 | + # this is a sub-group |
| 17 | + sbEvaluateSearch(criteria, data) |
| 18 | + } else { |
| 19 | + # this is a criteria |
| 20 | + cond = criteria$condition |
| 21 | + type = criteria$type |
| 22 | + x = data[[criteria$origData %||% criteria$data]] |
| 23 | + v = sbParseValue(sbExtractValue(criteria), type) |
| 24 | + sbEvaluateCondition(cond, type, x, v) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +sbExtractValue = function(criteria) { |
| 29 | + if (criteria$condition %in% c('between', '!between')) { |
| 30 | + # array values are passed in a funny way to R |
| 31 | + c(criteria$value1, criteria$value2) |
| 32 | + } else { |
| 33 | + criteria$value |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +sbParseValue = function(value, type) { |
| 38 | + # TODO: handle 'moment' and 'luxon' types mentioned in condition reference |
| 39 | + if (type %in% c('string', 'html')) { |
| 40 | + as.character(value) |
| 41 | + } else if (type %in% c('num', 'num-fmt', 'html-num', 'html-num-fmt')) { |
| 42 | + as.numeric(value) |
| 43 | + } else if (type %in% c('date')) { |
| 44 | + as.Date(value) |
| 45 | + } else { |
| 46 | + stop(sprintf('unsupported criteria type "%s"', type)) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +sbEvaluateCondition = function(condition, type, x, value) { |
| 51 | + # https://datatables.net/reference/option/searchBuilder.preDefined.criteria.condition |
| 52 | + if (type %in% c('string', 'html')) { |
| 53 | + switch( |
| 54 | + condition, |
| 55 | + '!=' = x != value, |
| 56 | + '!null' = !is.na(x) & x != '', |
| 57 | + '=' = x == value, |
| 58 | + 'contains' = grepl(value, x, fixed = TRUE), |
| 59 | + '!contains' = !grepl(value, x, fixed = TRUE), |
| 60 | + 'ends' = endsWith(as.character(x), value), |
| 61 | + '!ends' = !endsWith(as.character(x), value), |
| 62 | + 'null' = is.na(x) | x == '', |
| 63 | + 'starts' = startsWith(as.character(x), value), |
| 64 | + '!starts' = !startsWith(as.character(x), value), |
| 65 | + stop(sprintf('unsupported condition "%s" for criteria type "%s"', condition, type)) |
| 66 | + ) |
| 67 | + } else if (type %in% c('num', 'num-fmt', 'html-num', 'html-num-fmt')) { |
| 68 | + switch( |
| 69 | + condition, |
| 70 | + '!=' = x != value, |
| 71 | + '!null' = !is.na(x), |
| 72 | + '<' = x < value, |
| 73 | + '<=' = x <= value, |
| 74 | + '=' = x == value, |
| 75 | + '>' = x > value, |
| 76 | + '>=' = x >= value, |
| 77 | + 'between' = x >= value[1] & x <= value[2], |
| 78 | + '!between' = x < value[1] | x > value[2], |
| 79 | + 'null' = is.na(x), |
| 80 | + stop(sprintf('unsupported condition "%s" for criteria type "%s"', condition, type)) |
| 81 | + ) |
| 82 | + } else if (type %in% c('date', 'moment', 'luxon')) { |
| 83 | + switch( |
| 84 | + condition, |
| 85 | + '!=' = x != value, |
| 86 | + '!null' = !is.na(x), |
| 87 | + '<' = x < value, |
| 88 | + '=' = x == value, |
| 89 | + '>' = x > value, |
| 90 | + 'between' = x >= value[1] & x <= value[2], |
| 91 | + '!between' = x < value[1] | x > value[2], |
| 92 | + 'null' = is.na(x), |
| 93 | + stop(sprintf('unsupported condition "%s" for criteria type "%s"', condition, type)) |
| 94 | + ) |
| 95 | + } else { |
| 96 | + stop(sprintf('unsupported criteria type "%s"', type)) |
| 97 | + } |
| 98 | +} |
0 commit comments