Skip to content

Instantly share code, notes, and snippets.

@JustinMorgan
JustinMorgan / widgets 2.ts
Last active October 23, 2021 05:35
More of the piping involved in widget inheritance, associating model classes with service classes, and finding the right constructor by a string key.
//////////// model //////////////
interface IWidgetModel {
constructor: WidgetModelCtor;
}
type WidgetModelCtor = Function & {
readonly widgetType?: string;
serviceClass?: new (...args: any[]) => IWidgetService;
}
@JustinMorgan
JustinMorgan / widgets.ts
Last active October 21, 2021 15:03
Set-once static and instance value in subclasses / Requiring a static member / Specifying a static member in an interface
abstract class Widget {
abstract readonly widgetType: string;
public display: { [key: string]: string; };
constructor(data?: any) {
this.display = {
a: 'a' in data ? data.a : 'a-parentClass',
b: 'b' in data ? data.b : 'b-parentClass',
c: 'c' in data ? data.c : 'c-parentClass'
//used as an example DTO type.
class Crosslink
{
public string Code { get; set; }
public string FullDescription { get; set; }
}
public async Task<Crosslink[]> QueryAsync(CommandType commandType, string query, params DbParameter[] parameters)
{
var list = new List<Crosslink>();
@JustinMorgan
JustinMorgan / typescript-inheritance.ts
Created September 30, 2019 17:16
Typescript parent class/child class interaction and precedence
abstract class A {
p: string = 'A.p';
q: string;
r: string;
s = 'A.s'
c: C;
constructor(dto: any) {
console.log('A.constructor')
documentation?("jQuery.fn.order", {
Description: "jQuery plugin for client-side list sorting"
Usage: (foo, selector = null, sortBy = null, options = null) -> [
$(foo).order(selector, sortBy, options)
$(foo).order(selector, sortBy)
$(foo).order(selector, options)
$(foo).order(options)
$(foo).order()
]
Parameters:
@JustinMorgan
JustinMorgan / JsonDumper.cs
Last active January 17, 2017 21:25
Dump complex object to JSON (quick & dirty approximation of JavaScript's console.log)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using System.Text;
public static class JsonDumperSimple
{
public static string Stringify(object obj)
//--------------------------- Context-Binding Lambda Expressions ------------------------------------
// Expression bodies
var evens = [2,4,6,8,10];
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var fives = [];
// Statement bodies
nums.forEach(v => {
if (v % 5 === 0)
@JustinMorgan
JustinMorgan / shuffle-visualizer.coffee
Last active February 22, 2016 19:21
Shuffle Visualizer: Visual sanity-check for randomness in deck-shuffling algorithms (JS and CoffeeScript versions)
#Usage:
#1. Declare your shuffle() function. No parameters necessary, since `deck` is a global variable.
#2. Run `test()`.
deckSize = 52
deck = [0...deckSize]
rand = (min = 0, max = deckSize) ->
Math.floor(Math.random() * (max - min)) + min
@JustinMorgan
JustinMorgan / DictionaryExtensions.cs
Created December 1, 2015 00:03
ForEach and Zip for IDictionary
using System;
using System.Collections.Generic;
using System.Linq;
namespace Extensions
{
public static class DictionaryExtensions
{
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
{
@JustinMorgan
JustinMorgan / weekNumber.js
Last active October 20, 2015 21:50
Which number week of the year is it?
function weekNumber(date) {
var currYear = date.getFullYear();
var jan1 = new Date(currYear,0,1);
return Math.ceil((((date - jan1) / 86400000) + jan1.getDay() + 1)/7);
}
function weekNumberUTC(date) {
var currYear = date.getUTCFullYear();
var jan1 = new Date(currYear,0,1);
return Math.ceil((((date - jan1) / 86400000) + jan1.getUTCDay() + 1)/7);
}