  # --------------------------------------------------------------------------------------- #

I believe these functions were for when I was parsing the excel file 
  that the mkting team made (before I had direct access to the FB reporting tool)

No longer needed, but preserving

  # --------------------------------------------------------------------------------------- #
# --------------------------------------------------------------------------------------- #
  # --------------------------------------------------------------------------------------- #



parseNameDate <- function(x) {

  parenLeft  <- regexpr(pattern="\\(", x)
  parenRight <- regexpr(pattern="\\)", x)

  Name <- substr(x, 1, parenLeft-1)
  Name <- stringr::str_trim(Name)

  DateRng <- substr(x, parenLeft+1, parenRight-1)

  DateSplat <- strsplit(DateRng, "\\s*-\\s*")

  ## Error-check.  In case not all two-date ranges
  # --------------------------------------------------------------------------------------- #
  lens <- sapply(DateSplat, length)                                                         

    ##  Too many dates detected: Throw an error                                              ##
    if (any(lens>2))
      stop("\nCertain info entries have a peculiar date range and we cannot continue.",
           "They are: \n\t", paste0(x[lens>2], collapse="\n\t"), "\n",
           "Please fix the original file before proceeding.\n")

    ##  Too few dates detected:  Add NA's                                                    ##
    if (any(offs <- (lens < 2) )) {
      for (i in which(offs))                                                                    
        DateSplat[[i]] <- c(DateSplat[[i]], rep(NA, 2-lens[[i]]))                                 
    }
  # --------------------------------------------------------------------------------------- #


  DateSplat <- do.call(rbind, DateSplat)
  From <- lubridate::mdy(DateSplat[, 1])
  To   <- lubridate::mdy(DateSplat[, 2])


  return(list(Name=Name, DateFrom=as.character(From), DateTo=as.character(To)))
}


BannerType <- function(x, beginString=FALSE, originalIncludedInReturn=FALSE) {

  # preserve a backup if returning original
  if (originalIncludedInReturn)
    orig <- x

  if (is.factor(x))
    x <- as.character(x)

  # the different types to search for
  Types <- c("Promoted Post", "Post Engagement", "Social Ads", 
            "Sponsored Search", "Sponsored Post", "Sponsored Result",
            "Sponsored Stories", "Sponsored Story")


  type  <- rep("Other", length(x))
  notes <- rep(NA, length(x))

  for (t in Types) {
    pat <- paste0(ifelse(beginString, "^", ""), t)
    found <- grepl(pat, x)
    type[found] <- t
    notes[found] <- sub(pat, "", x)[found]
  }

  # Trim the texts
  # remove hyphens
  notes <- gsub("-", "", notes)
  # remove white space
  notes <- gsub("^\\s*|\\s*$", "", notes)

  notes[notes==""] <- NA

  notes[type=="Other"] <- x[type=="Other"]
  
  type[type=="Sponsored Stories"] <- "Sponsored Story"

  ## Return the table with the original x and an appropriate name
  if (originalIncludedInReturn)
    return(setnames(data.table(orig, type, notes), 
                    c(as.character(match.call()[[2]]), "type", "name")) )

  data.table(type, notes)

}

