flect

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 13 Imported by: 312

README

Flect

Go Reference Standard Test Go Report Card

This is a new inflection engine to replace https://github.com/markbates/inflect designed to be more modular, more readable, and easier to fix issues on than the original.

Flect provides word inflection features such as Singularize and Pluralize for English nouns and text utility features such as Camelize, Capitalize, Humanize, and more.

Due to the flexibly-complex nature of English noun inflection, it is almost impossible to cover all exceptions (such as identical/irregular plural). With this reason along with the main purpose of Flect, which is to make it easy to develop web application in Go, Flect has limitations with its own rules.

  • It covers regular rule (adding -s or -es and of the word)
  • It covers well-known irregular rules (such as -is to -es, -f to -ves, etc)
  • It covers well-known irregular words (such as children, men, etc)
  • If a word can be countable and uncountable like milk or time, it will be treated as countable.
  • If a word has more than one plural forms, which means it has at least one irregular plural, we tried to find most popular one. (The selected plural could be odd to you, please feel free to open an issue with back data)
    • For example, we selected "stadiums" over "stadia", "dwarfs" over "dwarves"
    • One or combination of en.wiktionary.org, britannica.com, and trends.google.com are used to check the recent usage trends.
  • However, we cannot cover all cases and some of our cases could not fit with your situation. You can override the default with functions such as InsertPlural(), InsertSingular(), or LoadInfrections().
  • If you have a json file named inflections.json in your application root, the file will be automatically loaded as your custom inflection dictionary.

Installation

$ go get github.com/gobuffalo/flect

Packages

github.com/gobuffalo/flect

The github.com/gobuffalo/flect package contains "basic" inflection tools, like pluralization, singularization, etc...

The Ident Type

In addition to helpful methods that take in a string and return a string, there is an Ident type that can be used to create new, custom, inflection rules.

The Ident type contains two fields.

  • Original - This is the original string that was used to create the Ident
  • Parts - This is a []string that represents all of the "parts" of the string, that have been split apart, making the segments easier to work with

Examples of creating new inflection rules using Ident can be found in the github.com/gobuffalo/flect/name package.

github.com/gobuffalo/flect/name

The github.com/gobuffalo/flect/name package contains more "business" inflection rules like creating proper names, table names, etc...

Documentation

Overview

