Skip to content

Instantly share code, notes, and snippets.

@jmcd
jmcd / TestOutputHelperTextWriterAdapter.cs
Last active December 8, 2022 09:04
Capture `Console.Out` in an XUnit `ITestOutputHelper`
public class TestOutputHelperTextWriterAdapter : TextWriter
{
private readonly ITestOutputHelper output;
private string currentLine = "";
public TestOutputHelperTextWriterAdapter(ITestOutputHelper output)
{
this.output = output;
}
@jmcd
jmcd / M.cs
Created January 8, 2019 07:37
Long Division
public class M
{
public static int[] DigitsOf(int value)
{
var numbers = new List<int>();
for (; value > 0; value /= 10)
{
numbers.Add(value % 10);
}
return numbers.ToArray();
@jmcd
jmcd / App.js
Created October 25, 2018 13:47
My first react app - an implementation of https://reactjs.org/docs/thinking-in-react.html
import React, { Component } from 'react';
import './App.css';
class App extends Component {
render() {
const unfilteredData = [
{ category: "Sporting Goods", price: "$49.99", stocked: true, name: "Football" },
{ category: "Sporting Goods", price: "$9.99", stocked: true, name: "Baseball" },
{ category: "Sporting Goods", price: "$29.99", stocked: false, name: "Basketball" },
@jmcd
jmcd / _recording.gif
Last active December 20, 2022 08:45
A demo of an animation system for SDL. Sequencing function-pointers that change the values of structs representing the model that gets rendered.
_recording.gif
@jmcd
jmcd / _matrix.gif
Last active September 11, 2018 08:05
Display like The Matrix movie on a C64 in 6502 assembly
_matrix.gif
@jmcd
jmcd / _screenshot.png
Last active January 14, 2019 08:30
A Morié pattern generator that compiles to native desktop application and wasm
_screenshot.png
@jmcd
jmcd / car_game.py
Last active July 21, 2018 18:32
BBC micro:bit car game
from microbit import *
from random import randint
SCENE_WIDTH = 4
INITIAL_DELAY = 500
POSSIBLE_ROADSIDE_ROWS = [
[False, False, True, True, True, ],
[True, False, False, True, True, ],
[True, True, False, False, True, ],
@jmcd
jmcd / MultipleRole.cs
Created June 26, 2018 09:25
Handling multiple role from IdentityServer4 on the client
/*
When IdentityServer4 returns role in access-token JSON the type of the "role" member value differs depending on how many roles are present. Two cases:
1) Single role - the value is a string, e.g. "role": "patient".
2) Multiple roles - the value is an array-of-string, e.g. "role": ["patient", "agent"]
The AspNetCore framework on the client side does not like the second case, and it assumes a string, giving an error saying that it can't cast from Newtonsoft.Json.Linq.JArray to Newtonsoft.Json.Linq.JToken.
You can work around this by checking the type of the role JSON in OpenIdConnectOptions.
*/
@jmcd
jmcd / DynamicOrganizationRoleClaimsFactory.cs
Created June 26, 2018 09:24
Adding dynamic claims to a principal
public class DynamicOrganizationRoleClaimsFactory : UserClaimsPrincipalFactory<ApplicationUser>
{
private readonly ApplicationDbContext dbContext;
public DynamicOrganizationRoleClaimsFactory(UserManager<ApplicationUser> userManager,
IOptions<IdentityOptions> optionsAccessor, ApplicationDbContext dbContext)
: base(userManager, optionsAccessor)
{
this.dbContext = dbContext;
}
@jmcd
jmcd / Iso.ts
Created June 1, 2018 15:20
cubes
namespace Iso {
function lface(ctx: CanvasRenderingContext2D, width: number, height: number) {
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(width, width / 2);
ctx.lineTo(width, height + width / 2);
ctx.lineTo(0, height);
ctx.closePath();