128 TILs and counting...

A single input in a form can submit

Today I learned that if you have a single text field in a form then browsers will automatically submit it when you press enter. <form> <label> Search <input type="text"> </label> </form> This is kinda handy since normally you need to add a submit button to be able to submit a form — but, you should be aware that if you add another field then you do need to add a submit button for the form to work....

January 7, 2025 · 1 min · Brandon Pugh

Type check and cast

Today I learned that you can use pattern matching in C# to check for a type and cast to it in the same expression. See the docs for more details. Microsoft even has a lint rule for it. if (x is Fruit) // Noncompliant { var f = (Fruit)x; // or x as Fruit // ... } if (x is Fruit fruit) { // ... }

December 13, 2024 · 1 min · Brandon Pugh

Selectively restoring changes from another branch or commit

Today I learned that you can use the --patch parameter with several git commands. The --patch parameter is probably most known for interactively staging changes from the cli, but I’ve never really used it because I find GUI clients are much more convenient for this. But apparently you can also use it with restore/checkout to grab specific changes from another branch. git restore --source=branch-name --patch This is something I haven’t seen easily done in any git client....

November 21, 2024 · 1 min · Brandon Pugh

Personal git ignore per repository

Today I learned that git has a $GIT_DIR/info/exclude file can contain additional patterns of files to ignore but isn’t committed to the repository. For me this is handy for some tooling configuration files that I use personally but don’t want to clutter the main .gitignore of the repo. If there are some files that you want to always ignore, then you can specify a global ignore file in your git config with core....

November 19, 2024 · 2 min · Brandon Pugh

Raw string literals in C# 11

Today I learned about raw string literals introduced in C# 11. The big improvement to me is that you don’t need to escape double quotes. So no more of this noise if you need a json string: string json = @" { ""number"": 42, ""text"": ""Hello, world"", ""nested"": { ""flag"": true } }"; You can now write: string json = """ { "number": 42, "text": "Hello, world", "nested": { "flag": true } } """; The other nice feature is that an extraneous indentation will stripped out based on the location of the closing """....

November 18, 2024 · 1 min · Brandon Pugh

SRI integrity hash algorithms

Today I learned that you can actually specify different hash algorithms for Subresource Integrity (SRI) hashes. If you aren’t familiar with SRI, this post does a good job explaining why it’s useful and how it would have mitigated the recent pollyfill.io incident. I needed to add the hashes to some scripts from a third party CDN that didn’t provide them and I came across this handy generator which let’s you choose which algorithm to use and defaults to SHA-384 and report-uri has a generator that just includes all 3 different hashes in the integrity value....

November 12, 2024 · 1 min · Brandon Pugh

Use URLSearchParams() to build a query string

Today I learned that javascript has a handy built-in function called URLSearchParams() that you can use to build a url query string from an object. const petfinderData = { key: "12345", shelterID: "abc00", count: 20, animals: ["dogs", "cats"], }; const query = new URLSearchParams(petfinderData); // returns "key=12345&shelterID=abc00&count=20&animals=dogs%2Ccats" const queryString = query.toString(); Read more: https://gomakethings.com/how-to-build-a-query-string-from-an-object-of-data-with-vanilla-js/ You’ll want to be careful with complex object list arrays since it serializes to a form like animals=dogs,cats, which not be the format the server expects....

November 7, 2024 · 1 min · Brandon Pugh

Edit commit message with git reword

I discovered that a reword option was added to --fixup back in git 2.32 The basic command looks like git commit --fixup=reword:<commit> and you can use it like you would the other autosquash commands. I recommend creating an alias for it though. Thanks to this post, I’ve had a fixup alias that uses fzf to select form recent commits so I created a similar one for reword: [alias] reword = "!...

November 2, 2024 · 1 min · Brandon Pugh

View transitions API

Today I learned about the new view transitions API and I was pretty impressed with how much of the heavy lifting the browser will now do for you to animate between states. Adam Argyle has a fun talk where he shows what you can do with this new API. I only had to add a couple of lines of javascript to animate reordering a list of items that would have required a more complicated technique like FLIP (first last invert play) in the past....

October 22, 2024 · 1 min · Brandon Pugh

Console.trace()

Today I learned about console.trace() when I needed to find out where a global function was being overridden: Object.defineProperty(window, 'setErrorMessage', { set(value) { console.trace('Global variable set:', value); } }); This little snippet outputs a stack trace anywhere code was trying to define this global function and let me right to the culprit. See more about .trace()

October 10, 2024 · 1 min · Brandon Pugh