titlecase

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: MIT Imports: 4 Imported by: 1

README

titlecase GoDoc Go Report Card

🔡 titlecase is a small package that applies the title case style to English text. In title case, all major words are capitalised, while minor words are lowercased. A simple example would be Lord of the Flies.

Usage

View full documentation (godoc) →

package main

import (
	"log"

	"github.com/revett/titlecase"
)

func main() {
	s, err := titlecase.Convert("the lord of the rings")
	if err != nil {
		log.Fatal(err)
	}

	log.Println(s) // 2021/04/25 21:09:50 The Lord of the Rings
}

Further reading: What Is Title Case?

Examples

The documentation for Convert includes a number of testable examples.

Dependencies

Credit

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

func Convert(s string, opts ...func(*converter)) (string, error)

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.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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