Skip to content

Instantly share code, notes, and snippets.

View Beej126's full-sized avatar

Beej Beej126

View GitHub Profile
@Beej126
Beej126 / csharp_type_pattern_matching.cs
Created September 18, 2020 16:34
csharp type pattern matching
//https://stackoverflow.com/questions/43080505/c-sharp-7-0-switch-on-system-type/46692500#46692500
switch (object.GetType())
{
case Type _ when typ == typeof(SqlExpression<>):
Console.WriteLine("yes!");
break;
}
@Beej126
Beej126 / shred_json.tsql
Created May 28, 2020 22:42
shred_json.tsql
DECLARE @json NVARCHAR(MAX) = '
[{
"invoiceNumber": "7000962",
"invoiceDate": "2018-08-22T00:00:00.000-07:00",
"transactionName": "RES LH INV",
"invoiceDueDate": "2018-09-01T00:00:00.000-07:00",
"originalInvoiceAmount": "3291.17",
"amountRemainingOnInvoice": "3291.17",
"sentToCollectionStatus": "False"
}, {
@Beej126
Beej126 / newtonsoft_di.cs
Last active May 5, 2020 20:49
enable asp.net core DI with newtonsoft contract resolver
//the blocker for what i needed at the time was these techniques only appear to resolve the DI required
//for populating nested properties within the root object deserialization...
//but i was looking for DI to the root object constructor and that wasn't happening yet...
//my guess is doing something more DI savvy in the ContractResolver...
//we see that the root object comes through there, but that object itself is not DI'd...
//i.e. it's not an interface mapped to a class itself...
//it has an interface in the constructor parms
//so maybe doing something like ActivatorUtilities.CreateInstance(IServiceProvider, concreteType)
//instead of sp.GetService(concreteType) at line 65 below... that actually smells pretty promising
@Beej126
Beej126 / AuthorizationPolicyBuilderExtensions.cs
Created April 16, 2020 15:43
c# ternary fluent builder
public static class AuthorizationPolicyBuilderExtensions
{
//https://devio.wordpress.com/2014/02/07/conditions-in-fluent-interface-method-chaining/
public static T Ternary<T>(this T t, bool cond, Func<T, T> ifTrue, Func<T, T> ifFalse)
where T : AuthorizationPolicyBuilder => cond ? ifTrue(t) : ifFalse(t);
}
@Beej126
Beej126 / cs.cs
Last active April 4, 2020 01:14
aspnet core snips
//ultimately this sever feature stuff isn't available in azure until after startup
var runningHostAddresses = app.ServerFeatures.Get<IServerAddressesFeature>().Addresses.Select(a => new Uri(a).Host);
var isLocalDev = new[] { "127.0.0.1", "localhost" }.Intersect(runningHostAddresses).Count() > 0;
Environment.SetEnvironmentVariable("eComm_Storefront_localdev", isLocalDev.ToString());
_logger.LogInformation($"running on host ips: {string.Join(", ", runningHostAddresses)}");
//
//it's fuzzy to see a good established pattern for doing local overrides for settings retrieved from a web api
//especially since the existing paradigm is based on Azure subscriptions (aka environments) being populated with settings,
//and a "localdev" PER DEVELOPER environment doesn't seem to be an easily retrofitted concept
//wscript debugger = scd10en.exe
// http://www.mediafire.com/file/bvobcrm39ve1ywn/Microsoft+script+debugger.zip
// https://www.google.com/search?q=scd10en.exe
//https://stackoverflow.com/questions/10980171/how-do-i-get-microsoft-script-debugger-to-work-with-64-bit-windows-7
//*** have launch the win32 version of cscript/wscript
// /d /x starts debugger
// %windir%\SysWOW64\cscript.exe /d /x script.js arg1 arg2
var wshShell = WScript.CreateObject("WScript.Shell");
@Beej126
Beej126 / msal_demo.js
Last active March 14, 2020 18:32
MSAL.js via System.js with SRI hash
//SRI = Subresources Integrity
var script = document.createElement("script");
script.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.6/system.js"); //new SystemJs v2+ doesn't support CJS modules
script.addEventListener('load', async () => {
System.config({
//msal requires an [SRI hash](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)
meta: { "https://alcdn.msftauth.net/lib/1.2.1/js/msal.js": { integrity: "sha384-9TV1245fz+BaI+VvCjMYL0YDMElLBwNS84v3mY57pXNOt6xcUYch2QLImaTahcOP" } },
//set .js as default extension for any NESTED dependencies "require()'d" from within the main import below
@Beej126
Beej126 / html2svg.js
Last active May 20, 2022 16:50
html2svg download in chrome page
//https://github.com/bubkoo/html-to-image
//load systemjs as a way to import html-to-image library which is published as node module with require dependencies
//you could do this via normal <script> tag as well
var script = document.createElement("script");
script.setAttribute("src", "https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.21.6/system.js"); //new SystemJs doesn't support CJS modules
document.body.appendChild(script);
//set .js as default extension for nested dependencies "require()'d" from index.js below
System.config({
@echo off
setlocal
set source=%~f1
call:remove_quotes %source%
set dest=%~f2
call:remove_quotes %dest%
::echo source: %source%
::echo dest: %dest%
::pause
@Beej126
Beej126 / snippets.ps1
Last active January 16, 2020 23:47
powershell snippets
# oh yeah!!
npm i ((Get-Content .\package.json | ConvertFrom-Json).dependencies | gm -Name @Progress* | select -ExpandProperty Name | % { $_ + "@latest" } )
# npm list filtered by name and joined on one line
# facilitates doing a bunch of updates to the whole kendo bundle
# old way: npm list 2>$null | sls "^\+-- (@progress.*?)@" | select @{n="val";e={$_.matches[0].groups[1].value+"@latest"}} | join-string -property val -separator " " | clip
npm list 2>$null | sls "^\+-- (@progress.*?)@" | % { $_.matches[0].groups[1].value} | join-string -Separator " "
# *nix sed is more concise on this one