Skip to content

Instantly share code, notes, and snippets.

View gsscoder's full-sized avatar
💭
obsessive coding disorder

coder (π³) gsscoder

💭
obsessive coding disorder
View GitHub Profile
@gsscoder
gsscoder / Test-Property.ps1
Last active April 6, 2020 15:20
Properly way to test object properties existence in PowerShell with strict mode activated
Set-StrictMode -Version Latest
function Test-Property([Parameter(ValueFromPipeline)] $object, [string] $name) {
try { $name -cin $object.PSObject.Properties.Name }
catch { $false }
}
$path = '/Users/someone/temp/file.json'
$json = $path | Get-Content -Raw | ConvertFrom-Json
[bool] $exists = $json.someKey | Test-Property -name 'someOtherKey'
@gsscoder
gsscoder / New-AzResource_APIM.ps1
Created April 3, 2020 06:24
Attempt to create APIM with New-AzResource
# This code actually doesn't work.
$secret = 'B=EBh3glTf523Xj@uLGO]o@kSteLoC@O'
$group = 'INTRANETAI-Europe-DEV'
$service = 'intranetai-europe-test-dev'
$context = New-AzApiManagementContext -ResourceGroupName $group -ServiceName $service
$versionSet = New-AzApiManagementApiVersionSet -Context $context -Name 'news' `
-Scheme Header -HeaderName 'x-api-version' `
@gsscoder
gsscoder / ConvertTo-PascalCase.ps1
Last active February 16, 2021 22:15
PowerShell function to convert kebab-case to PascalCase
# 'hello-wonderful-world' | ConvertTo-PascalCase
# outcome: HelloWonderfulWorld
function ConvertTo-PascalCase([Parameter(ValueFromPipeline)] [string] $text) {
($text -split '-' | ForEach-Object {
"$($_.ToCharArray()[0].ToString().ToUpper())$($_.Substring(1))" }) -join ''
}
@gsscoder
gsscoder / self-parse_pipeline.yml
Last active March 5, 2020 06:37
Azure Pipeline step to check it's pool or fail
trigger:
- none
pool:
vmImage: 'windows-latest'
steps:
- powershell: |
$value = Select-String -Path 'azure-pipelines.yml' -Pattern "(?<=vmImage:\s').*(?='$)"
if ($value -like '*windows*') {
@gsscoder
gsscoder / AzurePipelines_Conditions.cs
Last active February 22, 2020 19:59
C# draft of Azure Pipelines conditions with ExpressionEngine
using System;
using System.Collections.Immutable;
using System.Linq;
using ExpressionEngine;
class Program
{
static void Main(string[] args)
{
var context = new Context();
@gsscoder
gsscoder / iterators.ts
Created February 13, 2020 06:10
Simple TypeScript iterator example
function yieldNumbers() : IterableIterator<number> {
return generator()
function *generator(): IterableIterator<number> {
yield 0
yield 1
yield 2
yield 3
}
}
@gsscoder
gsscoder / factorial.ts
Last active October 23, 2022 06:48
Typescript script to calculate factorial of a number
// $ tsc factorial.ts && node factorial.js
// 3628800
function factorial(num: number) : number {
if (num == 0) return 1
else return num * factorial(num - 1)
}
console.log(factorial(10))
@gsscoder
gsscoder / cleanzsh.sh
Created February 7, 2020 17:18
Script to speed ZSH startup (removing history files)
#!/bin/sh
sudo rm -rf /private/var/log/asl/*.asl
@gsscoder
gsscoder / Guard.cs
Last active November 27, 2021 13:07
C# arguments validation guard class
using System;
using System.Linq;
using System.Runtime.CompilerServices;
static class Guard
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void AgainstNull(string argumentName, object value)
{
if (value == null) throw new ArgumentNullException(argumentName, $"{argumentName} cannot be null.");
@gsscoder
gsscoder / EventHelper.cs
Last active January 21, 2020 13:44
C# helper class for raising events
using System;
using System.Runtime.CompilerServices;
static class EventHelper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void RaiseEvent(object sender, EventHandler handler, EventArgs args, bool enabled)
{
if (enabled && handler != null) {
handler(sender, args);