Package flect is a new inflection engine to replace [https://github.com/markbates/inflect](https://github.com/markbates/inflect) designed to be more modular, more readable, and easier to fix issues on than the original.

Index

Constants

View Source
const Version = "v1.0.0"

Version holds Flect version number

Variables

This section is empty.

Functions

func AddPlural

func AddPlural(suffix string, repl string)

AddPlural adds a rule that will replace the given suffix with the replacement suffix. The name is confusing. This function will be deprecated in the next release.

func AddSingular

func AddSingular(ext string, repl string)

AddSingular adds a rule that will replace the given suffix with the replacement suffix. The name is confusing. This function will be deprecated in the next release.

func Camelize

func Camelize(s string) string

Camelize returns a camelize version of a string

bob dylan = bobDylan
widget_id = widgetID
WidgetID = widgetID

func Capitalize

func Capitalize(s string) string

Capitalize will cap the first letter of string

user = User
bob dylan = Bob dylan
widget_id = Widget_id

func Dasherize

func Dasherize(s string) string

Dasherize returns an alphanumeric, lowercased, dashed string

Donald E. Knuth = donald-e-knuth
Test with + sign = test-with-sign
admin/WidgetID = admin-widget-id

func Humanize added in v0.1.4

func Humanize(s string) string

Humanize returns first letter of sentence capitalized. Common acronyms are capitalized as well. Other capital letters in string are left as provided.

employee_salary = Employee salary
employee_id = employee ID
employee_mobile_number = Employee mobile number
first_Name = First Name
firstName = First Name

func InsertPluralRule added in v1.0.0

func InsertPluralRule(suffix, repl string)

InsertPluralRule inserts a rule that will replace the given suffix with the repl(acement) at the begining of the list of the pluralize rules.

func InsertSingularRule added in v1.0.0

func InsertSingularRule(suffix, repl string)

InsertSingularRule inserts a rule that will replace the given suffix with the repl(acement) at the beginning of the list of the singularize rules.

func LoadAcronyms

func LoadAcronyms(r io.Reader) error

LoadAcronyms loads rules from io.Reader param

func LoadInflections

func LoadInflections(r io.Reader) error

LoadInflections loads rules from io.Reader param

func Ordinalize

func Ordinalize(s string) string

Ordinalize converts a number to an ordinal version

42 = 42nd
45 = 45th
1 = 1st

func Pascalize

func Pascalize(s string) string

Pascalize returns a string with each segment capitalized

user = User
bob dylan = BobDylan
widget_id = WidgetID

func Pluralize

func Pluralize(s string) string

Pluralize returns a plural version of the string

user = users
person = people
datum = data

func PluralizeWithSize added in v0.2.2

func PluralizeWithSize(s string, i int) string

PluralizeWithSize will pluralize a string taking a number number into account.

PluralizeWithSize("user", 1) = user
PluralizeWithSize("user", 2) = users

func Singularize

func Singularize(s string) string

Singularize returns a singular version of the string

users = user
data = datum
people = person

func SingularizeWithSize added in v0.2.2

func SingularizeWithSize(s string, i int) string

SingularizeWithSize will singular a string taking a number number into account.

SingularizeWithSize("user", 1) = user
SingularizeWithSize("user", 2) = users

func Titleize

func Titleize(s string) string

Titleize will capitalize the start of each part

"Nice to see you!" = "Nice To See You!"
"i've read a book! have you?" = "I've Read A Book! Have You?"
"This is `code` ok" = "This Is `code` OK"

func Underscore

func Underscore(s string) string

Underscore a string

bob dylan --> bob_dylan
Nice to see you! --> nice_to_see_you
widgetID --> widget_id

Types

type CustomDataParser

type CustomDataParser func(io.Reader) error

CustomDataParser are functions that parse data like acronyms or plurals in the shape of a io.Reader it receives.

type Ident

type Ident struct {
	Original string
	Parts    []string
}

Ident represents the string and it's parts

func New

func New(s string) Ident

New creates a new Ident from the string

func (Ident) Camelize

func (i Ident) Camelize() Ident

Camelize returns a camelize version of a string

bob dylan = bobDylan
widget_id = widgetID
WidgetID = widgetID

func (Ident) Capitalize

func (i Ident) Capitalize() Ident

Capitalize will cap the first letter of string

user = User
bob dylan = Bob dylan
widget_id = Widget_id

func (Ident) Dasherize

func (i Ident) Dasherize() Ident

Dasherize returns an alphanumeric, lowercased, dashed string

Donald E. Knuth = donald-e-knuth
Test with + sign = test-with-sign
admin/WidgetID = admin-widget-id

func (Ident) Humanize added in v0.1.4

func (i Ident) Humanize() Ident

Humanize First letter of sentence capitalized

func (*Ident) LastPart added in v0.2.2

func (i *Ident) LastPart() string

LastPart returns the last part/word of the original string

func (Ident) MarshalText

func (i Ident) MarshalText() ([]byte, error)

MarshalText marshals Ident into byte array

func (Ident) Ordinalize

func (i Ident) Ordinalize() Ident

Ordinalize converts a number to an ordinal version

42 = 42nd
45 = 45th
1 = 1st

func (Ident) Pascalize

func (i Ident) Pascalize() Ident

Pascalize returns a string with each segment capitalized

user = User
bob dylan = BobDylan
widget_id = WidgetID

func (Ident) Pluralize

func (i Ident) Pluralize() Ident

Pluralize returns a plural version of the string

user = users
person = people
datum = data

func (Ident) ReplaceSuffix added in v0.2.2

func (i Ident) ReplaceSuffix(orig, new string) Ident

ReplaceSuffix creates a new Ident with the original suffix replaced by new

func (Ident) Singularize

func (i Ident) Singularize() Ident

Singularize returns a singular version of the string

users = user
data = datum
people = person

func (Ident) String

func (i Ident) String() string

String implements fmt.Stringer and returns the original string

func (Ident) Titleize

func (i Ident) Titleize() Ident

Titleize will capitalize the start of each part

"Nice to see you!" = "Nice To See You!"
"i've read a book! have you?" = "I've Read A Book! Have You?"
"This is `code` ok" = "This Is `code` OK"

func (Ident) ToLower

func (i Ident) ToLower() Ident

ToLower is a convience wrapper for strings.ToLower

func (Ident) ToUpper

func (i Ident) ToUpper() Ident

ToUpper is a convience wrapper for strings.ToUpper

func (Ident) Underscore

func (i Ident) Underscore() Ident

Underscore a string

bob dylan --> bob_dylan
Nice to see you! --> nice_to_see_you
widgetID --> widget_id

func (*Ident) UnmarshalText

func (i *Ident) UnmarshalText(data []byte) error

UnmarshalText unmarshalls byte array into the Ident

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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