Documentation
¶
Overview ¶
titlecase is a package for converting strings to title case format, which is a style traditionally used for the titles of books, movies, songs and plays. The style is also often used for headlines in newspapers, essays and blogs.
Rules ¶
Generally, all words are capitalised apart from:
- Articles (a, an, the)
- Conjunctions (and, or, but)
- Prepositions (in, on, for, up)
However the specifics of the rules depend on the chosen style as the title case format is not an agreed universal standard.
Styles ¶
There are a number of different styles to choose from, each with their own ruleset:
- AMA
- AP
- APA
- Bluebook
- Chicago
- MLA
- New York Times
- Wikipedia
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlwaysFirstWord ¶ added in v0.4.0
func AlwaysFirstWord(b bool) func(*converter)
AlwaysFirstWord is a functional option for configuring if the first word should always be capitalised.
Example ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert(
"the lord of the rings", titlecase.AlwaysFirstWord(false),
)
fmt.Println(s)
}
Output: the Lord of the Rings
func AlwaysLastWord ¶ added in v0.4.0
func AlwaysLastWord(b bool) func(*converter)
AlwaysLastWord is a functional option for configuring if the last word should always be capitalised.
Example ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert(
"the lord of the", titlecase.AlwaysLastWord(false),
)
fmt.Println(s)
}
Output: The Lord of the
func Convert ¶
Convert returns the provided string to title case, where all major words are capitalised and all minor words all lowercased. Zero or more functional options can be passed to configure how the string is converted.
Example (Book) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert("the catcher in the rye")
fmt.Println(s)
}
Output: The Catcher in the Rye
Example (Film) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert("the lord of the rings")
fmt.Println(s)
}
Output: The Lord of the Rings
Example (Play) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert("much ado about nothing")
fmt.Println(s)
}
Output: Much Ado About Nothing
Example (UpperCase) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert("new mac OSX features in 11.2")
fmt.Println(s)
}
Output: New Mac OSX Features in 11.2
func Debug ¶ added in v0.4.0
func Debug(c *converter)
Debug is a functional option for configuring debug logging to be enabled.
Example ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert("the lord of the rings", titlecase.Debug)
fmt.Println(s)
}
Output: The Lord of the Rings
func MaxMinorWordLength ¶ added in v0.3.0
func MaxMinorWordLength(n int) func(*converter)
MaxMinorWordLength is a functional option for configuring the maximum length that a minor word (conjunction, preposition or determiner) can be, after which it will be capitalised.
Example ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert(
"the lord of the rings", titlecase.MaxMinorWordLength(1),
)
fmt.Println(s)
}
Output: The Lord Of The Rings
func PersistUpperCase ¶ added in v0.4.0
func PersistUpperCase(b bool) func(*converter)
PersistUpperCase is a functional option for configuring if words that are originally uppercase should be kept untouched.
Example ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
s, _ := titlecase.Convert(
"tim davie works at the BBC as director general",
titlecase.PersistUpperCase(false),
)
fmt.Println(s)
}
Output: Tim Davie Works at the Bbc as Director General
Types ¶
This section is empty.