Learn R Programming

whapi (version 0.0.2)

whapi_make_unique: Make identifiers unique while preserving order

Description

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)

Value

A character vector of the same length with unique IDs.

Arguments

x

A character vector of IDs (possibly with duplicates).

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

Run this code
whapi_make_unique(c("yes", "no", "yes", "yes", "maybe", "no"))
# -> "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)

Run the code above in your browser using DataLab