$.fn.display = function(show) {
return this.each(function() {
(show) ? $(this).show() : $(this).hide();
});
};
Monday, May 18, 2009
jQuery display
Labels:
jquery
jQuery toggleClass
You can extend jQuery to swap classes with a
So you can do stuff like this:
toggleClass
as shown below:
//extend jquery
(function($) {
$.fn.toggleClass = function(check, replace) {
return this.each(function() {
if ($(this).hasClass(check)) {
$(this).removeClass(check);
$(this).addClass(replace);
} else {
if ($(this).hasClass(replace))
$(this).removeClass(replace);
$(this).addClass(check);
}
});
};
})(jQuery);
So you can do stuff like this:
<input type="button" value="Toggle"
onclick="$('#myDiv').toggleClass('expanded','collapsed')"/>
Friday, May 15, 2009
C# Generic Extension Methods
Just found this interesting generic extension method "Is<T>" on hardcodet.net.
So you can do stuff like this:
/// <summary>
/// Checks a list of candidates for equality to a given
/// reference value.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value">The evaluated value.</param>
/// <param name="candidates">A list of possible values that are
/// regarded valid.</param>
/// <returns>True if one of the submitted <paramref name="candidates"/>
/// matches the evaluated value. If the <paramref name="candidates"/>
/// parameter itself is null, too, the method returns false as well,
/// which allows to check with null values, too.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="candidates"/>
/// is a null reference.</exception>
public static bool Is<T>(this T value, params T[] candidates)
{
if (candidates == null) return false;
foreach (var t in candidates)
{
if (value.Equals(t)) return true;
}
return false;
}
So you can do stuff like this:
string status = Order.StatusCode;
if(status.Is("UnAssigned","Cancelled","Suspended"))
{
//Do Something
}
Wednesday, May 13, 2009
C# String Extensions
public static class StringExtensions
{
#region - Substring Methods -
/// <summary>
/// Gets the substring after the first occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// A null separator will return the empty string if the input string is not null.
/// </remarks>
/// <example>
/// StringExtensions.SubstringAfter(null, "*") = null;
/// StringExtensions.SubstringAfter("", "*") = "";
/// StringExtensions.SubstringAfter("*", null) = "";
/// StringExtensions.SubstringAfter("abc", "a") = "bc";
/// StringExtensions.SubstringAfter("abcba", "b") = "cba";
/// StringExtensions.SubstringAfter("abc", "c") = "";
/// StringExtensions.SubstringAfter("abc", "d") = "";
/// StringExtensions.SubstringAfter("abc", "") = "abc";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring after the first occurrence of the separator, null if null String input</returns>
public static string SubstringAfter(this string str, string separator)
{
if (string.IsNullOrEmpty(str))
return str;
if (separator == null)
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.IndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return string.Empty;
}
return str.Substring(index + separator.Length);
}
/// <summary>
/// Gets the substring after the last occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// An empty or null separator will return the empty string if the input string is not null.
/// </remarks>
/// <example>
/// StringExtensions.SubstringAfterLast(null, "*") = null;
/// StringExtensions.SubstringAfterLast("", "*") = "";
/// StringExtensions.SubstringAfterLast("*", "") = "";
/// StringExtensions.SubstringAfterLast("*", null) = "";
/// StringExtensions.SubstringAfterLast("abc", "a") = "bc";
/// StringExtensions.SubstringAfterLast("abcba", "b") = "a";
/// StringExtensions.SubstringAfterLast("abc", "c") = "";
/// StringExtensions.SubstringAfterLast("a", "a") = "";
/// StringExtensions.SubstringAfterLast("a", "z") = "";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring after the last occurrence of the separator, null if null String input</returns>
public static string SubstringAfterLast(this string str, string separator)
{
if (string.IsNullOrEmpty(str))
return str;
if (string.IsNullOrEmpty(separator))
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.LastIndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return string.Empty;
}
return str.Substring(index + separator.Length);
}
/// <summary>
/// Gets the substring before the first occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// A null separator will return the input string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBefore(null, "*") = null;
/// StringExtensions.SubstringBefore("", "*") = "";
/// StringExtensions.SubstringBefore("abc", "a") , "";
/// StringExtensions.SubstringBefore("abcba", "b") , "a";
/// StringExtensions.SubstringBefore("abc", "c") , "ab";
/// StringExtensions.SubstringBefore("abc", "d") , "abc";
/// StringExtensions.SubstringBefore("abc", "") , "";
/// StringExtensions.SubstringBefore("abc", null) , "abc";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null </param>
/// <returns>the substring before the first occurrence of the separator, null if null String input</returns>
public static string SubstringBefore(this string str, string separator)
{
if (string.IsNullOrEmpty(str) || separator == null)
return str;
if (separator == string.Empty)
return string.Empty;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.IndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return str;
}
return str.Substring(0, index);
}
/// <summary>
/// Gets the substring before the last occurrence of a separator. The separator is not returned.
/// </summary>
/// <remarks>
/// A null string input will return null. An empty ("") string input will return the empty string.
/// An empty or null separator will return the input string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBeforeLast(null, "*") = null;
/// StringExtensions.SubstringBeforeLast("", "*") = "";
/// StringExtensions.SubstringBeforeLast("abcba", "b") = "abc";
/// StringExtensions.SubstringBeforeLast("abc", "c") = "ab";
/// StringExtensions.SubstringBeforeLast("a", "a") = "";
/// StringExtensions.SubstringBeforeLast("a", "z") = "a";
/// StringExtensions.SubstringBeforeLast("a", null) = "a";
/// StringExtensions.SubstringBeforeLast("a", "") = "a";
/// </example>
/// <param name="str">the String to get a substring from, may be null</param>
/// <param name="separator">the String to search for, may be null</param>
/// <returns>the substring before the last occurrence of the separator, null if null String input</returns>
public static string SubstringBeforeLast(this string str, string separator)
{
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(separator))
return str;
CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo;
int index = compareInfo.LastIndexOf(str, separator, CompareOptions.Ordinal);
if (index < 0)
{
//No such substring
return str;
}
return str.Substring(0, index);
}
/// <summary>
/// Gets the String that is nested in between two instances of the same String.
/// </summary>
/// <remarks>A null input String returns null. A null tag returns null.</remarks>
/// <example>
/// StringExtensions.SubstringBetween(null, "*") = null;
/// StringExtensions.SubstringBetween("", "") = "";
/// StringExtensions.SubstringBetween("", "tag") = null;
/// StringExtensions.SubstringBetween("tagabctag", null) = null;
/// StringExtensions.SubstringBetween("tagabctag", "") = "";
/// StringExtensions.SubstringBetween("tagabctag", "tag") = "abc";
/// </example>
/// <param name="str">the String containing the substring, may be null</param>
/// <param name="tag">the String before and after the substring, may be null </param>
/// <returns>the substring, null if no match</returns>
public static string SubstringBetween(this string str, string tag)
{
return SubstringBetween(str, tag, tag);
}
/// <summary>
/// Gets the String that is nested in between two Strings. Only the first match is returned.
/// </summary>
/// <remarks>
/// A null input String returns null. A null open/close returns null (no match).
/// An empty ("") open and close returns an empty string.
/// </remarks>
/// <example>
/// StringExtensions.SubstringBetween("wx[b]yz", "[", "]") = "b";
/// StringExtensions.SubstringBetween(null, "*", "*") = null;
/// StringExtensions.SubstringBetween("*", null, "*") = null;
/// StringExtensions.SubstringBetween("*", "*", null) = null;
/// StringExtensions.SubstringBetween("", "", "") = "";
/// StringExtensions.SubstringBetween("", "", "]") = null;
/// StringExtensions.SubstringBetween("", "[", "]") = null;
/// StringExtensions.SubstringBetween("yabcz", "", "") = "";
/// StringExtensions.SubstringBetween("yabcz", "y", "z") = "abc";
/// StringExtensions.SubstringBetween("yabczyabcz", "y", "z") = "abc";
/// </example>
/// <param name="str">the String containing the substring, may be null</param>
/// <param name="open">the String before the substring, may be null</param>
/// <param name="close">the String after the substring, may be null </param>
/// <returns>the substring, null if no match</returns>
public static string SubstringBetween(this string str, string open, string close)
{
if (str == null || open == null || close == null)
return null;
string result = SubstringBefore(SubstringAfter(str, open), close);
if (result == string.Empty && str == string.Empty && (open != string.Empty || close != string.Empty))
return null;
return result;
}
/// <summary>
/// Searches a String for substrings delimited by a start and end tag,
/// returning all matching substrings in an array.
/// </summary>
/// <remarks>
/// A null input String returns null. A null open/close returns null (no match).
/// An empty ("") open/close returns null (no match).
/// </remarks>
/// <example>
/// StringExtensions.SubstringsBetween("[a][b][c]", "[", "]") = [ "a", "b", "c" ];
/// StringExtensions.SubstringsBetween(null, "*", "*") = null;
/// StringExtensions.SubstringsBetween("*", null, "*") = null;
/// StringExtensions.SubstringsBetween("*", "*", null) = null;
/// StringExtensions.SubstringsBetween("", "[", "]") = []);
/// </example>
/// <param name="str">the String containing the substrings, null returns null, empty returns empty</param>
/// <param name="open">the String identifying the start of the substring, empty returns null</param>
/// <param name="close">the String identifying the end of the substring, empty returns null </param>
/// <returns>a String Array of substrings, or null if no match</returns>
public static string[] SubstringsBetween(this string str, string open, string close)
{
if (str == null || string.IsNullOrEmpty(open) || string.IsNullOrEmpty(close))
{
return null;
}
int strLen = str.Length;
if (strLen == 0)
{
return new string[]{};
}
int closeLen = close.Length;
int openLen = open.Length;
List<string> list = new List<string>();
int pos = 0;
while (pos < (strLen - closeLen))
{
int start = str.IndexOf(open, pos);
if (start < 0)
{
break;
}
start += openLen;
int end = str.IndexOf(close, start);
if (end < 0)
{
break;
}
list.Add(str.Substring(start, end - start));
pos = end + closeLen;
}
if (list.Count == 0)
{
return null;
}
return list.ToArray();
}
/// <summary>
/// Repeats the specified string n times.
/// </summary>
/// <param name="instr">The input string.</param>
/// <param name="n">The number of times input string should be repeated.</param>
/// <returns></returns>
public static string Repeat(this string str, int n)
{
if (string.IsNullOrEmpty(str))
return str;
var result = new StringBuilder(str.Length * n);
return result.Insert(0, str, n).ToString();
}
#endregion
#region - Boolean Check Methods -
/// <summary>
/// Checks if the String can be converted into a numeric value.
/// </summary>
/// <remarks>null or an empty string will return false.</remarks>
/// <param name="str">the String to check, may be null </param>
/// <returns>true if it can be parsed into a numeric (decimal) value</returns>
public static bool IsNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
return false;
decimal tmp;
return decimal.TryParse(str, out tmp);
}
/// <summary>
/// Checks if the String contains only unicode digits.
/// A decimal point is not a unicode digit and returns false.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null </param>
/// <returns>true if only contains digits, and is non-null</returns>
public static bool IsDigit(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsDigit(str, i) == false)
return false;
}
return true;
}
/// <summary>
/// Checks if the String contains only unicode letters.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null</param>
/// <returns>true if only contains letters, and is non-null</returns>
public static bool IsAlpha(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsLetter(str, i) == false)
return false;
}
return true;
}
/// <summary>
/// Checks if the String contains only unicode letters or digits.
/// </summary>
/// <remarks>null will return false. An empty String ("") will return true.</remarks>
/// <param name="str">the String to check, may be null</param>
/// <returns>true if only contains letters or digits, and is non-null</returns>
public static bool IsAlphaNumeric(this string str)
{
if (str == null)
return false;
int sz = str.Length;
for (int i = 0; i < sz; i++)
{
if (Char.IsLetterOrDigit(str, i) == false)
return false;
}
return true;
}
#endregion
}
Labels:
C#,
string extensions
Thursday, May 7, 2009
SQL Server - Get Table Schema
USE [Northwind]
GO
/****** Object: StoredProcedure [dbo].[GetTableSchema] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GetTableSchema]
(
@table varchar(50)
)
AS
SELECT
c.table_name As TableName,
c.column_name As ColumnName,
c.data_type As DataType,
c.character_maximum_length As MaxLength,
COALESCE (
( SELECT
CASE cu.column_name
WHEN null THEN 0
ELSE 1
END
FROM information_schema.constraint_column_usage cu
INNER join information_schema.table_constraints ct
ON ct.constraint_name = cu.constraint_name
WHERE
ct.constraint_type = 'PRIMARY KEY'
AND ct.table_name = c.table_name
AND cu.column_name = c.column_name
),0) AS IsPrimaryKey
FROM information_schema.columns c
INNER JOIN information_schema.tables t
ON c.table_name = t.table_name
WHERE @table = t.table_name and
(t.table_type = 'BASE TABLE' and not
(t.table_name = 'dtproperties') and not
(t.table_name = 'sysdiagrams'))
ORDER BY c.table_name, c.ordinal_position
Labels:
Schema,
SQL Server
Subscribe to:
Posts (Atom)