Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / Install.ps1
Last active November 8, 2020 13:25
PowerShell Install helper function
function Install {
[OutputType([void])]
param (
[Parameter(Mandatory, ValueFromPipeline)] [string] $Module,
[Parameter(Mandatory)] [ValidatePattern("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,9}$")] [string] $Version
)
"Testing module '$Module' presence." | Write-Verbose
$moduleInfo = Get-InstalledModule $Module -ErrorAction SilentlyContinue -Verbose:$false
if (-not $moduleInfo) {
"Module '$Module' not present. Attempting installation." | Write-Verbose
@gsscoder
gsscoder / git-tag_helpers.sh
Created April 21, 2020 08:05
Two simple BASH scripts to add/remove Git tags from local/remote
# addtag.sh
###########
#!/bin/sh
git tag -a v$1 -m "Version $1" && git push origin --follow-tags
# rmtag.sh
##########
#!/bin/sh
git tag -d v$1 && git push origin -d v$1
@gsscoder
gsscoder / TypeSwitch_PatternMatching.cs
Last active May 4, 2020 05:20
Helper class that allows pattern matching on types
// Based on: https://docs.microsoft.com/en-gb/archive/blogs/jaredpar/switching-on-types
// Example:
// TypeSwitch.Do(propertyInfo.PropertyType,
// TypeSwitch.Case<bool>(() => value = property.Value.GetBoolean()),
// TypeSwitch.Case<int>(() => value = property.Value.GetInt32()),
// TypeSwitch.Case<long>(() => value = property.Value.GetInt64()),
// TypeSwitch.Case<double>(() => value = property.Value.GetDouble()),
// TypeSwitch.Case<string>(() => value = property.Value.GetString()),
// TypeSwitch.Default(() => throw new InvalidOperationException("Type is not supported.")));