titlecase

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2021 License: MIT Imports: 3 Imported by: 1

README

titlecase GoDoc Go Report Card

🔡 Title case converter, supports multiple styles (AMA, AP, New York Times, Wikipedia etc).

Usage

View full documentation

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?

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 Convert

func Convert(s string, opts ...func(*rule.Set)) (string, error)

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 FirstWord added in v0.3.0

func FirstWord(b bool) func(*rule.Set)

FirstWord 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(
		"much ado about nothing", titlecase.FirstWord(false),
	)
	fmt.Println(s)
}
Output:
much Ado About Nothing

func LastWord added in v0.3.0

func LastWord(b bool) func(*rule.Set)

LastWord 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 rings", titlecase.LastWord(false),
	)
	fmt.Println(s)
}
Output:
The Lord of the rings

func MaxMinorWordLength added in v0.3.0

func MaxMinorWordLength(n int) func(*rule.Set)

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(2),
	)
	fmt.Println(s)
}
Output:
The Lord of The Rings

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