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 Convert ¶
Convert returns the provided string to title case, where all major words are capitalised and all minor words all lowercased.
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
func CustomRuleset ¶ added in v0.2.0
func CustomRuleset(rs Ruleset) func(*converter)
CustomRuleset is a functional option for configuring the converter to use a custom ruleset.
Example (FirstWord) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
rs := titlecase.DefaultRuleset()
rs.FirstWord = false
s, _ := titlecase.Convert(
"much ado about nothing", titlecase.CustomRuleset(rs),
)
fmt.Println(s)
}
Output: much Ado About Nothing
Example (LastWord) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
rs := titlecase.DefaultRuleset()
rs.LastWord = false
s, _ := titlecase.Convert(
"the lord of the rings", titlecase.CustomRuleset(rs),
)
fmt.Println(s)
}
Output: The Lord of the rings
Example (MaxMinorWordLength) ¶
package main
import (
"fmt"
"github.com/revett/titlecase"
)
func main() {
rs := titlecase.DefaultRuleset()
rs.MaxMinorWordLength = 2
s, _ := titlecase.Convert(
"the lord of the rings", titlecase.CustomRuleset(rs),
)
fmt.Println(s)
}
Output: The Lord of The Rings