Skip to content

Instantly share code, notes, and snippets.

@ssajous
ssajous / Dice.cs
Last active October 14, 2021 15:41
Implementations of Dice's Coefficient used to get a similarity index between two strings.
public static double DiceCoefficient(string stOne, string stTwo)
{
HashSet<string> nx = BuildBigramSet(stOne);
HashSet<string> ny = BuildBigramSet(stTwo);
HashSet<string> intersection = new HashSet<string>(nx);
intersection.IntersectWith(ny);
double dbOne = intersection.Count;
return (2 * dbOne) / (nx.Count + ny.Count);
@ssajous
ssajous / KOMultiselectBinding.js
Last active June 13, 2016 21:37
Knockout.js binding handler for the JQuery UI multiselect widget
ko.bindingHandlers.multiselect = {
init: function (element, valueAccessor, allBindingAccessors) {
"use strict";
var options = valueAccessor();
ko.bindingHandlers.options.update(
element,
function() { return options.options; },
allBindingAccessors
@ssajous
ssajous / Error Validation - CustomGlobalConfig.cs
Last active December 19, 2015 16:59
Validation Filter to send a JSON response with validation errors when an HTTP Request contains an invalid data model. Uses JSON.net.
using Newtonsoft.Json.Serialization;
using System.Web.Http;
using System.Web.Http.Validation.Providers;
using MyApplication.Web.Filters;
namespace MyApplication.Web
{
public static class CustomGlobalConfig
{
public static void Customize(HttpConfiguration config)
@ssajous
ssajous / validationUtility.js
Created July 15, 2013 14:26
Validation utility to be used with Twitter Bootstrap. Displays tooltip on the first invalid element in the form, as well as highlights all invalid elements.
var ValidationUtility = function() {
var validationElements = $('[data-role="validate"]');
var elementCount;
validationElements.popover({
placement: 'top'
});
validationElements.on('invalid', function() {
if (elementCount === 0) {
@ssajous
ssajous / GenericRepository.cs
Last active December 19, 2015 22:48
Entity Framework generic repository. Meant to be used as a wrapper to access a particular type/table from DbContext
public class GenericRepository<T> : IRepository<T> where T : class
{
protected DbSet<T> DBSet {get;set;}
protected DbContext Context { get; set; }
public GenericRepository(DbContext context)
{
if (context == null)
{
throw new ArgumentException("An instance of DbContext is " +
@ssajous
ssajous / bootstrapShowModal.js
Last active April 12, 2018 13:39
Knockout.js binding handle to hide and show bootstrap modal dialog. jsFiddle example http://jsfiddle.net/shub2079/TAtvU/
// Custom binding for modal dialog
// Bind a Bootstrap modal div to an observable
// boolean. When the observable changes, the
// modal window's visibility will also change.
//
// The binding is two-way, so changes to the
// modal window's visibility will also change
// the bound observable's value
ko.bindingHandlers.bootstrapShowModal = {
init: function (element, valueAccessor) {
@ssajous
ssajous / DelimStringToIntTable.sql
Created July 26, 2013 15:37
Takes a string of comma delmited integer values and splits them into a table which can be joined into other queries.
CREATE FUNCTION [dbo].[DelimStringToIntTable](@input AS VARCHAR(MAX))
RETURNS
@Result TABLE(ID BIGINT)
AS
BEGIN
DECLARE @str VARCHAR(20)
DECLARE @ind Int
IF(@input is not null)
BEGIN
@ssajous
ssajous / IStringSerializer.cs
Created August 23, 2013 03:17
Components to generate secure tokens to be created by a secure web service to use for authentication of subsequent calls. It essentially operates on the same premise of a remember me cookie in forms authentication. The token expiration should use configured values instead of hard coding... This example uses JSON .NET to serialize objects into js…
public interface IStringSerializer
{
T Deserialize<T>(string item);
string Serialize(object item);
}
@ssajous
ssajous / FB.html
Last active December 21, 2015 14:58
<!--
Import the Facebook API javascript. FB examples load this dynamically through jquery, but this avoids
any ajax caching issues.
-->
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
@ssajous
ssajous / AzureMobileServiceGenericCRUD.js
Last active December 25, 2015 08:29
Javascript generic REST client to Azure Mobile Services for basic CRUD operations.
var httpVerbs = {
POST: 'POST',
PUT: 'PUT',
PATCH: 'PATCH',
GET: 'GET',
DEL: 'DELETE'
};
var constants = {
// Should be your application specific key for Azure