Skip to content

Ensures that a character vector of identifiers is unique by appending numeric suffixes (_2, _3, ...) when duplicates are found, while preserving the original order.

Usage

whapi_make_unique(x)

Arguments

x

A character vector of IDs (possibly with duplicates).

Value

A character vector of the same length with unique IDs.

Details

This helper is particularly useful when generating button IDs for WhatsApp interactive messages via Whapi. Even after whapi_slugifying labels, duplicates may remain (e.g., two buttons both titled "Yes"). The function guarantees uniqueness by incrementally appending a suffix.

Algorithm:

  • Iterates through x in order;

  • Keeps a counter of how many times each ID has appeared;

  • First occurrence is left unchanged;

  • Subsequent duplicates get suffixed with _<n>.

See also

whapi_slugify() for slug-safe ID creation.

Examples

whapi_make_unique(c("yes", "no", "yes", "yes", "maybe", "no"))
#> [1] "yes"   "no"    "yes_2" "yes_3" "maybe" "no_2" 
# -> "yes", "no", "yes_2", "yes_3", "maybe", "no_2"

# Combined with whapi_slugify
titles <- c("Yes!", "Yes!", "No?")
ids <- whapi_make_unique(whapi_slugify(titles))
tibble::tibble(title = titles, id = ids)
#> # A tibble: 3 × 2
#>   title id   
#>   <chr> <chr>
#> 1 Yes!  yes  
#> 2 Yes!  yes_2
#> 3 No?   no