Category Archives: Brain Dump

Some theories and ideas to be hashed out

C# : How to write a custom Equality Comparer to be used for a HashSet

I need to write a custom Equality Comparer to be used for a class that I created.

Class that will be used for the HashSet

 ///<summary>Item for HashSet example</summary>
public enum ItemGroup
{
    group1 = 1,
    group2,
    group3,
    group4
}
public class Item
{
    public string Name { get; set; }
    public ItemGroup Group { get; set; }

    public Item(string name, ItemGroup group)
    {
        Name = name;
        Group = group;
    }
}
///<summary>Custom Equality Comparer using the base class EqualityComparer<T></summary>
public class MyItemEqualityComparer : EqualityComparer<Item>
{
    //make this a singleton object
    private static readonly MyItemEqualityComparer _instance = new MyItemEqualityComparer();

    public static MyItemEqualityComparer Instance { get { return _instance; } }

    private MyItemEqualityComparer() { }

    public override bool Equals(Item x, Item y)
    {
        return x.Name.ToUpperInvariant() == y.Name.ToUpperInvariant() && x.Group == y.Group;
    }
    public override int GetHashCode(Item obj)
    {
        return obj.Name.ToUpperInvariant().GetHashCode() ^ obj.Group.GetHashCode();
    }
}

Using

class Program
{
    static void Main(string[] args)
    {
        var items = new HashSet<Item>(MyItemEqualityComparer.Instance);
        items.Add(new Item("Item 1", ItemGroup.group1));
        items.Add(new Item("Item 2", ItemGroup.group2));
        items.Add(new Item("ITEM 3", ItemGroup.group3));
        items.Add(new Item("ITEM 3", ItemGroup.group3));
        items.Add(new Item("Item 3", ItemGroup.group3));

        foreach (Item item in items)
        {
            Console.WriteLine($"Item's name : {item.Name}, Item's Group : {item.Group.ToString()}");

        }
    }
}

Outputs

  • Item’s name : Item 1, Item’s Group : group1
  • Item’s name : Item 2, Item’s Group : group2
  • Item’s name : ITEM 3, Item’s Group : group3

C# : How do you keep a list with UNIQUE values only?

Probelm

Given code :

var items = new List<string>();
items.Add("string1");
items.Add("string2");
items.Add("string3");
items.Add("string1");

foreach(var item in items){
    Console.WriteLine(item);
}

The output will show

string1
string2
string3
string1

Answer

Use a HashSet Example:

var items = new HashSet<string>();
items.Add("string1");
items.Add("string2");
items.Add("string3");
items.Add("string1");

foreach(var item in items){
    Console.WriteLine(item);
}

The output will show

string1
string2
string3

Gotchas

When overwriting object’s Equal method in your class you have added to the HashSet. HashSet calls the Equals method in which is calling your overridden Equals method. If you are comparing a string, strings are compared with Ordinal Case Sensitive string comparison by default. Make sure you are comparing your object’s fields properly and safely for situations such as this.

Upload Images to Amazon S3 Server using Node and Express.js

The code below will walk you through the process to get a working example created for uploading an image file to your Amazon S3 Server. You should already have a bucket created and have your key and secret on hand. We will add those strings to a config file. Once your done you will be able to upload an image file to your Amazon S3 server and verify the image was uploaded by refreshing your S3 Admin portal or view the image from your returned code.

Get the code from Github : https://github.com/wesleyduff/AmazonS3FileUpload

Continue reading

Use of !! in JavaScript

BANG BANG

Want to find out if something is false if it is a string, number, obj etc….


var x = “No”
typeof x
// string

var xBools = !!x;
//true – because variable x has a string of No, could be a string of false as well. A string is an object and that object has alpha characters
typeof xBools
// boolean

var emptyString = “”;
typeof emptyString 
// string

var emptyStringBools = !!emptyString;
//false – because the string is empty
typeof emptyStringBools 
// Boolean

var nulls = null;
typeof nulls
// object

var nullsBools = !!nulls;
//false
typeof nullsBools
// boolean

var under;
typeof under
// undefined

var underBools = !!under;
//false
typeof underBools
// boolean

Check to see if a variable has a value and if that value is usable.

How To: Submit Your Form Data as JSON data to be Consumed by a RESTful Service (Node.js & Express.js)

I need to obtain a JSON object that cantains all of my form data as key value pairs {name : value}.

Answer :

  • Use jQuery SerializeArray
    • This will not give you what you need right off. It will send back something like this
    • [{name: 'key', value = 'value'}]
  • Use the result from SerializeArray and run it through the custom SerializedArrayToJSON method described below
    • Pass the serialized array as a parameter to the SerializedArrayToJSON method.
      • The SerializedArrayToJSON will return a formatted javaScript object you can pass in your POST method.
      • Inside your route the req.body will contain each piece of your form data as ‘key : value’ pairs.

Example :


 var serializedArrayData= $('form').serializeArray();

 function SerializedArrayToJSON (serializedArrayData){
   //reformat formData
   var holder = [], returnData = {};

   for(var i = 0; i < serializedArrayData.length; i++){
    for(_key in serializedArrayData[i]){
       if(serializedArrayData[i].hasOwnProperty(_key)){
          holder.push(serializedArrayData[i][_key]);
       }
     }
   }
   for(var j = 0; j < holder.length; j++){
     if(j % 2 == 0 ){
       returnData[""+holder[j]+""] = holder[j + 1];
     }
   }

   return returnData;
};

HOW TO: Incorporate an Email Service in your Node.js App with Nodemailer + SendGrid

In this tutorial we will use the github code provided from a previous post to get our Node + Express + Jade app up and running. Then we will talk about how to send emails using nodemailer and SendGrid.

Find the finished code on github here

Continue reading

HOW TO: Setup a Node.js Server using Express.js and Jade

In this tutorial I will cover the steps needed to get started with Node.js and Express.js from the ground up. Express does come with a generator but I do not go into generated Express apps. This tutorial is a bare bones example of getting Node, Express and Jade to work together to display a home page.

Continue reading