Skip to content

Instantly share code, notes, and snippets.

@chadedrupt
chadedrupt / EnumerationExtensions.cs
Created August 3, 2012 02:12
Enum Flag Extensions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Enum.Extensions
{
public static class EnumerationExtensions
{
@chadedrupt
chadedrupt / gist:3291477
Created August 8, 2012 02:18
LINQ grouped SUM
var jobsOwing = from t in Db.Transactions
group t by t.Job into g
where g.Sum(t => t.Value) < 0
select new { Job = g.Key, NetPosition = g.Sum(t => t.Value) };
@chadedrupt
chadedrupt / ko.bootstrap.typeahead.js
Created April 10, 2013 06:12
Bootstrap Typeahead Custom Knockout Binding
ko.bindingHandlers.typeahead = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
$(element).typeahead();
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var value = ko.utils.unwrapObservable(valueAccessor());
$(element).data('typeahead').source = value;
}
};
@chadedrupt
chadedrupt / bootstrap.knockout.validation.js
Last active February 27, 2016 17:13
Use Bootstrap's error stying with knockout validation
ko.validation.configure({
insertMessages: true,
decorateElement: true,
errorElementClass: 'error',
errorMessageClass: 'help-inline'
});
@chadedrupt
chadedrupt / AddressAutocomplete.js
Last active July 24, 2020 01:37
Knockout Binding for Google Places autocomplete input. Also populates sibling bindings that match address component types of the places api. That part depends on underscore.js.
ko.bindingHandlers.addressAutocomplete = {
init: function (element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(), allBindings = allBindingsAccessor();
var options = { types: ['geocode'] };
ko.utils.extend(options, allBindings.autocompleteOptions)
var autocomplete = new google.maps.places.Autocomplete(element, options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
@chadedrupt
chadedrupt / index.html
Last active August 29, 2015 14:03
Chaos Game
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
function chaosGame(width, height, numSamplesMax) {
var corners = [
{
var connect = new Connect({
apiKey: 'YOUR_API_KEY'
});
var purchase = {
customer: {
firstName: 'Tom',
lastName: 'Smith'
},
id: '1849506679',
import * as _ from 'lodash'
const camelize = (object: any): any => {
if (_.isArray(object)) return _.map(object, camelize)
if (_.isPlainObject(object)) return _.chain(object)
.mapKeys((value: any, key: any) => _.camelCase(key))
.mapValues((value: any) => camelize(value))
.value()
return object
}
@chadedrupt
chadedrupt / mapSelector.ts
Created August 20, 2019 06:19
mapSelector for reselect
import { createSelector, Selector } from 'reselect'
import { isNil } from 'lodash'
export default function mapSelector<S, A, B>(
selector: Selector<S, A>,
mapping: (a: NonNullable<A>) => B
) {
return createSelector(
selector,
value => {
@chadedrupt
chadedrupt / Button.tsx
Created June 28, 2023 12:46
Tailwind Button
import classNames from 'classnames'
import cn from 'variant-classnames'
type Size = 'xs' | 'sm' | 'md' | 'lg' | 'xl'
type Color = 'stone' | 'indigo' | 'error'
const variants = {
size: {
xs: 'gap-x-1 px-2 py-1 text-xs',
sm: 'gap-x-1.5 px-2 py-1 text-sm',