Recent Snippets

Posting to Medium using Node.js and fetch()

You can use the Medium REST API to post new articles on Medium. They previously had a Node.js SDK to make using the API a bit simpler, but that SDK is no longer supported. Rather than relying on an unsupported SDK, you can call the REST API endpoints directly using the node-fetch package. The following... […]

Special formatting for SVG images with CSS

I frequently run into a problem displaying SVG images in my blog posts. SVG images tend to have no background color (so the post background bleeds through) and don’t have any space between the artwork and what would be the border. This left the SVG images looking a bit squished and washed out even though... […]

Flattening a JavaScript array with a generator

ECMAScript 2019 will introduce the Array.prototype.flat() method to aid in flattening out multidimensional arrays in JavaScript. However, flattening arrays has been easy in JavaScript every since ECMAScript 6 (2015) introduced generators. Because generators can yield values in any order, not just linearly, it’s possible to use a recursive generator to easily flatten an array. Here’s... […]

Reading streams with promises in Node.js

If you’ve tried to use the Node.js fs.promises API, you might have noticed that there doesn’t appear to be a cross-platform way of using fs.readFile() to read from process.stdin (such as process.stdin). Using the old, callback-based API, you could pass in a numeric file descriptor, such as: import fs from "fs"; const text = fs.readFileSync(0,... […]

Setting up Visual Studio Code intellisense for Jest globals

One of the benefits of Visual Studio Code is it’s ability to automatically detect the type of JavaScript value you’re working with and provide autocomplete (aka intellisense) for properties and methods. This works really well whenever you’re using a module system and explicitly importing values from modules. If a framework happens to add random global... […]