Skip to content

Instantly share code, notes, and snippets.

View stand-sure's full-sized avatar

Christopher J. Anderson stand-sure

View GitHub Profile
@stand-sure
stand-sure / AsyncLazy.cs
Created December 3, 2020 20:24
Async version of Lazy<T>
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
//// inspired by Stephen Toub's https://devblogs.microsoft.com/pfxteam/asynclazyt/
/// <summary>
/// Provides support for async lazy initialization.
/// </summary>
/// <typeparam name="T">Specifies the type of element being lazily initialized.</typeparam>
@stand-sure
stand-sure / isSubclassOf.ts
Created May 28, 2020 14:17
determines if the shape of an object's prototype is the same as the shape of another object
const isSubclassOf = function isSubclassOf(derived: any, base: any) {
const pair = [Reflect.getPrototypeOf(derived), base];
const allKeys = pair.reduce(
(keys, instance) => keys.concat(Object.keys(instance)),
[]
);
const distinctKeys = new Set(allKeys);
@stand-sure
stand-sure / sum.ts
Created May 27, 2020 18:01
sums an arbitrary number of arguments
const sum: (...values: any[]) => number = (...values) => {
return values.reduce(
(total, val) => total + (isNaN(Number(val)) ? 0 : Number(val))
);
};
export { sum };
@stand-sure
stand-sure / Moq-demo.cs
Last active April 26, 2024 14:58
Example of how to use Moq in ASP.NET MVC Unit Tests with NUnit
namespace MoqDemo.Tests
{
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
using NUnit.Framework;
using HomeController = Controllers.HomeController;