Posts

C# and, or, not pattern matching

Pattern matching in C# gives you the ability to shorten some of your code that checks for types, discrete values, and relational comparisons. One way this helps is to replace some of your boolean expressions with the and , or , and not syntax. if ( i is > = 2 and < = 7 ) { WriteLine ( " between 2 and 7 " ) ; } if ( i is 1 or 2 or 3 ) { WriteLine ( " In 1, 2, 3 " ) ; } There's a lot more you can do with pattern matching, so if you haven't looked into them yet, look up some documentation and articles, and see if there are ways it can help you be more productive. View code on GitHub

Javascript GUID

If you need a GUID in Javascript, here are samples for building your own, either in a browser setting, or using NodeJS where you can easily add more features. In Browser: function createGuid ( ) { return " xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx " . replace ( / [ xy ] / g , function ( c ) { var r = Math . random ( ) * 16 | 0 , v = c == " x " ? r : ( r & 0x3 | 0x8 ) ; return v . toString ( 16 ) ; } ) ; } NodeJS Technically this isn't a proper GUID , but it is 16 cryptographically-pseudorandom bytes and works just fine if you need something that fits into a Guid/Uniqueidentifier or whatever else you're calling it. const crypto = require ( " crypto " ) ; class Guid { constructor ( hex ) { if ( typeof hex != " string " ) { throw " Must be a string " ; } var val = hex . replace ( / [ ^ 0 - 9a - fA - F

Destructuring in javascript

Used to extract values from arrays and objects and assign to variables, making it easier for callers to see what they need to pass in to the function " use strict " ; // old way: function PersonOld ( options ) { this . name = options . name ; this . age = options . age ; this . height = options . height ; } // new way: function PersonNew ( { name , age , height } ) { this . name = name ; this . age = age ; this . height = height ; } // if you want it optional, then set to a default value: function PersonOptional ( { name , age , height } = { name : " Bill " , age : 31 , height : 23 } ) { this . name = name ; this . age = age ; this . height = height ; } // you can use defaults within the object: function PersonDefaults ( { name , age = 99 , height } ) { this . name = name ; this . age = age ; this . height = height ; } var options = { name :

SHA hash in NodeJS

const crypto = require ( " crypto " ) ; const filename = process . argv [ 2 ] ; const fs = require ( " fs " ) ; const hash = crypto . createHash ( " sha256 " ) ; const input = fs . createReadStream ( filename ) ; input . on ( " readable " , ( ) = > { const data = input . read ( ) ; if ( data ) { hash . update ( data ) ; } else { console . log ( ` ${ hash . digest ( " hex " ) } ${ filename } ` ) ; } } ) ; Reference https://nodejs.org/api/crypto.html

Javascript does element have class

If you need to check if an element has a given class, there are a few ways. jQuery has a hasClass function. If you're limited to users using IE10 or a real browser, then you can use the Element.classList property. But if you have to cater to old IE users, and aren’t using jQuery, then you can use the following to check to see if an element has a class. Just put this in one of your header script files, before anything that would use it: if ( ! Object . prototype . hasClass ) { Object . prototype . hasClass = function ( x ) { if ( ! this . className || ! this . className . indexOf ) { return false ; } return ( " " + this . className + " " ) . indexOf ( " " + x + " " ) >= 0 ; } ; } var elem = document . getElementById ( " div1 " ) ; var hasRed = elem . hasClass ( " red " ) ;

Get Selected Text in Javascript

function getSelectedText ( ) { if ( window . getSelection ) { return window . getSelection ( ) ; } if ( document . getSelection ) { return document . getSelection ( ) ; } if ( document . selection ) { return document . selection . createRange ( ) . text ; } return " " ; }

CDN with failover

Loading your scripts from a CDN is generally a good idea, for many reasons. Using a CDN helps load your site faster, possibly use less bandwidth, and reduces costs. Once in awhile, your CDN may be unavailable, and you want the site to work anyway. For example, if you have an internal intranet application, but your office external internet is down. You can try to use the CDN by default, but if it doesn’t work, you’ll load the script file from your server. < script type = "text/javascript" > function addScriptToDOM ( url ) { document . write ( ' \x3C script type="text/javascript" src=" ' + url + ' "> \x3C /script> ' ) ; } </ script > < script type = "text/javascript" src = "//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js" > </ script > < script type = "text/javascript" > window . jQuery || addScriptToDOM ( " /Scripts/jquery-