or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

byte-sizes.mdenglish.mdindex.mdnumber-formatting.mdordinals.mdsi-notation.mdtime-formatting.md
tile.json

english.mddocs/

English Language Utilities

Provides English-specific utilities for pluralization and formatting word lists with conjunctions and Oxford commas.

Import

import "github.com/dustin/go-humanize/english"

Pluralization

Plural

func Plural(quantity int, singular, plural string) string

Formats an integer and a string into a single pluralized string. Returns the quantity followed by the appropriate singular or plural form of the word.

Parameters:

  • quantity - The number of items
  • singular - The singular form of the word
  • plural - The plural form of the word (use empty string for regular pluralization)

Returns: String with quantity and pluralized word (e.g., "1 object", "42 objects")

Example:

english.Plural(1, "object", "")          // "1 object"
english.Plural(42, "object", "")         // "42 objects"
english.Plural(0, "object", "")          // "0 objects"
english.Plural(2, "bus", "")             // "2 buses"
english.Plural(1, "child", "children")   // "1 child"
english.Plural(5, "child", "children")   // "5 children"
english.Plural(99, "locus", "loci")      // "99 loci"

PluralWord

func PluralWord(quantity int, singular, plural string) string

Returns just the pluralized word form (without the quantity) based on the quantity. Uses simple English pluralization rules if the plural form is not explicitly provided.

Parameters:

  • quantity - The number of items (determines singular vs plural)
  • singular - The singular form of the word
  • plural - The plural form of the word (use empty string for regular pluralization)

Returns: The appropriate word form (e.g., "object" or "objects")

Example:

english.PluralWord(1, "object", "")        // "object"
english.PluralWord(42, "object", "")       // "objects"
english.PluralWord(0, "object", "")        // "objects"
english.PluralWord(2, "bus", "")           // "buses"
english.PluralWord(1, "child", "children") // "child"
english.PluralWord(5, "child", "children") // "children"
english.PluralWord(99, "locus", "loci")    // "loci"

Regular Pluralization Rules

When the plural form is an empty string, the function applies regular English pluralization rules:

  • Words ending in "s", "x", "z", "ch", "sh": add "es" (bus → buses, box → boxes)
  • Words ending in consonant + "y": replace "y" with "ies" (city → cities)
  • Words ending in "f" or "fe": replace with "ves" (knife → knives, leaf → leaves)
  • Most other words: add "s" (cat → cats, dog → dogs)

Note: These rules handle many common cases but not all irregular plurals. For irregular plurals (e.g., "child" → "children", "person" → "people"), provide the explicit plural form.

Word Series

WordSeries

func WordSeries(words []string, conjunction string) string

Converts a list of words into a word series in English without using an Oxford comma (serial comma).

Parameters:

  • words - Slice of words to format
  • conjunction - The conjunction to use (typically "and" or "or")

Returns: Formatted word series

Example:

english.WordSeries([]string{"foo"}, "and")
// "foo"

english.WordSeries([]string{"foo", "bar"}, "and")
// "foo and bar"

english.WordSeries([]string{"foo", "bar", "baz"}, "and")
// "foo, bar and baz"  (no Oxford comma)

english.WordSeries([]string{"red", "green", "blue", "yellow"}, "or")
// "red, green, blue or yellow"

english.WordSeries([]string{}, "and")
// ""

english.WordSeries([]string{"Alice", "Bob", "Charlie"}, "and")
// "Alice, Bob and Charlie"

OxfordWordSeries

func OxfordWordSeries(words []string, conjunction string) string

Converts a list of words into a word series in English using an Oxford comma (serial comma) before the final conjunction.

Parameters:

  • words - Slice of words to format
  • conjunction - The conjunction to use (typically "and" or "or")

Returns: Formatted word series with Oxford comma

Example:

english.OxfordWordSeries([]string{"foo"}, "and")
// "foo"

english.OxfordWordSeries([]string{"foo", "bar"}, "and")
// "foo and bar"

english.OxfordWordSeries([]string{"foo", "bar", "baz"}, "and")
// "foo, bar, and baz"  (Oxford comma before "and")

english.OxfordWordSeries([]string{"red", "green", "blue", "yellow"}, "or")
// "red, green, blue, or yellow"

english.OxfordWordSeries([]string{}, "and")
// ""

english.OxfordWordSeries([]string{"Alice", "Bob", "Charlie"}, "and")
// "Alice, Bob, and Charlie"

Common Use Cases

Dynamic Message Generation

count := 5
message := fmt.Sprintf("You have %s",
	english.Plural(count, "unread message", ""))
// Output: "You have 5 unread messages"

User-Friendly Lists

users := []string{"Alice", "Bob", "Charlie"}
fmt.Printf("Online users: %s",
	english.WordSeries(users, "and"))
// Output: "Online users: Alice, Bob and Charlie"

// Or with Oxford comma:
fmt.Printf("Online users: %s",
	english.OxfordWordSeries(users, "and"))
// Output: "Online users: Alice, Bob, and Charlie"

Search Results

matches := 3
fmt.Printf("Found %s",
	english.Plural(matches, "match", "matches"))
// Output: "Found 3 matches"

matches = 1
fmt.Printf("Found %s",
	english.Plural(matches, "match", "matches"))
// Output: "Found 1 match"

Options and Choices

options := []string{"save", "cancel", "help"}
fmt.Printf("Available commands: %s",
	english.OxfordWordSeries(options, "or"))
// Output: "Available commands: save, cancel, or help"

Irregular Plurals

childCount := 3
fmt.Printf("The family has %s",
	english.Plural(childCount, "child", "children"))
// Output: "The family has 3 children"

mouseCount := 2
fmt.Printf("Detected %s",
	english.Plural(mouseCount, "mouse", "mice"))
// Output: "Detected 2 mice"

Combining Multiple Features

// Get just the word form for complex messages
numFiles := 42
fileWord := english.PluralWord(numFiles, "file", "")
numFolders := 1
folderWord := english.PluralWord(numFolders, "folder", "")

fmt.Printf("Selected %d %s and %d %s",
	numFiles, fileWord, numFolders, folderWord)
// Output: "Selected 42 files and 1 folder"

Empty Lists

// Both WordSeries functions handle empty slices gracefully
items := []string{}
result := english.WordSeries(items, "and")
// result = ""

if result == "" {
	fmt.Println("No items available")
}

Notes

  • Oxford Comma: The Oxford comma (also called serial comma) is a comma placed before the coordinating conjunction in a list of three or more items. Its use is a matter of style preference. Use WordSeries without it or OxfordWordSeries with it.
  • Regular Pluralization: The automatic pluralization rules work for most regular English words but not all irregular plurals. Always provide explicit plural forms for irregular words.
  • Case Sensitivity: The functions preserve the case of input words. They don't perform any case transformations.
  • ASCII Limitation: The special cases for regular pluralization are not guaranteed to work correctly for non-ASCII strings.
  • Zero Quantity: Zero is treated as plural (e.g., "0 objects", not "0 object").