Line 1: Imports Microsoft.VisualBasic
Line 2: Imports System.Web.Script.Serialization
Line 3: Imports System.IO
Line 4: Imports System.Runtime.CompilerServices
Line 5:
Line 6: Public Module ExtensionMethods
Line 7:
Line 8: Private _Words0 As String() = {"", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine "}
Line 9: Private _Words1 As String() = {"ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen "}
Line 10: Private _Words2 As String() = {"twenty ", "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety "}
Line 11: Private _Words3 As String() = {"thousand ", "million ", "billion "}
Line 12:
Line 13: <Extension()>
Line 14: Function TwoStrings(value As XElement) As Tuple(Of String, String)
Line 15: Dim str = value.ToString(SaveOptions.DisableFormatting)
Line 16: Dim arr = str.Split({"#PH#"}, StringSplitOptions.None)
Line 17: Dim tuple = New Tuple(Of String, String)(arr(0), arr(1))
Line 18: Return tuple
Line 19: End Function
Line 20:
Line 21: <Extension()>
Line 22: Function TwoStrings(value As XDocument) As Tuple(Of String, String)
Line 23: Dim str = value.ToString(SaveOptions.DisableFormatting)
Line 24: Dim arr = str.Split({"#PH#"}, StringSplitOptions.None)
Line 25: Dim tuple = New Tuple(Of String, String)(arr(0), arr(1))
Line 26: Return tuple
Line 27: End Function
Line 28:
Line 29: <Extension()>
Line 30: Function TwoStrings(value As String) As Tuple(Of String, String)
Line 31: Dim arr = value.Split({"#PH#"}, StringSplitOptions.None)
Line 32: Dim tuple = New Tuple(Of String, String)(arr(0), arr(1))
Line 33: Return tuple
Line 34: End Function
Line 35:
Line 36: <Extension()> _
Line 37: Public Function ToWords(number As Integer, Optional isUK As Boolean = True) As String
Line 38: If number = 0 Then Return "zero"
Line 39:
Line 40: Dim [and] As String = If(isUK, "and ", "")
Line 41:
Line 42: ' deals with UK or US numbering
Line 43: If number = -2147483648 Then Return (Convert.ToString((Convert.ToString((Convert.ToString("minus two billion one hundred ") & [and]) + "forty seven million four hundred ") & [and]) + "eighty three thousand " + "six hundred ") & [and]) + "forty eight"
Line 44:
Line 45: Dim num As Integer() = New Integer(3) {}
Line 46: Dim first As Integer = 0
Line 47: Dim u As Integer, h As Integer, t As Integer
Line 48: Dim sb As New System.Text.StringBuilder()
Line 49: If number < 0 Then
Line 50: sb.Append("Minus ")
Line 51: number = -number
Line 52: End If
Line 53:
Line 54: num(0) = number Mod 1000
Line 55: ' units
Line 56: num(1) = Math.Floor(number / 1000)
Line 57: num(2) = Math.Floor(number / 1000000)
Line 58: num(1) = Math.Floor(num(1) - 1000 * num(2))
Line 59: ' thousands
Line 60: num(3) = Math.Floor(number / 1000000000)
Line 61: ' billions
Line 62: num(2) = num(2) - 1000 * num(3)
Line 63: ' millions
Line 64: For i As Integer = 3 To 1 Step -1
Line 65: If num(i) <> 0 Then
Line 66: first = i
Line 67: Exit For
Line 68: End If
Line 69: Next
Line 70:
Line 71: For i As Integer = first To 0 Step -1
Line 72: If num(i) = 0 Then
Line 73: Continue For
Line 74: End If
Line 75: u = num(i) Mod 10
Line 76: ' ones
Line 77: t = Math.Floor(num(i) / 10)
Line 78: h = Math.Floor(num(i) / 100)
Line 79: ' hundreds
Line 80: t = t - 10 * h
Line 81: ' tens
Line 82: If h > 0 Then sb.Append(_Words0(h) + "hundred ")
Line 83: If u > 0 OrElse t > 0 Then
Line 84: If h > 0 OrElse i < first Then sb.Append([and])
Line 85: If t = 0 Then
Line 86: sb.Append(_Words0(u))
Line 87: ElseIf t = 1 Then
Line 88: sb.Append(_Words1(u))
Line 89: Else
Line 90: sb.Append(_Words2(t - 2) + _Words0(u))
Line 91: End If
Line 92: End If
Line 93: If i <> 0 Then sb.Append(_Words3(i - 1))
Line 94: Next
Line 95: Return sb.ToString().TrimEnd()
Line 96: End Function
Line 97:
Line 98: <Extension()> _
Line 99: Public Function CapFirst(value As String, Optional isUK As Boolean = True) As String
Line 100: If String.IsNullOrEmpty(value) Then Return value
Line 101: Return Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value.ToLower())
Line 102: End Function
Line 103:
Line 104: End Module
Line 105:
Line 106: Public Module JsonHelper
Line 107:
Line 108: Public Function ToJson(obj As Object) As String
Line 109: Dim serializer As New JavaScriptSerializer()
Line 110: Return serializer.Serialize(obj)
Line 111: End Function
Line 112:
Line 113: Public Function ToJson(obj As Object, recursionDepth As Integer) As String
Line 114: Dim serializer As New JavaScriptSerializer()
Line 115: serializer.RecursionLimit = recursionDepth
Line 116: Return serializer.Serialize(obj)
Line 117: End Function
Line 118:
Line 119: Public Function FromJson(Of T)(json As String) As T
Line 120: Dim serializer As New JavaScriptSerializer()
Line 121: Return serializer.Deserialize(Of T)(json)
Line 122: End Function
Line 123:
Line 124: Public Function FromJson(json As String) As Object
Line 125: Dim serializer As New JavaScriptSerializer()
Line 126: Return serializer.DeserializeObject(json)
Line 127: End Function
Line 128:
Line 129: Public Sub SendToClient(obj As Object, context As HttpContext)
Line 130: Dim json = JsonHelper.ToJson(obj)
Line 131: context.Response.Clear()
Line 132: context.Response.ContentType = "application/json; charset=utf-8"
Line 133: context.Response.Write(json)
Line 134: context.Response.Flush()
Line 135: End Sub
Line 136:
Line 137: End Module
Line 138:
Line 139: Public Module HtmlHelper
Line 140:
Line 141: Public Function Render(control As Control, Optional minimise As Boolean = False) As String
Line 142:
Line 143: Dim sb As New StringBuilder()
Line 144: Dim tw As New StringWriter(sb)
Line 145: Dim hw As New HtmlTextWriter(tw)
Line 146:
Line 147: control.RenderControl(hw)
Line 148: Dim result = sb.ToString().Trim()
Line 149: If minimise Then
Line 150: result = Regex.Replace(result, "[\n\r]+", "")
Line 151: result = Regex.Replace(result, "[\s\t]+", " ")
Line 152: End If
Line 153: Return result
Line 154:
Line 155: End Function
Line 156:
Line 157: End Module
Line 158:
Line 159: Public Module ListHelper
Line 160:
Line 161: <Extension()> _
Line 162: Public Function IsNullOrEmpty(Of T)(enumerable As IEnumerable(Of T)) As Boolean
Line 163: If enumerable Is Nothing Then
Line 164: Return True
Line 165: End If
Line 166: ' If this is a list, use the Count property for efficiency.
Line 167: ' * The Count property is O(1) while IEnumerable.Count() is O(N).
Line 168:
Line 169: Dim collection = TryCast(enumerable, ICollection(Of T))
Line 170: If collection IsNot Nothing Then
Line 171: Return collection.Count < 1
Line 172: End If
Line 173: Return Not enumerable.Any()
Line 174: End Function
Line 175:
Line 176:
Line 177: End Module
|