168 TILs and counting...

Stash changes in the index

I just recently discovered that Git 2.35 added the --staged option to the git stash command. This makes it easy to quickly stash only the changes that you鈥檝e staged.

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鈥檝e 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鈥檛 seen easily done in any git client. ...

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鈥檛 committed to the repository. For me this is handy for some tooling configuration files that I use personally but don鈥檛 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.excludesFile. ...

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鈥檛 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 """. 馃敟 ...

 路 1 min 路 

SRI integrity hash algorithms

Today I learned that you can actually specify different hash algorithms for Subresource Integrity (SRI) hashes. If you aren鈥檛 familiar with SRI, this post does a good job explaining why it鈥檚 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鈥檛 provide them and I came across this handy generator which let鈥檚 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. ...

 路 1 min 路 

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鈥檒l 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. Asp.net model-binding for instance, by default expects a format like animals=dogs&animals=cats (and some other variations). ...

 路 1 min 路 

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鈥檝e had a fixup alias that uses fzf to select form recent commits so I created a similar one for reword: [alias] reword = "!git log -n 50 --pretty=format:'%h %s' --no-merges | fzf | cut -c -7 | xargs -o -I{} git commit --fixup=reword:{}" Note, if you鈥檙e on Windows, you can install fzf via Chocolatey. And you might also be interested in PSFzf which a Powershell module that wraps fzf. ...

View ModelState errors while debugging

Today I learned how you can view the Modelstate errors while debugging a controller in Asp.Net: ModelState.Where(x => x.Value.Errors.Count > 0).Select(x => x.Key).ToList() Just paste that in the watch window. If you鈥檝e tried debugging to find what field is causing the Modelstate to be invalid, you鈥檒l know how tedious it is to dig through but thanks to this post for this handy tip!

 路 1 min 路 

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. ...

Don鈥檛 put an error boundary in the root of a react component

You probably don鈥檛 want to put a React error boundary at the root of a component (meaning, wrapping the component鈥檚 own render output). This is because error boundaries only catch errors in their child component tree. They won鈥檛 catch errors that occur within the error boundary component itself, including errors in its own render method or lifecycle methods. To use an error boundary effectively, you need to wrap the component you want to protect with the error boundary from the parent. ...

 路 2 min 路