Sometimes you want to quickly prove a small bit of code works. There are multiple ways to do this, but this is the easiest one I’ve found.

Robot doing a one-handed hand stand with ease
Robot doing a one-handed hand stand with ease
Easier than a one-hand handstand

I have .git configured so that any file that starts with a period doesn’t get uploaded into source control. Therefore I start by creating a file named either .run.js or .run.ts. I keep these files at the root of my project and reuse them whenever I want to experiment with code.


Sometimes a little forceful bending is a good thing

Just a quick one on the Visual Studio Code debugger. I seem to need to revisit the launch.json file every couple of months. The most recent bit of odd behavior was a torrential downpour of Could not read source map... messages in bright orange. Not the end of the world, but not exactly convenient having pages and pages of bright red error messages cluttering up the terminal window. After putting up with this for a while, turns out the solution was, you guessed it, a trip to the launch.json file. The following configuration setting did the trick

"resolveSourceMapLocations": […

In addition to renaming you sometimes also need to remove properties and merge those changes into a new object.

Android holding an oversized wrench
Android holding an oversized wrench
Time to get out the big wrench and unbolt some properties

In the previous article we looked at how to rename a property on an object. If you haven’t read that post, it makes for a more logical starting point on this journey.


When you need a new object that is a close relation to an existing object but one or more of the property names must change.

Android “rest”ing against fresh batteries with legs “spread” out.
Android “rest”ing against fresh batteries with legs “spread” out.
Am I resting or spreading out?

I first saw this technique in this excellent post by Joel Thoms. That article has a collection of tips. I just wanted to focus on this one technique, and provide a more robust explanation.

Real-World Need

While my example code is very slimmed down this technique did have roots in a real-world need. We were writing an API call to return a single object that we wanted to construct from some existing objects. …


The npm package ts-sinon can be quite useful but you have to remember sinon.restore() may not do what you think it does.

Finger on string — reminder
Finger on string — reminder

In this example, we will have a main class we want to unit test and it will use a second helper class. Here’s the main class we want to test.


Newer flavors of the for loop like for...of, for...in , and the array prototype forEach are making me more averse to using the standard JavaScript for statement. You know, the one that looks like this.

const MAX_COUNT = 10
for (let index = 0; index < MAX_COUNT; ++index) {}

Here’s a common realish-world TypeScript snippet that I’ve seen in tests:


Someone mentioned Utah county, Utah today. Since counties are subdivisions of states, it seems rather uppity (not to mention confusing) for a county to take on the name of the state. This line of thinking naturally led me to think about county names in places I have lived. Most of those places seemed well-behaved except New Hampshire.


I’ve ignored the fact that you can nest JavaScript functions and a neat trick was hiding in plain sight.

The Original Problem

I was refactoring some code that was more opaque than it needed to be, especially given I wasn’t very familiar with the business domain logic. Here’s a simplified approximation of the original code:


Teamwork

I’ve been using Slack for over five years now and a few tips can make you more efficient and a better teammate. In the age of COVID-19 a lot more people are using Slack. So a tip for the veterans out there, be kind and patient (and maybe point your teammates here!)

  1. If you retain nothing else, remember this one; hold down the ⇧(Shift)Key when hitting Enter to add a line break without sending the message. This prevents the person on the receiving end from getting an alert, a toast notification, and a badge number increase every time you hit…

Tod Gentille

Pluralsight author and software developer for web services using Node and Typescript. Coding since 1980.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store