Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
ninmonkey / Using Powershell New or Assignment.md
Last active May 17, 2025 17:46
Testing `type = @( $data )` and `[type]::new( $data )`

Explicitly calling ::new()

using namespace System.Collections.Generic
$example = 9, 4, 3

[List[int]]::new( [int[]]$example ) 
  • I had to type it as an [int[]]
@ninmonkey
ninmonkey / BasicTable.html
Last active May 16, 2025 19:04
Focing Html with 3 columns to `<table>` to share column widths
<html><body>
<table class="cols-3">
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
@ninmonkey
ninmonkey / Saving-Excel-Workbook-As-Base64-Round-Trip.pq
Last active April 25, 2025 17:14
Saving-Excel-Workbook-As-Base64-Round-Trip.pq
let
Path_Xlsx = "c:\data\workbook.xlsx",
Summary = [
// [1] using "enter-data" saves a table as json like this
b64_str = "i45WSkosUoqNBQA=",
bytes = Binary.FromText( b64_str, BinaryEncoding.Base64),
bytes_decompressed = Binary.Decompress( bytes, Compression.Deflate),
final_json_str = Text.FromBinary( bytes_decompressed, TextEncoding.Utf8 ),
// [2] converting some binary file to base64
@ninmonkey
ninmonkey / PwshClass▸Using▸Text.Json.ps1
Last active April 20, 2025 21:06
AutoJson.Basic-Text.Json.md
#Requires -Version 7
using namespace System.Collections.Generic
using namespace System.Text
using namespace System.Text.Json
using namespace System.Text.Json.Serialization
using namespace System.Linq
$assembly = Add-type -AssemblyName System.Text.Json -PassThru -ea 'stop'
<#
@ninmonkey
ninmonkey / Strip-Shared-Predent-Text-Like-Python.ps1
Last active April 7, 2025 20:29
Strip-Shared-Predented-Text-Like-Python.ps1
$longMessage = @"
This is a long message.
It has several lines.
Some are indented
more than others.
Some should start at the first column.
Some have "quoted text" in them.
"@
function TrimPredent {
@ninmonkey
ninmonkey / Microtopia Hunger.cs
Last active March 27, 2025 00:15
Microtopia Hunger.md
void updateHunger() {
// applied about every 1 second?
curDrain = GetCurrentDrain();
energy -= curDrain // but clamps to keep it >= 0
newTier = GetTier( energy )
if ( newTier != curTier ) {
SetTier( newTier )
}
}
@ninmonkey
ninmonkey / Experiment with SplitByTextWhitespace.md
Last active March 19, 2025 22:06
Power Query and splitting by multiple whitespace

This function works sort of like calling Regex.Split with this regex

\s+

Here's fun way to experiment

let
@ninmonkey
ninmonkey / QuickCompare-TextEncoding.ps1
Last active March 5, 2025 00:33
Assumes pwsh7. Decode a few strings with mismatched encodings
# Assumes pwsh7
# tip: pwsh 7 added overloads
$StrIn = 'Г¶'
$bytes = [Text.Encoding]::GetEncoding(1252).GetBytes( $StrIn )
$Bytes = [Text.Encoding]::GetEncoding('utf-8').GetString( $bytes )
# and also
$StrIn.EnumerateRunes | Ft -auto
@ninmonkey
ninmonkey / PowerQuery - Nested Let vs Record Expression.md
Last active February 21, 2025 16:05
Let expressions are synactic sugar for record expressions

I used pseudocode to shorten the example. The structure is good.

Using nested let

let
    Source1 =
        let 
            Workbook = File.Contents(...),
            Final    = Table.SelectColumns( ... )
@ninmonkey
ninmonkey / Pwsh▸ParameterIsBlank▸NewEmployee.ps1
Last active February 3, 2025 15:12
Pwsh▸ParameterIsBlank▸NewEmployee.ps1
function NewUser {
<#
.synopsis
Create a new user with a name. Any "blank" values will fallback with a default value
.DESCRIPTION
If you use ValidateIfNotSomething, invalid or missing values throws.
- Sometimes you want the command that always works, with a fallback value
- $null, empty string to count as blank