Add a lot of functions to Base
This commit is contained in:
101
Base/ModuleExtensions.vb
Normal file
101
Base/ModuleExtensions.vb
Normal file
@@ -0,0 +1,101 @@
|
||||
Imports System.Runtime.CompilerServices
|
||||
|
||||
Public Module ModuleExtensions
|
||||
Const UnixEraStartTicks As Long = 621355968000000000
|
||||
|
||||
' ======================================================
|
||||
' === DATETIME
|
||||
' ======================================================
|
||||
|
||||
<Extension()>
|
||||
Public Function GetUnixTimestamp(pDate As Date) As Long
|
||||
Dim UnixEraTicks = pDate.Ticks - UnixEraStartTicks
|
||||
Return UnixEraTicks \ 10000
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function DateFromUnix(pTimestamp As Long) As Date
|
||||
Return New Date(UnixEraStartTicks + pTimestamp * 10000)
|
||||
End Function
|
||||
|
||||
' ======================================================
|
||||
' === STRING
|
||||
' ======================================================
|
||||
|
||||
''' <summary>
|
||||
''' Truncates a string to the specified length if it exceeds that length.
|
||||
''' </summary>
|
||||
''' <param name="pString">The string</param>
|
||||
''' <param name="pLength">The maximum string length</param>
|
||||
''' <returns>The truncated string</returns>
|
||||
<Extension()>
|
||||
Public Function Truncate(pString As String, pLength As Integer) As String
|
||||
If String.IsNullOrEmpty(pString) Then Return pString
|
||||
Return pString.Substring(0, Math.Min(pLength, pString.Length))
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Replaces single quotes in text for SQL Commands.
|
||||
''' </summary>
|
||||
''' <param name="pString">The string</param>
|
||||
''' <returns>The escaped string.</returns>
|
||||
<Extension()>
|
||||
Public Function EscapeForSQL(pString As String) As String
|
||||
Return ObjectEx.NotNull(pString, String.Empty).Replace("'", "''")
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Converts a string to boolean. Accepts true and 1 as truthy values
|
||||
''' </summary>
|
||||
''' <param name="pString">The input string</param>
|
||||
''' <returns>True if input is true or 1, otherwise false.</returns>
|
||||
<Extension()>
|
||||
Public Function ToBoolean(pString As String) As Boolean
|
||||
If String.IsNullOrEmpty(pString) Then Return False
|
||||
Return (pString.Trim().ToLower() = "true") OrElse (pString.Trim() = "1")
|
||||
End Function
|
||||
|
||||
' ======================================================
|
||||
' === DATATABLE
|
||||
' ======================================================
|
||||
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldName), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function ItemEx(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T
|
||||
Try
|
||||
Return ObjectEx.NotNull(pRow.Item(pFieldIndex), pDefaultValue)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function FieldOrDefault(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T
|
||||
Return ItemEx(pRow, pFieldName, pDefaultValue)
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function FieldOrDefault(Of T)(pRow As DataRow, pFieldIndex As Integer, Optional pDefaultValue As T = Nothing) As T
|
||||
Return ItemEx(pRow, pFieldIndex, pDefaultValue)
|
||||
End Function
|
||||
|
||||
<Extension()>
|
||||
Public Function First(pTable As DataTable) As DataRow
|
||||
Try
|
||||
If pTable Is Nothing OrElse pTable.Rows.Count = 0 Then
|
||||
Return Nothing
|
||||
End If
|
||||
Return pTable.Rows.Item(0)
|
||||
Catch ex As Exception
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
End Module
|
||||
Reference in New Issue
Block a user