@cryptotaxi247 / kubo / commits / ddc5bb89d

vendor inflection package (MIT)

Brian Tiger Chow committed Jan 20, 2015 at 02:57 UTC ddc5bb89d73a67036c758a2d5a09b5d05bf431ff
11 files changed +554
Godeps/Godeps.json
+4
@@ -60,6 +60,10 @@
60 "ImportPath": "github.com/bren2010/proquint",
61 "Rev": "5958552242606512f714d2e93513b380f43f9991"
62 },
63 + {
64 + "ImportPath": "github.com/briantigerchow/inflect",
65 + "Rev": "cef1f9cc2234281dc58ea10be7e9aad5e282ecab"
66 + },
67 {
68 "ImportPath": "github.com/camlistore/lock",
69 "Rev": "ae27720f340952636b826119b58130b9c1a847a0"
Godeps/_workspace/src/github.com/briantigerchow/inflect/.gitignore new
+1
@@ -0,0 +1 @@
1 +.DS_Store
Godeps/_workspace/src/github.com/briantigerchow/inflect/README.md new
+63
@@ -0,0 +1,63 @@
1 +# inflect
2 +
3 +Inflections made easy for Go.
4 +
5 +[![Build Status](https://drone.io/github.com/chuckpreslar/inflect/status.png)](https://drone.io/github.com/chuckpreslar/inflect/latest)
6 +
7 +## Installation
8 +
9 +With Google's [Go](http://www.golang.org) installed on your machine:
10 +
11 + $ go get -u github.com/chuckpreslar/inflect
12 +
13 +## Usage
14 +
15 +```go
16 +import (
17 + "github.com/chuckpreslar/inflect"
18 +)
19 +
20 +func main() {
21 + inflect.Pluralize("user") // users
22 + inflect.Pluralize("knife") // knives
23 +
24 + inflect.Singularize("orders") // order
25 +
26 + inflect.UpperCamelCase("this_is_underscored_mixedCased-And-Hyphenated") // ThisIsUnderscoredMixedCasedAndHyphenated
27 +}
28 +```
29 +
30 +## Support
31 +
32 +* Pluralization and singularization of words with proper language rules.
33 +* Case transformation from and to upper camel casing, lower camel casing, underscoring, hyphenating, and constantization.
34 +
35 +## Documentation
36 +
37 +View godoc or visit [godoc.org](http://godoc.org/github.com/chuckpreslar/inflect).
38 +
39 + $ godoc inflect
40 +
41 +## License
42 +
43 +> The MIT License (MIT)
44 +
45 +> Copyright (c) 2013 Chuck Preslar
46 +
47 +> Permission is hereby granted, free of charge, to any person obtaining a copy
48 +> of this software and associated documentation files (the "Software"), to deal
49 +> in the Software without restriction, including without limitation the rights
50 +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51 +> copies of the Software, and to permit persons to whom the Software is
52 +> furnished to do so, subject to the following conditions:
53 +
54 +> The above copyright notice and this permission notice shall be included in
55 +> all copies or substantial portions of the Software.
56 +
57 +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58 +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59 +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60 +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61 +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62 +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
63 +> THE SOFTWARE.
Godeps/_workspace/src/github.com/briantigerchow/inflect/inflect.go new
+122
@@ -0,0 +1,122 @@
1 +// Package inflect provides an inflector.
2 +package inflect
3 +
4 +import (
5 + "fmt"
6 + "regexp"
7 + "strings"
8 +)
9 +
10 +func Pluralize(str string) string {
11 + if inflector, ok := Languages[Language]; ok {
12 + return inflector.Pluralize(str)
13 + }
14 +
15 + return str
16 +}
17 +
18 +func Singularize(str string) string {
19 + if inflector, ok := Languages[Language]; ok {
20 + return inflector.Singularize(str)
21 + }
22 +
23 + return str
24 +}
25 +
26 +func FromNumber(str string, n int) string {
27 + switch n {
28 + case 1:
29 + return Singularize(str)
30 + default:
31 + return Pluralize(str)
32 + }
33 +}
34 +
35 +// Split's a string so that it can be converted to a different casing.
36 +// Splits on underscores, hyphens, spaces and camel casing.
37 +func split(str string) []string {
38 + // FIXME: This isn't a perfect solution.
39 + // ex. WEiRD CaSINg (Support for 13 year old developers)
40 + return strings.Split(regexp.MustCompile(`-|_|([a-z])([A-Z])`).ReplaceAllString(strings.Trim(str, `-|_| `), `$1 $2`), ` `)
41 +}
42 +
43 +// UpperCamelCase converts a string to it's upper camel case version.
44 +func UpperCamelCase(str string) string {
45 + pieces := split(str)
46 +
47 + for index, s := range pieces {
48 + pieces[index] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(s[0])), strings.ToLower(s[1:]))
49 + }
50 +
51 + return strings.Join(pieces, ``)
52 +}
53 +
54 +// LowerCamelCase converts a string to it's lower camel case version.
55 +func LowerCamelCase(str string) string {
56 + pieces := split(str)
57 +
58 + pieces[0] = strings.ToLower(pieces[0])
59 +
60 + for i := 1; i < len(pieces); i++ {
61 + pieces[i] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(pieces[i][0])), strings.ToLower(pieces[i][1:]))
62 + }
63 +
64 + return strings.Join(pieces, ``)
65 +}
66 +
67 +// Underscore converts a string to it's underscored version.
68 +func Underscore(str string) string {
69 + pieces := split(str)
70 +
71 + for index, piece := range pieces {
72 + pieces[index] = strings.ToLower(piece)
73 + }
74 +
75 + return strings.Join(pieces, `_`)
76 +}
77 +
78 +// Hyphenate converts a string to it's hyphenated version.
79 +func Hyphenate(str string) string {
80 + pieces := split(str)
81 +
82 + for index, piece := range pieces {
83 + pieces[index] = strings.ToLower(piece)
84 + }
85 +
86 + return strings.Join(pieces, `-`)
87 +}
88 +
89 +// Constantize converts a string to it's constantized version.
90 +func Constantize(str string) string {
91 + pieces := split(str)
92 +
93 + for index, piece := range pieces {
94 + pieces[index] = strings.ToUpper(piece)
95 + }
96 +
97 + return strings.Join(pieces, `_`)
98 +}
99 +
100 +// Humanize converts a string to it's humanized version.
101 +func Humanize(str string) string {
102 + pieces := split(str)
103 +
104 + pieces[0] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(pieces[0][0])), strings.ToLower(pieces[0][1:]))
105 +
106 + for i := 1; i < len(pieces); i++ {
107 + pieces[i] = fmt.Sprintf(`%v`, strings.ToLower(pieces[i]))
108 + }
109 +
110 + return strings.Join(pieces, ` `)
111 +}
112 +
113 +// Titleize converts a string to it's titleized version.
114 +func Titleize(str string) string {
115 + pieces := split(str)
116 +
117 + for i := 0; i < len(pieces); i++ {
118 + pieces[i] = fmt.Sprintf(`%v%v`, strings.ToUpper(string(pieces[i][0])), strings.ToLower(pieces[i][1:]))
119 + }
120 +
121 + return strings.Join(pieces, ` `)
122 +}
Godeps/_workspace/src/github.com/briantigerchow/inflect/inflect_test.go new
+126
@@ -0,0 +1,126 @@
1 +package inflect
2 +
3 +import (
4 + "testing"
5 +)
6 +
7 +func TestPluralize(t *testing.T) {
8 + tests := []string{"half", "potato", "cello", "disco", "chef", "wife", "poppy", "sty", "football", "tester", "play", "hero", "tooth", "mouse", "goose", "person", "foot", "money", "monkey", "calf", "lie", "auto", "studio"}
9 + results := []string{"halves", "potatoes", "cellos", "discos", "chefs", "wives", "poppies", "sties", "footballs", "testers", "plays", "heroes", "teeth", "mice", "geese", "people", "feet", "money", "monkeys", "calves", "lies", "autos", "studios"}
10 +
11 + for index, test := range tests {
12 + if result := Pluralize(test); result != results[index] {
13 + t.Errorf("Expected %v, got %v", results[index], result)
14 + }
15 + }
16 +}
17 +
18 +func TestCommonPluralize(t *testing.T) {
19 + tests := []string{"user", "order", "product", "verse", "test", "upload", "class", "course", "game", "score", "body", "life", "dice"}
20 + results := []string{"users", "orders", "products", "verses", "tests", "uploads", "classes", "courses", "games", "scores", "bodies", "lives", "die"}
21 +
22 + for index, test := range tests {
23 + if result := Pluralize(test); result != results[index] {
24 + t.Errorf("Expected %v, got %v", results[index], result)
25 + }
26 + }
27 +}
28 +
29 +func TestSingularization(t *testing.T) {
30 + tests := []string{"halves", "potatoes", "cellos", "discos", "chefs", "wives", "poppies", "sties", "footballs", "testers", "plays", "heroes", "teeth", "mice", "geese", "people", "feet", "money", "monkeys", "calves", "lies", "autos", "studios"}
31 + results := []string{"half", "potato", "cello", "disco", "chef", "wife", "poppy", "sty", "football", "tester", "play", "hero", "tooth", "mouse", "goose", "person", "foot", "money", "monkey", "calf", "lie", "auto", "studio"}
32 +
33 + for index, test := range tests {
34 + if result := Singularize(test); result != results[index] {
35 + t.Errorf("Expected %v, got %v", results[index], result)
36 + }
37 + }
38 +}
39 +
40 +func TestCommonSingularization(t *testing.T) {
41 + tests := []string{"users", "orders", "products", "verses", "tests", "uploads", "classes", "courses", "games", "scores", "bodies", "lives", "die"}
42 + results := []string{"user", "order", "product", "verse", "test", "upload", "class", "course", "game", "score", "body", "life", "dice"}
43 +
44 + for index, test := range tests {
45 + if result := Singularize(test); result != results[index] {
46 + t.Errorf("Expected %v, got %v", results[index], result)
47 + }
48 + }
49 +}
50 +
51 +func TestUpperCamelCase(t *testing.T) {
52 + tests := []string{"_pre", "post_", " spaced", "single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
53 + results := []string{"Pre", "Post", "Spaced", "Single", "LowerCamelCase", "UnderScored", "HyphenAted", "UpperCamelCase", "SpacedOut"}
54 +
55 + for index, test := range tests {
56 + if result := UpperCamelCase(test); result != results[index] {
57 + t.Errorf("Expected %v, got %v", results[index], result)
58 + }
59 + }
60 +}
61 +
62 +func TestLowerCamelCase(t *testing.T) {
63 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
64 + results := []string{"single", "lowerCamelCase", "underScored", "hyphenAted", "upperCamelCase", "spacedOut"}
65 +
66 + for index, test := range tests {
67 + if result := LowerCamelCase(test); result != results[index] {
68 + t.Errorf("Expected %v, got %v", results[index], result)
69 + }
70 + }
71 +}
72 +
73 +func TestUnderscore(t *testing.T) {
74 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
75 + results := []string{"single", "lower_camel_case", "under_scored", "hyphen_ated", "upper_camel_case", "spaced_out"}
76 +
77 + for index, test := range tests {
78 + if result := Underscore(test); result != results[index] {
79 + t.Errorf("Expected %v, got %v", results[index], result)
80 + }
81 + }
82 +}
83 +
84 +func TestHyphenate(t *testing.T) {
85 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
86 + results := []string{"single", "lower-camel-case", "under-scored", "hyphen-ated", "upper-camel-case", "spaced-out"}
87 +
88 + for index, test := range tests {
89 + if result := Hyphenate(test); result != results[index] {
90 + t.Errorf("Expected %v, got %v", results[index], result)
91 + }
92 + }
93 +}
94 +
95 +func TestConstantize(t *testing.T) {
96 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
97 + results := []string{"SINGLE", "LOWER_CAMEL_CASE", "UNDER_SCORED", "HYPHEN_ATED", "UPPER_CAMEL_CASE", "SPACED_OUT"}
98 +
99 + for index, test := range tests {
100 + if result := Constantize(test); result != results[index] {
101 + t.Errorf("Expected %v, got %v", results[index], result)
102 + }
103 + }
104 +}
105 +
106 +func TestHumanize(t *testing.T) {
107 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
108 + results := []string{"Single", "Lower camel case", "Under scored", "Hyphen ated", "Upper camel case", "Spaced out"}
109 +
110 + for index, test := range tests {
111 + if result := Humanize(test); result != results[index] {
112 + t.Errorf("Expected %v, got %v", results[index], result)
113 + }
114 + }
115 +}
116 +
117 +func TestTitleize(t *testing.T) {
118 + tests := []string{"single", "lowerCamelCase", "under_scored", "hyphen-ated", "UpperCamelCase", "spaced Out"}
119 + results := []string{"Single", "Lower Camel Case", "Under Scored", "Hyphen Ated", "Upper Camel Case", "Spaced Out"}
120 +
121 + for index, test := range tests {
122 + if result := Titleize(test); result != results[index] {
123 + t.Errorf("Expected %v, got %v", results[index], result)
124 + }
125 + }
126 +}
Godeps/_workspace/src/github.com/briantigerchow/inflect/languages.go new
+19
@@ -0,0 +1,19 @@
1 +// Package inflect provides an inflector.
2 +package inflect
3 +
4 +import (
5 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/inflect/languages"
6 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/inflect/types"
7 +)
8 +
9 +var (
10 + // Language to use when converting a word from it's plural to
11 + // singular forms and vice versa.
12 + Language = "en"
13 +
14 + // Languages avaiable for converting a word from
15 + // it's plural to singular forms and vice versa.
16 + Languages = map[string]*types.LanguageType{
17 + "en": languages.English,
18 + }
19 +)
Godeps/_workspace/src/github.com/briantigerchow/inflect/languages/english.go new
+64
@@ -0,0 +1,64 @@
1 +// Package languages provides language rules to use with the inflect package.
2 +package languages
3 +
4 +import (
5 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/briantigerchow/inflect/types"
6 +)
7 +
8 +// Defines irregular words, uncountables words, and pluralization/singularization rules for the English language.
9 +//
10 +// FIXME: Singular/Plural rules could be better, I went to school for engineering, not English.
11 +var English = types.Language("en").
12 + // Pluralization rules.
13 + Plural(`(auto)$`, `${1}s`).
14 + Plural(`(s|ss|sh|ch|x|to|ro|ho|jo)$`, `${1}es`).
15 + Plural(`(i)fe$`, `${1}ves`).
16 + Plural(`(t|f|g)oo(th|se|t)$`, `${1}ee${2}`).
17 + Plural(`(a|e|i|o|u)y$`, `${1}ys`).
18 + Plural(`(m|l)ouse$`, `${1}ice`).
19 + Plural(`(al|ie|l)f$`, `${1}ves`).
20 + Plural(`(d)ice`, `${1}ie`).
21 + Plural(`y$`, `ies`).
22 + Plural(`$`, `s`).
23 + // Singularization rules.
24 + Singular(`(auto)s$`, `${1}`).
25 + Singular(`(rse)s$`, `${1}`).
26 + Singular(`(s|ss|sh|ch|x|to|ro|ho|jo)es$`, `${1}`).
27 + Singular(`(i)ves$`, `${1}fe`).
28 + Singular(`(t|f|g)ee(th|se|t)$`, `${1}oo${2}`).
29 + Singular(`(a|e|i|o|u)ys$`, `${1}y`).
30 + Singular(`(m|l)ice$`, `${1}ouse`).
31 + Singular(`(al|ie|l)ves$`, `${1}f`).
32 + Singular(`(l)ies`, `${1}ie`).
33 + Singular(`ies$`, `y`).
34 + Singular(`(d)ie`, `${1}ice`).
35 + Singular(`s$`, ``).
36 + // Irregulars words.
37 + Irregular(`person`, `people`).
38 + Irregular(`child`, `children`).
39 + // Uncountables words.
40 + Uncountable(`fish`).
41 + Uncountable(`sheep`).
42 + Uncountable(`deer`).
43 + Uncountable(`tuna`).
44 + Uncountable(`salmon`).
45 + Uncountable(`trout`).
46 + Uncountable(`music`).
47 + Uncountable(`art`).
48 + Uncountable(`love`).
49 + Uncountable(`happiness`).
50 + Uncountable(`advice`).
51 + Uncountable(`information`).
52 + Uncountable(`news`).
53 + Uncountable(`furniture`).
54 + Uncountable(`luggage`).
55 + Uncountable(`rice`).
56 + Uncountable(`sugar`).
57 + Uncountable(`butter`).
58 + Uncountable(`water`).
59 + Uncountable(`electricity`).
60 + Uncountable(`gas`).
61 + Uncountable(`power`).
62 + Uncountable(`money`).
63 + Uncountable(`currency`).
64 + Uncountable(`scenery`)
Godeps/_workspace/src/github.com/briantigerchow/inflect/types/irregular.go new
+34
@@ -0,0 +1,34 @@
1 +// Package types contains common types useful to the inflect package.
2 +package types
3 +
4 +import "strings"
5 +
6 +// IrregularType provides a structure for irregular words that do not follow standard rules.
7 +type IrregularType struct {
8 + Singular string // The singular form of the irregular word.
9 + Plural string // The plural form of the irregular word.
10 +}
11 +
12 +// IrregularsType defines a slice of pointers to IrregularType.
13 +type IrregularsType []*IrregularType
14 +
15 +// IsIrregular returns an IrregularType and bool if the IrregularsType slice contains the word.
16 +func (self IrregularsType) IsIrregular(str string) (*IrregularType, bool) {
17 + str = strings.ToLower(str)
18 + for _, irregular := range self {
19 + if strings.ToLower(irregular.Singular) == str || strings.ToLower(irregular.Plural) == str {
20 + return irregular, true
21 + }
22 + }
23 +
24 + return nil, false
25 +}
26 +
27 +// Irregular if a factory method to a new IrregularType.
28 +func Irregular(singular, plural string) (irregular *IrregularType) {
29 + irregular = new(IrregularType)
30 + irregular.Singular = singular
31 + irregular.Plural = plural
32 +
33 + return
34 +}
Godeps/_workspace/src/github.com/briantigerchow/inflect/types/language.go new
+80
@@ -0,0 +1,80 @@
1 +// Package types contains common types useful to the inflect package.
2 +package types
3 +
4 +// LanguageType provides a structure for storing inflections rules of a language.
5 +type LanguageType struct {
6 + Short string // The short hand form represention the language, ex. `en` (English).
7 + Pluralizations RulesType // Rules for pluralizing standard words.
8 + Singularizations RulesType // Rules for singularizing standard words.
9 + Irregulars IrregularsType // Slice containing irregular words that do not follow standard rules.
10 + Uncountables UncountablesType // Words that are uncountable, having the same form for both singular and plural.
11 +}
12 +
13 +func convert(str, form string, language *LanguageType, rules RulesType) string {
14 + if language.Uncountables.Contains(str) {
15 + return str
16 + } else if irregular, ok := language.Irregulars.IsIrregular(str); ok {
17 + if form == "singular" {
18 + return irregular.Singular
19 + }
20 + return irregular.Plural
21 + } else {
22 + for _, rule := range rules {
23 + if rule.Regexp.MatchString(str) {
24 + return rule.Regexp.ReplaceAllString(str, rule.Replacer)
25 + }
26 + }
27 + }
28 +
29 + return str
30 +}
31 +
32 +// Pluralize converts the given string to the languages plural form.
33 +func (self *LanguageType) Pluralize(str string) string {
34 + return convert(str, "plural", self, self.Pluralizations)
35 +}
36 +
37 +// Singularize converts the given string to the languages singular form.
38 +func (self *LanguageType) Singularize(str string) string {
39 + return convert(str, "singular", self, self.Singularizations)
40 +}
41 +
42 +// Plural defines a pluralization rule for a language.
43 +func (self *LanguageType) Plural(matcher, replacer string) *LanguageType {
44 + self.Pluralizations = append(self.Pluralizations, Rule(matcher, replacer))
45 +
46 + return self
47 +}
48 +
49 +// Plural defines a singularization rule for a language.
50 +func (self *LanguageType) Singular(matcher, replacer string) *LanguageType {
51 + self.Singularizations = append(self.Singularizations, Rule(matcher, replacer))
52 +
53 + return self
54 +}
55 +
56 +// Plural defines an irregular word for a langauge.
57 +func (self *LanguageType) Irregular(singular, plural string) *LanguageType {
58 + self.Irregulars = append(self.Irregulars, Irregular(singular, plural))
59 +
60 + return self
61 +}
62 +
63 +// Plural defines an uncountable word for a langauge.
64 +func (self *LanguageType) Uncountable(uncountable string) *LanguageType {
65 + self.Uncountables = append(self.Uncountables, uncountable)
66 +
67 + return self
68 +}
69 +
70 +// Language if a factory method to a new LanguageType.
71 +func Language(short string) (language *LanguageType) {
72 + language = new(LanguageType)
73 +
74 + language.Pluralizations = make(RulesType, 0)
75 + language.Singularizations = make(RulesType, 0)
76 + language.Irregulars = make(IrregularsType, 0)
77 + language.Uncountables = make(UncountablesType, 0)
78 +
79 + return
80 +}
Godeps/_workspace/src/github.com/briantigerchow/inflect/types/rule.go new
+25
@@ -0,0 +1,25 @@
1 +// Package types contains common types useful to the inflect package.
2 +package types
3 +
4 +import (
5 + "regexp"
6 +)
7 +
8 +// RuleType provides a structure for pluralization/singularizations rules
9 +// of a language.
10 +type RuleType struct {
11 + Regexp *regexp.Regexp // The regular expression the rule must match.
12 + Replacer string // The replacement to use if the RuleType's Regexp is matched.
13 +}
14 +
15 +// RulesType defines a slice of pointers to RuleType.
16 +type RulesType []*RuleType
17 +
18 +// Rule if a factory method to a new RuleType.
19 +func Rule(matcher, replacer string) (rule *RuleType) {
20 + rule = new(RuleType)
21 + rule.Regexp = regexp.MustCompile(matcher)
22 + rule.Replacer = replacer
23 +
24 + return
25 +}
Godeps/_workspace/src/github.com/briantigerchow/inflect/types/uncountable.go new
+16
@@ -0,0 +1,16 @@
1 +// Package types contains common types useful to the inflect package.
2 +package types
3 +
4 +// UncountablesType is an array of strings
5 +type UncountablesType []string
6 +
7 +// Contains returns a bool if the str is found in the UncountablesType.
8 +func (self UncountablesType) Contains(str string) bool {
9 + for _, word := range self {
10 + if word == str {
11 + return true
12 + }
13 + }
14 +
15 + return false
16 +}