truncate

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2019 License: MIT Imports: 2 Imported by: 34

README

truncate

Build Status GoDoc

Go library for truncating strings

Sample usage:

package truncate_test

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func ExampleTruncate() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 17, "...", truncate.PositionEnd)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
	// Output: This is a long... : 17 characters
}

func ExampleTruncate_second() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 15, "...", truncate.PositionStart)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
	// Output: ... a long text : 15 characters
}

func ExampleTruncate_third() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 5, "zzz", truncate.PositionMiddle)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
	// Output: Tzzzt : 5 characters
}

func ExampleTruncator() {
	text := "This is a long text"
	truncated := truncate.Truncator(text, 9, truncate.CutStrategy{})
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
	// Output: This is a : 9 characters
}

Documentation

Overview

Package truncate provides set of strategies to truncate strings

Index

Examples

Constants

View Source
const DEFAULT_OMISSION = "…"

Variables

This section is empty.

Functions

func Truncate

func Truncate(str string, length int, omission string, pos TruncatePosition) string

Truncate truncates string according the parameters

Example
package main

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func main() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 17, "...", truncate.PositionEnd)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
}
Output:

This is a long... : 17 characters
Example (Second)
package main

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func main() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 15, "...", truncate.PositionStart)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
}
Output:

... a long text : 15 characters
Example (Third)
package main

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func main() {
	text := "This is a long text"
	truncated := truncate.Truncate(text, 5, "zzz", truncate.PositionMiddle)
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
}
Output:

Tzzzt : 5 characters

func Truncator

func Truncator(str string, length int, strategy Strategy) string

Truncator cuts a string to length using the truncation strategy

Example
package main

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func main() {
	text := "This is a long text"
	truncated := truncate.Truncator(text, 9, truncate.CutStrategy{})
	fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated))
}
Output:

This is a : 9 characters

Types

type CutEllipsisLeadingStrategy

type CutEllipsisLeadingStrategy struct{}

CutEllipsisLeadingStrategy simply truncates the string from the start the desired length and adds ellipsis at the front

func (CutEllipsisLeadingStrategy) Truncate

func (s CutEllipsisLeadingStrategy) Truncate(str string, length int) string

type CutEllipsisStrategy

type CutEllipsisStrategy struct{}

CutEllipsisStrategy simply truncates the string to the desired length and adds ellipsis at the end

func (CutEllipsisStrategy) Truncate

func (s CutEllipsisStrategy) Truncate(str string, length int) string

type CutStrategy

type CutStrategy struct{}

CutStrategy simply truncates the string to the desired length

func (CutStrategy) Truncate

func (CutStrategy) Truncate(str string, length int) string

type EllipsisMiddleStrategy

type EllipsisMiddleStrategy struct{}

EllipsisMiddleStrategy truncates the string to the desired length and adds ellipsis in the middle

func (EllipsisMiddleStrategy) Truncate

func (e EllipsisMiddleStrategy) Truncate(str string, length int) string

type Strategy

type Strategy interface {
	Truncate(string, int) string
}

Strategy is an interface for truncation strategy

type TruncatePosition

type TruncatePosition int
const (
	PositionStart TruncatePosition = iota
	PositionMiddle
	PositionEnd
)

Jump to

Keyboard shortcuts

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