inflector

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2017 License: MIT Imports: 4 Imported by: 5

README

Inflector

Build Status GoDoc Coverage Status Go Report Card

Inflector is a set of functions aimed to transform strings. You can pluralize, singularize and do other transformations.

Examples
inflector.Pluralize("person") // "people"
inflector.Singularize("posts") // "post"

For more examples, take a look on the examples_test.go.

Installation

$ go get github.com/tangzero/inflector

Update

$ go get -u github.com/tangzero/inflector

License
MIT License

Copyright (c) 2017 Jairo Luiz aka TangZero

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ShouldCache = false

ShouldCache set if the inflector should (or not) cache the inflections

Functions

func Camelize

func Camelize(term string) string

Camelize converts strings to UpperCamelCase.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Camelize("my_account"))
	fmt.Println(inflector.Camelize("user-profile"))
	fmt.Println(inflector.Camelize("ssl_error"))
	fmt.Println(inflector.Camelize("http_connection_timeout"))
	fmt.Println(inflector.Camelize("restful_controller"))
	fmt.Println(inflector.Camelize("multiple_http_calls"))
}
Output:

MyAccount
UserProfile
SSLError
HTTPConnectionTimeout
RESTfulController
MultipleHTTPCalls

func ClearCache

func ClearCache()

ClearCache clear the inflection cache. Both for singulars and plurals.

func Dasherize

func Dasherize(term string) string

Dasherize converts strings to dashed, lowercase form.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Dasherize("MyAccount"))
	fmt.Println(inflector.Dasherize("user_profile"))
}
Output:

my-account
user-profile

func ForeignKey

func ForeignKey(term string) string

ForeignKey creates a foreign key name from an ORM model name.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.ForeignKey("Message"))
	fmt.Println(inflector.ForeignKey("AdminPost"))
}
Output:

message_id
admin_post_id

func Ordinal

func Ordinal(number int64) string

Ordinal returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Ordinal(1))
	fmt.Println(inflector.Ordinal(2))
	fmt.Println(inflector.Ordinal(14))
	fmt.Println(inflector.Ordinal(1002))
	fmt.Println(inflector.Ordinal(1003))
	fmt.Println(inflector.Ordinal(-11))
	fmt.Println(inflector.Ordinal(-1021))
}
Output:

st
nd
th
nd
rd
th
st

func Ordinalize

func Ordinalize(number int64) string

Ordinalize turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Ordinalize(1))
	fmt.Println(inflector.Ordinalize(2))
	fmt.Println(inflector.Ordinalize(14))
	fmt.Println(inflector.Ordinalize(1002))
	fmt.Println(inflector.Ordinalize(1003))
	fmt.Println(inflector.Ordinalize(-11))
	fmt.Println(inflector.Ordinalize(-1021))
}
Output:

1st
2nd
14th
1002nd
1003rd
-11th
-1021st

func Pluralize

func Pluralize(singular string) string

Pluralize returns the plural form of the word in the string.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Pluralize("post"))
	fmt.Println(inflector.Pluralize("octopus"))
	fmt.Println(inflector.Pluralize("sheep"))
	fmt.Println(inflector.Pluralize("words"))
	fmt.Println(inflector.Pluralize("CamelOctopus"))
}
Output:

posts
octopi
sheep
words
CamelOctopi

func Singularize

func Singularize(plural string) string

Singularize returns the singular form of a word in a string.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Singularize("posts"))
	fmt.Println(inflector.Singularize("octopi"))
	fmt.Println(inflector.Singularize("sheep"))
	fmt.Println(inflector.Singularize("word"))
	fmt.Println(inflector.Singularize("CamelOctopi"))
}
Output:

post
octopus
sheep
word
CamelOctopus

func Tableize

func Tableize(term string) string

Tableize creates the name of a table for an ORM model.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Tableize("RawScaledScorer"))
	fmt.Println(inflector.Tableize("ham_and_egg"))
	fmt.Println(inflector.Tableize("fancyCategory"))
}
Output:

raw_scaled_scorers
ham_and_eggs
fancy_categories

func Underscorize

func Underscorize(term string) string

Underscorize converts strings to underscored, lowercase form.

Example
package main

import (
	"fmt"

	"github.com/tangzero/inflector"
)

func main() {
	fmt.Println(inflector.Underscorize("MyAccount"))
	fmt.Println(inflector.Underscorize("user-profile"))
	fmt.Println(inflector.Underscorize("SSLError"))
	fmt.Println(inflector.Underscorize("HTTPConnectionTimeout"))
	fmt.Println(inflector.Underscorize("RESTfulController"))
	fmt.Println(inflector.Underscorize("MultipleHTTPCalls"))
}
Output:

my_account
user_profile
ssl_error
http_connection_timeout
restful_controller
multiple_http_calls

Types

This section is empty.

Jump to

Keyboard shortcuts

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