Plurality.Style (Plurality v0.3.0)

Copy Markdown View Source

Case and style preservation for inflected words.

When a word is inflected, the transformation happens on a downcased copy. This module detects the casing style of the original input and reapplies it to the inflected result, so that "LEAF" becomes "LEAVES" (not "leaves") and "Leaf" becomes "Leaves" (not "leaves").

How it works

Inflection only changes the suffix of a word. This module finds the shared prefix between the original and the result via a byte-walking scan, slices it from the original (zero-copy sub-binary), and appends the remaining suffix from the inflected result.

For ALL CAPS input, a single String.upcase/1 on the whole result is faster and used instead.

Examples

OriginalInflectedOutput
"LEAF""leaves""LEAVES"
"Leaf""leaves""Leaves"
"leaf""leaves""leaves"
"ResourceAttachment""resourceattachments""ResourceAttachments"
"camelCase""camelcases""camelCases"

Usage

This module is used internally by Plurality.Engine and Plurality.Custom.

Summary

Functions

Transfers the casing pattern of original onto result.

Functions

match_style(original, result)

@spec match_style(String.t(), String.t()) :: String.t()

Transfers the casing pattern of original onto result.

Returns result unchanged when original and result are identical.

Examples

iex> Plurality.Style.match_style("LEAF", "leaves")
"LEAVES"

iex> Plurality.Style.match_style("Leaf", "leaves")
"Leaves"

iex> Plurality.Style.match_style("leaf", "leaves")
"leaves"

iex> Plurality.Style.match_style("sheep", "sheep")
"sheep"

iex> Plurality.Style.match_style("camelCase", "camelcases")
"camelCases"

iex> Plurality.Style.match_style("ResourceAttachment", "resourceattachments")
"ResourceAttachments"