Skip to content

Instantly share code, notes, and snippets.

View DForshner's full-sized avatar

David Forshner DForshner

View GitHub Profile
@DForshner
DForshner / SubscribeToAmplifyAndReturnAPromise.js
Created April 25, 2014 19:07
Subscribe to data from amplifyJS returning a promise.
/**
* Subscribe to data from amplifyJS returning a promise.
* @param {string} storeKey key that amplify stores data under.
* @param {string} publishKey key that amplify publishes data to.
* @returns {Promise} On success the promise will return the drop down data.
*/
subscribe: function(storeKey, publishKey) {
var deferred = $.Deferred();
var publishCallback = function () {
amplify.unsubscribe(publishKey, publishCallback);
@DForshner
DForshner / EnumerateEnumNames.cs
Created April 18, 2014 21:31
Enumerate enum names.
public enum Animals
{
Cat,
Dog,
Goat,
}
var animalNames = ((Animals[])Enum.GetValues(typeof(Animals)))
.Select(key => Enum.GetName(typeof(Animals), key));
@DForshner
DForshner / EmptyReadOnlyDictionary.cs
Created March 24, 2014 20:55
Returns a cached version of an empty dictionary so we can avoid the overhead of creating a new dictionary each time.
public static class ReadOnlyDictionaryHelper
{
/// <summary>
/// Returns a cached version of an empty dictionary so we can avoid the overhead of creating a new dictionary each time.
/// </summary>
internal class EmptyReadOnlyDictionary<TKey, TValue>
{
static volatile ReadOnlyDictionary<TKey, TValue> instance;
public static IReadOnlyDictionary<TKey, TValue> Instance
{
// OrderBy
Func<IQueryable<Foo>, IOrderedQueryable<Foo>> test = q => q.OrderBy(x => x.Prop1);
private static Dictionary<string, Func<IQueryable<Foo>, IOrderedQueryable<Foo>>> orderByLookup
= new Dictionary<string, Func<IQueryable<Foo>, IOrderedQueryable<Foo>>>()
{
{ "PropA", q => q.OrderBy(x => x.PropA)},
{ "PropB", q => q.OrderByDescending(x => x.PropB)},
{ "PropC", q => q.OrderByDescending(x => x.PropC)},
};
@DForshner
DForshner / SqlDataReaderHelper.cs
Last active August 29, 2015 13:56
Helper class that executes a stored procedure using an SQL data reader and maps the results to a list of objects.
public class SqlDataReaderHelper<T> where T : class
{
private const int TIMEOUT = 600;
private readonly string sqlCommandText;
private readonly string sqlConnectionString;
private IList<SqlParameter> parameters = new List<SqlParameter>();
private Func<SqlDataReader, T> mapper;
public SqlDataReaderHelper(string sqlCommandText, string sqlConnectionString, Func<SqlDataReader, T> mapper)
@DForshner
DForshner / BackboneBaseViewVsMixin.js
Last active August 29, 2015 13:56
Psuedo code showing the difference between a Backbone base view vs. a mix-in.
// Base View - Used to add "Is a" type functionality that is used by most/all derived views.
(function (Backbone, _) {
App.views.BaseView = Backbone.View.extend({
initialize: function () { },
//...
});
})(Backbone, _);
// Mixin - Used to add shared functionality with limited context to objects.
(function () {
@DForshner
DForshner / Regex.js
Created February 27, 2014 17:05
Playing around with regular expressions in JavaScript
var url1 = "https://example.com/".match(/^https?\:\/\/[^ ]*/);
console.log("Url1: ", url1[0]);
var url2 = "http://example.com/".match(/^https?\:\/\/[^ ]*/);
console.log("Url2: ", url2[0]);
var doubleEntry = "This a double double".match(/(\w+) \1/);
console.log("Find double entry: ", doubleEntry[1]);
var username = 'user3';
var userData = "user1=sad; user2=angry; user3=happy; user4=crazy"
@DForshner
DForshner / SerializedListRepresentationConverter.cs
Last active August 29, 2015 13:56
Using Regex to change how a serialized sequences of items are represented.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text.RegularExpressions;
namespace Infrastructure.Tests
{
/// <summary>
/// Converts how lists of elements are represented in a serialized string.
/// </summary>
public class SerializedListRepresentationConverter
@DForshner
DForshner / SQL Server Reporting Services (SSRS) Report Expressions.txt
Last active August 29, 2015 13:55
A collection of SQL Server Reporting Services (SSRS) report expressions.
Some SQL Server Reporting Services Expressions.
- Change tablex cell's border style depending on line field. (Cell's property window)
=iif(Fields!Type.Value = 2,"Dotted","None")
- Change tablex cell's font based on line field. (Cell's property window)
=iif(Fields!Type.Value = 2, "Bold", "Default")
- Display a specific row's field value
=Round(Lookup("SpecialTotal", Fields!RowTitle.Value, Fields!Total.Value, "MyDS"))
@DForshner
DForshner / ForInObjectProperties.js
Created January 29, 2014 22:29
Filtering out prototype properties when iterating over an object's properties.
// Display properties
function Display(id, props) {
var frag = document.createDocumentFragment();
var list = document.getElementById(id);
console.log("============", id);
// Add properties to fragment
for (var i = 0; props[i]; i++) {
console.log(props[i]);
var el = document.createElement("li");