1 namespace Generator
.Extentions
4 import System
.Text
.RegularExpressions
6 # String class extention
7 def ToCapital(self as string
) as string
:
8 return self[:1].ToUpper() + self[1:].ToLower()
9 def ToTitle(self as string
) as string
:
10 return join(word
.ToCapital() for word
in self.Split(char(' ')))
11 def ToCamel(self as string
) as string
:
12 name
= Regex
.Replace(self, "(_|\\s)([a-z])", { m
as Match |
return m
.Groups
[2].ToString().ToUpper() })
13 return name
[:1].ToUpper() + name
[1:]
14 def ToClassName(self as string
) as string
:
16 def ToVarName(self as string
) as string
:
17 name
= self.ToCapital()
18 return name
[:1].ToLower() + name
[1:]
19 def ToHumanName(self as string
) as string
:
20 return /[A
-Z
]/.Replace(self, ' $0').Trim().ToCapital()
21 def ToPlural(self as string
) as string
:
22 return Inflector
.Instance
.ToPlural(self)
23 def ToSingular(self as string
) as string
:
24 return Inflector
.Instance
.ToSingular(self)
25 def ToFileName(self as string
) as string
:
26 # All lower case for compatibility
27 return /_
/.Replace(self, '').Trim().ToLower()
28 def ToPath(self as string
) as string
:
29 sep
= Path
.DirectorySeparatorChar
.ToString()
30 return self.Replace("/", sep
).Replace("\\", sep
)
32 # Integer class extention
33 def Times(self as int
, c
as callable(int
)):
36 def ToOrdinal(self as int
):
37 # Adapted from http://api.rubyonrails.com/classes/Inflector.html#M000814
38 if self % 100 in range(11, 14):
51 def Each(self as System
.Collections
.IEnumerable
, action
as callable(object
)):
55 # Date and time extentions
56 def Ago(self as System
.TimeSpan
) as System
.DateTime
:
57 return System
.DateTime
.Now
.Subtract(self)
59 def FromNow(self as System
.TimeSpan
) as System
.DateTime
:
60 return System
.DateTime
.Now
.Add(self)