Skip to content

Commit 9cff892

Browse files
committed
Add ignore.case param. to + op; closes #47
1 parent 311706b commit 9cff892

File tree

4 files changed

+42
-8
lines changed

4 files changed

+42
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: RefManageR
2-
Version: 1.1.0
2+
Version: 1.2.0
33
Title: Straightforward 'BibTeX' and 'BibLaTeX' Bibliography Management
44
Authors@R: person(c("Mathew", "W."), "McLean", role = c("aut", "cre"),
55
email = "[email protected]")

R/BibEntryAddOp.R

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#' is a duplicate? Can include \code{"bibtype"} to check entry type and
1414
#' \code{"key"} to check entry keys. Specifying \code{"all"} checks all fields
1515
#' using \code{\link{duplicated}}.
16+
#' @param ignore.case logical; if \code{TRUE}, case is ignored when determining
17+
#' if fields are duplicates.
1618
#' @return an object of class BibEntry
1719
#' @family operators
1820
#' @seealso \code{\link{duplicated}}, \code{\link{unique}}
@@ -27,7 +29,7 @@
2729
#' bib2 <- bib[45:length(bib)]
2830
#'
2931
#' ## The following is FALSE because the parent entry of one entry in bib1
30-
#' ## is in bib2, so the child entry in is expanded in the BibEntry object
32+
#' ## is in bib2, so the child entry is expanded in the BibEntry object
3133
#' ## returned by `[` to include the fields inherited from the dropped parent
3234
#' identical(merge(bib1, bib2, 'all'), bib)
3335
#' toBiblatex(bib1[[1L]])
@@ -54,6 +56,7 @@
5456
#' @keywords methods
5557
`+.BibEntry` <- function(e1, e2){
5658
fields.to.check <- .BibOptions$merge.fields.to.check
59+
ignore.case <- .BibOptions$ignore.case
5760
awl <- "all" %in% fields.to.check
5861
att1 <- attributes(e1)[bibentry_list_attribute_names]
5962
att2 <- attributes(e2)[bibentry_list_attribute_names]
@@ -81,17 +84,26 @@
8184
possible.dup <- unlist(e2$bibtype) %in% unlist(e1$bibtype)
8285
}
8386
remain.dup.f <- setdiff(fields.to.check, c('bibtype', 'key'))
84-
87+
8588
if (length(dup.ind <- possible.dup) && length(remain.dup.f)){
89+
ty <- unclass(e1)
90+
ty <- lapply(ty, function(y, f, ignore.case){
91+
y <- y[f]
92+
if (ignore.case)
93+
y <- lapply(y, tolower)
94+
y
95+
}, f = remain.dup.f, ignore.case = ignore.case)
8696
dup.ind <- vapply(unclass(e2[possible.dup]),
87-
function(x, y, flds){
97+
function(x, y, flds, ignore.case){
8898
x <- x[flds]
99+
if (ignore.case)
100+
x <- lapply(x, tolower)
89101
for (i in seq_along(y)){
90-
if (identical(y[[i]][flds], x))
102+
if (identical(y[[i]], x))
91103
return(TRUE)
92104
}
93105
return(FALSE)
94-
}, FALSE, y = unclass(e1), flds = remain.dup.f)
106+
}, FALSE, y = ty, flds = remain.dup.f, ignore.case = ignore.case)
95107
if (length(dup.ind))
96108
dup.ind <- which(dup.ind)
97109
}
@@ -128,8 +140,10 @@
128140
#' @rdname merge.BibEntry
129141
merge.BibEntry <- function(x, y,
130142
fields.to.check = BibOptions()$merge.fields.to.check,
143+
ignore.case = BibOptions()$ignore.case,
131144
...){
132-
oldfields <- BibOptions(merge.fields.to.check = fields.to.check)
145+
oldfields <- BibOptions(merge.fields.to.check = fields.to.check,
146+
ignore.case = ignore.case)
133147
on.exit(BibOptions(oldfields))
134148
`+.BibEntry`(x, y)
135149
}

inst/NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Changes in Version 1.2.0 (2018-04-24)
2+
--------------------------------------------------------
3+
4+
* `+.BibEntry` and `merge.BibEntry` gain an argument ignore.case,
5+
which defaults to `BibOptions()$ignore.case` (`TRUE`) so that case is
6+
ignore when checking for duplicate fields (h/t Justin Calabrese #47)
7+
8+
19
Changes in Version 1.1.0 (2018-04-02)
210
--------------------------------------------------------
311

tests/testthat/test-methods.R

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ test_that("merge function, two fields to check", {
5151
expect_equal(length(res), 6L)
5252
})
5353

54+
test_that("addition operator ignore.case",
55+
{
56+
bib1 <- BibEntry(key = "k1", title = "THE TITLE", author = "Smith, John",
57+
bibtype = "Misc", year = 2018)
58+
bib2 <- BibEntry(key = "k2", title = "The Title", author = "Smith, John",
59+
bibtype = "Misc", year = 2018)
60+
BibOptions(ignore.case = TRUE, merge.fields.to.check = c("year", "title"))
61+
expect_message(bib1 + bib2, "Only duplicates")
62+
expect_length(merge(bib1, bib2, ignore.case = FALSE,
63+
fields.to.check = "title"), 2)
64+
})
65+
5466
test_that("head and tail", {
5567
expect_length(head(bib), 6L)
5668
expect_equal(tail(bib, 3), bib[(length(bib)-2):length(bib)])
@@ -67,7 +79,7 @@ test_that("open", {
6779

6880
## no error if cannot open
6981
expect_message(open(testbib, open.field = "eprint"),
70-
"Could not open the specified entry.")
82+
"Could not open the specified entry.")
7183

7284
open(bib["kastenholz"]) # DOI
7385
testbib <- BibEntry(bibtype = "Misc", key = "arxiv", eprinttype = "arxiv",

0 commit comments

Comments
 (0)