text

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 12, 2026 License: Unlicense Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommonSentenceStarts = []string{"He", "She", "They", "His", "Her", "Their", "The", "It"}

CommonSentenceStarts lists pronouns and articles that JoinSentenceParts and AppendClause lowercase when they appear at the start of a non-first part, preventing awkward mid-sentence capitalisation.

Functions

func AppendAside

func AppendAside(s, clause string) string

AppendAside is like AppendClause but also appends a trailing comma, producing a parenthetical aside: "base, clause,".

func AppendClause

func AppendClause(s, clause string) string

AppendClause appends clause to s, inserting a comma separator when s is non-empty and does not already end with one. Words from CommonSentenceStarts at the start of clause are lowercased to avoid mid-sentence capitalisation.

func AppendRelated

func AppendRelated(s, clause string) string

AppendRelated joins s and clause with an HTML em-dash (—), stripping any sentence terminator from s first. The first letter of clause is lowercased unconditionally.

func CardinalNoun

func CardinalNoun(n int) string

CardinalNoun returns the English word for n. Values 0-199 are returned as words (e.g. "forty-two"); values above 199 fall back to a decimal digit string.

func CardinalSuffix

func CardinalSuffix(n int) string

CardinalSuffix returns the English ordinal suffix for n: "st", "nd", "rd", or "th". It correctly handles the 11/12/13 exceptions (e.g. 11 -> "th", not "st").

func CardinalWithUnit

func CardinalWithUnit(n int, singular string, plural string) string

CardinalWithUnit returns "one <singular>" when n is 1, and "<CardinalNoun(n)> <plural>" otherwise (e.g. "one child", "two children").

func FinishSentence

func FinishSentence(s string) string

FinishSentence trims trailing whitespace and soft punctuation (",", ":", ";") from s and appends a period if s does not already end with ".", "!", or "?". Returns an empty string for blank input.

func FormatSentence

func FormatSentence(s string) string

FormatSentence formats s as a complete, well-formed sentence.

func JoinList

func JoinList(strs []string) string

JoinList joins elements with commas and a final " and ", producing natural English list prose (e.g. "one, two and three"). Leading/trailing whitespace and punctuation are stripped from each element.

func JoinListOr

func JoinListOr(strs []string) string

JoinListOr is like JoinList but uses " or " before the final element.

func JoinSentenceParts

func JoinSentenceParts(parts ...string) string

JoinSentenceParts concatenates non-empty parts with a single space between them. Empty and whitespace-only parts are skipped. The colon ":" is special-cased to receive no leading space.

func JoinSentences

func JoinSentences(ss ...string) string

JoinSentences formats each argument as a complete sentence and joins them with a single space. Empty arguments are skipped.

func LowerFirst

func LowerFirst(s string) string

LowerFirst lowercases the first rune of s and returns the result. It panics if s is empty.

func LowerIfFirstWordIn

func LowerIfFirstWordIn(s string, words ...string) string

LowerIfFirstWordIn lowercases the first rune of s if s begins with one of the given words followed by a space. The match is an exact word check - a bare prefix is not enough. If no word matches, s is returned unchanged.

func MaybeAn

func MaybeAn(s string) string

MaybeAn returns a string intended to be appended to a literal "a" to form the correct indefinite article. It returns "n <s>" when s starts with a vowel (so "a"+"n apple" = "an apple") and " <s>" otherwise (so "a"+" book" = "a book").

func MaybePluralise

func MaybePluralise(s string, quantity int) string

MaybePluralise appends "s" to s when quantity is not 1.

func MaybePossessiveSuffix

func MaybePossessiveSuffix(s string) string

MaybePossessiveSuffix appends the correct English possessive suffix: "'" when s already ends in "s" (e.g. "Jones'"), or "'s" otherwise.

func MultiplicativeAdverb

func MultiplicativeAdverb(n int) string

MultiplicativeAdverb returns the English adverb for n occurrences: "no" for 0, "once" for 1, "twice" for 2, and "N times" for larger values.

func OrdinalNoun

func OrdinalNoun(n int) string

OrdinalNoun returns the English ordinal word for n (e.g. 1 -> "first", 21 -> "twenty-first"). Values above 100 are not handled and produce unexpected results.

func PrefixLines

func PrefixLines(s string, prefix string) string

PrefixLines prepends prefix to every line in s, including the first.

