titlecase

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: MIT Imports: 2 Imported by: 1

README

titlecase GoDoc

🔡 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(*converter)) (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 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

Types

type Ruleset added in v0.2.0

type Ruleset struct {
	FirstWord          bool
	LastWord           bool
	MaxMinorWordLength int
}

Ruleset defines a set of rules for a given style.

func DefaultRuleset added in v0.2.0

func DefaultRuleset() Ruleset

DefaultRuleset defines the default ruleset used by the package.

Jump to

Keyboard shortcuts

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