Skip to content

Instantly share code, notes, and snippets.

View sebnilsson's full-sized avatar
☁️

Seb Nilsson sebnilsson

☁️
View GitHub Profile
@sebnilsson
sebnilsson / update-jquery-on-the-fly.js
Last active December 9, 2015 19:29
Update jQuery on-the-fly in Javascript
console.log($().jquery); // Show original jQuery-version
// Closure
var $gs = $.getScript;
// http://api.jquery.com/jQuery.noConflict/
// jQuery.noConflict( [removeAll] )
// removeAll: A Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).
$.noConflict(true);
@sebnilsson
sebnilsson / FileSizeHelper.cs
Last active December 11, 2015 04:38
Get human-readable file-sizes
public class FileSizeHelper
{
private static readonly string[] Units = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private static string GetReadableFileSize(long size) // Size in bytes
{
int unitIndex = 0;
while (size >= 1024)
{
size /= 1024;
public DateTime? TryGetAssemblyCreationDate(Assembly assembly)
{
try
{
return System.IO.File.GetCreationTime(assembly.Location);
}
catch
{
return null;
}
@sebnilsson
sebnilsson / TagPrefix-Error
Last active December 13, 2015 17:58
User Controls vs. Custom Controls - Web.config - system.web.pages.controls
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Invalid or missing attributes found in the tagPrefix entry. For user control, you must also specify 'tagName' and 'src'. For custom control, you must also specify 'namespace', and optionally 'assembly'.
@sebnilsson
sebnilsson / ActionStartsWithGetConstraint.cs
Created March 8, 2013 12:19
RouteConstraint for actions that starts with "Get"
public class ActionStartsWithGetConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var action = values["action"];
if (action == null)
{
return false;
}
@sebnilsson
sebnilsson / Distinct.cs
Last active December 15, 2015 02:18
Distinct enumerable through LINQ-group by
public static IEnumerable<TSource> Distinct<TSource>(
this IEnumerable<TSource> source, Func<TSource, object> predicate)
{
return from item in source.GroupBy(predicate) select item.First();
}
@sebnilsson
sebnilsson / cookies.js
Created March 20, 2013 14:34
Get/set cookies
var cookies = {
setItem: function(name, value, days) {
var date = new Date();
date.setDate(date.getDate() + days);
var cookieValue = escape(value) + ((days === null) ? '' : '; expires=' + date.toUTCString());
document.cookie = name + '=' + cookieValue;
},
getItem: function(name) {
var i, key, value, cookieList = document.cookie.split(';'), item, splitIndex;
for (i = 0; i < cookieList.length; i++) {
@sebnilsson
sebnilsson / ObjectExpandoObjectExtensions.cs
Last active November 16, 2017 11:52
Convert anonymous type to dynamic
public static ExpandoObject ToExpandoObject(this object obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
IDictionary<string, object> expando = new ExpandoObject();
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(obj.GetType()))
expando.Add(property.Name, property.GetValue(obj));
@sebnilsson
sebnilsson / DateTimeExtensions.cs
Created April 3, 2013 07:55
Format C# UTC DateTime to Local Format in JavaScript with Moment.js
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static double GetEpochTicks(this DateTime dateTime)
{
return dateTime.Subtract(Epoch).TotalMilliseconds;
}
@sebnilsson
sebnilsson / PadLeft.js
Created April 5, 2013 14:25
Add padLeft to string prototype
String.prototype.padLeft = function (len, c) {
var s = this;
c = c || '0';
while (s.length < len) {
s = c + s;
}
return s;
};