func RemoveAllWhitespace

func RemoveAllWhitespace(s string) string

RemoveAllWhitespace removes every whitespace character from s.

func RemoveRedundantWhitespace

func RemoveRedundantWhitespace(s string) string

RemoveRedundantWhitespace collapses each run of whitespace to a single space and trims leading and trailing whitespace.

func ReplaceFirstNumberWithCardinalNoun

func ReplaceFirstNumberWithCardinalNoun(s string) string

ReplaceFirstNumberWithCardinalNoun finds the first isolated digit sequence in s and replaces it with its English word form via CardinalNoun. If no digit sequence is found, s is returned unchanged.

func SmallCardinalNoun

func SmallCardinalNoun(n int) string

SmallCardinalNoun returns the English word for n when n is 0-5 ("no", "one", ..., "five"), and a decimal digit string for larger values. Use this instead of CardinalNoun when the number is expected to be small but a fallback to digits is acceptable for prose readability.

func StartsWithNumeral

func StartsWithNumeral(s string) bool

StartsWithNumeral reports whether s begins with an ASCII digit.

func StripNewlines

func StripNewlines(s string) string

StripNewlines replaces every newline character in s with a space.

func StripTerminator

func StripTerminator(s string) string

StripTerminator removes all trailing whitespace and punctuation (" ", ",", ":", ";", ".", "!", "?") from s.

func StripWasIs

func StripWasIs(st string) string

StripWasIs removes a leading "was " or "is " auxiliary verb from st, returning the remainder. Used to convert passive voice fragments into bare verb phrases when tense needs to change.

func UpperFirst

func UpperFirst(s string) string

UpperFirst trims surrounding whitespace from s, then uppercases its first rune. Returns an empty string if s is blank after trimming.

Types

type Para

type Para struct {
	// contains filtered or unexported fields
}

func (*Para) AddCompleteSentence

func (p *Para) AddCompleteSentence(ss ...string)

AddCompleteSentence combines the supplied strings into a formatted sentence and adds it to the para. Any open sentence is finished first. The para is left at the start of a new sentence.

func (*Para) AppendAsAside

func (p *Para) AppendAsAside(clause string)

AppendAsAside appends an aside to the current sentence, preceding it with a comma if necessary and appending a comma after the clause.

func (*Para) AppendClause

func (p *Para) AppendClause(ss ...string)

AppendClause appends a clause to the current sentence, preceding it with a comma if necessary.

func (*Para) AppendList

func (p *Para) AppendList(ss ...string)

func (*Para) Continue

func (p *Para) Continue(ss ...string)

Continue appends parts to the current sentence. Empty and whitespace-only parts are skipped. A colon ":" is appended without a preceding space. The first letter is not uppercased until the sentence is finalized.

func (*Para) Current added in v0.0.3

func (p *Para) Current() string

Current returns the accumulated text without finalizing the current sentence. Unlike Text, it does not add a period or uppercase the first letter.

func (*Para) CurrentSentenceLength

func (p *Para) CurrentSentenceLength() int

func (*Para) CurrentSentenceWords

func (p *Para) CurrentSentenceWords() int

func (*Para) DropSentence

func (p *Para) DropSentence()

DropSentence drops the current sentence

func (*Para) FinishSentence

func (p *Para) FinishSentence(ss ...string)

FinishSentence completes the current sentence, It combines the supplied strings into text which are added the current sentence and it then terminates the sentence with a full stop and leaves the paragraph ready for the next one.

func (*Para) FinishSentenceWithTerminator

func (p *Para) FinishSentenceWithTerminator(t string)

FinishSentence completes the current sentence, terminating it with t and leaves the paragraph ready for the next one.

func (*Para) IsEmpty added in v0.0.3

func (p *Para) IsEmpty() bool

func (*Para) Length

func (p *Para) Length() int

func (*Para) ReplaceSentence

func (p *Para) ReplaceSentence(s string)

ReplaceSentence replaces the current sentence with s

func (*Para) StartSentence added in v0.0.3

func (p *Para) StartSentence(ss ...string)

StartSentence begins a new sentence by finishing any existing sentence and combining the strings into text which becomes the current sentence. No punctuation or formatting is performed on the new sentence.

func (*Para) Text

func (p *Para) Text() string

Text finalizes the current sentence and returns the full paragraph as a string.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL