titlecase

package module
v0.4.2 Latest Latest
Warning

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

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

README

titlecase GoDoc Go Report Card

🔡 titlecase is a small Go 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?

Rules

Word / Part-of-Speech Output Default Configure
Articles (a, an, the) Lowercase Enabled TODO
Subordinating conjunctions, only as and if Lowercase Enabled TODO
Coordinating conjunctions Lowercase Enabled TODO
Prepositions up to 4 letters Lowercase Enabled TODO
Everything else Capitalise n/a n/a
First word Capitalise Enabled AlwaysFirstWord
Last word Capitalise Enabled AlwaysLastWord
Second part of hyphenated word Capitalise Enabled TODO
First word after a colon Capitalise Enabled TODO
Uppercase word (e.g. IBM) Keep Enabled PersistUpperCase

Each of these rules can be configured by passing one or more a functional options to Convert. For example:

titlecase.Convert("the lord of the rings", titlecase.AlwaysFirstWord(false))

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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert(s string, opts ...Option) (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("lord of the flies")
	fmt.Println(s)
}
Output:
Lord of the Flies
Example (Film)
package main

import (
	"fmt"

	"github.com/revett/titlecase"
)

func main() {
	s, _ := titlecase.Convert("once upon a time in hollywood")
	fmt.Println(s)
}
Output:
Once Upon a Time in Hollywood
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 Debug added in v0.4.0

func Debug(c *converter)

Debug is a functional option for configuring debug logging to be enabled.

func WithParser added in v0.4.1

func WithParser(p grammar.Parser) func(*converter)

WithParser is a functional option that allows the default parsing function (which carries out tokenisation and part-of-speech tagging) to be set.

Types

type Option added in v0.4.1

type Option func(*converter)

Option is functional option for configuring the Convert function.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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