human_readable_slug

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MPL-2.0 Imports: 2 Imported by: 2

README

Human Readable Slug

Lightweight Go library generating a human-readable slug by avoiding some bad combinations like:

  • I and l
  • 0 and O
  • m and n
  • or if letter1 = letter2

Usage

To install this library, run this command:

go get -u github.com/anhgelus/human-readable-slug

And now you generate some slugs with the function GenerateSlug in github.com/anhgelus/human-readable-slug. E.g.:

slug := GenerateSlug(time.Now().Unix(), 7) // generates a 7 chars-length slug with the current timestamp as seed 

You can also check if a slug is human-readable with the function IsHumanReadable. E.g.:

IsHumanReadable("aab") // returns false with the default options
IsHumanReadable("Aab") // returns true with the default options

If you want to add a custom bad combination, you can use the function AddBadCombination.E.g.:

AddBadCombination('b', 'd') // add the combination "bd" and "db"
AddBadCombination('q', 'p') // add the combination "qp" and "pq"
Customize

If you want to customize the options of the slug generation, you must use the CustomSlug type.

  • HasCapitalLetters is true if you want to have capital letters (default: true)
  • HasNumbers is true if you want to have numbers (default: true)
  • CanBe2ConsecutiveLetters is true if you want to have 2 consecutive letters or more (default: false)

With this type you can now call every other functions except the AddBadCombination which is global.

options := CustomSlug{
    CanBe2ConsecutiveLetters: false,
    HasNumbers:               false,
    HasCapitalLetters:        true,
}
options.GenerateSlug(time.Now().Unix(), 7) // Will generate a 7 chars length slug without numbers
options.IsHumanReadable("helloWorld") // returns true
options.IsHumanReadable("hell0World") // returns false due to the `0`

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = CustomSlug{
	CanBe2ConsecutiveLetters: false,
	HasNumbers:               true,
	HasCapitalLetters:        true,
}

Functions

func AddBadCombination

func AddBadCombination(a rune, b rune)

func GenerateSlug

func GenerateSlug(random uint64, size uint) string

GenerateSlug returns a random slug with the size specified

func IsHumanReadable

func IsHumanReadable(slug string) bool

IsHumanReadable checks if a slug is human readable

Types

type CustomSlug

type CustomSlug struct {
	// Do you want "ABC..."?
	HasCapitalLetters bool
	// Do you want "0123..."
	HasNumbers bool
	// Do you want "aa" or "DD"...?
	CanBe2ConsecutiveLetters bool
	// contains filtered or unexported fields
}

func (*CustomSlug) GenerateSlug

func (options *CustomSlug) GenerateSlug(random uint64, size uint) string

GenerateSlug returns a random slug with the size specified

func (*CustomSlug) IsHumanReadable

func (options *CustomSlug) IsHumanReadable(slug string) bool

IsHumanReadable checks if a slug is human readable

Jump to

Keyboard shortcuts

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