Blog
Word Boundaries and Lookahead Assertions
As I was trying to improve the lexer for
postcss-calc
, I learnt about two regular expression features: the word boundary anchor character and lookahead.Word Boundaries and Lookahead Assertions
As I was trying to improve the lexer for
postcss-calc
, I learnt about two regular expression features: the word boundary anchor character and lookahead.How large is a Buffer?
The
gzip()
andgzipSync()
functions in the zlib return a result of typeBuffer
. How do you calculate the size of the result? I came across two properties of theBuffer
type:length
andbyteLength
. Is there a difference? The API documentation forBuffer
does not tell you, because it lacks thelength
andbyteLength
properties. Where do these properties come from? Likely from the prototype.Scripting in Java?
Since Java 11, you can run a Java program without compiling it first. Can you script in Java like in Python?
Migrate a Node.js project to ESM
To use an ESM Node.js module in your own, you need to use import. But node can only use import inside ESM modules, so you need to convert your own CommonJS modules to ESM. Writing import and converting it to require() with Babel does not count! Otherwise you will get an error like this: SyntaxError: Cannot use import statement outside a module So, how do you migrate your code to use import instead of require()?
Invasion of the Killer Retries
How can you bring your web application down? For example, by trying to make it more reliable.
Docker cheatsheet
Here’s how to perform some common tasks with Docker.
Dive into esbuild
To prepare your frontend code for production, you can use esbuild. Compared to webpack or rollup, esbuild offers limited code splitting options, but if your JavaScript code is mostly used on one page, esbuild can be a good solution.
Secrets of CSS modules
With a frontend framework like Next.js or just a bundler like webpack, you can import the CSS class names from a CSS module as if importing variables from a JavaScript module. For example, if your CSS file contains this code: // styles.css someClass { background-color: blue; } you can apply the someClass class to an HTML element from a JavaScript file by importing the class name: // main.js import { someClass } from '.
Technical Writing Tips
Writing my React book has been humbling: I wasn’t as good a writer as a I imagined. Here’s some what I’ve learnt working with my editor.
Backing up MySQL with Percona XtraBackup
The logical backup created by
mysqldump
was just 430MB. but for testing I wanted to repeatedly tear down and restore the same database. The restore was too slow even after disabling foreign key checks and unique checks.I decided to try Percona XtraBackup. Here’s how I completed a full backup and restore. For incremental backups, you must do things a little differently, but I won’t handle incremental backups in this post.
The Sociology of Input Validation
Maybe writing programs that accept whatever data comes in, and dealing with the consequences later, isn’t as sloppy as it sounds.
Havoc with Hibernate Filters and Inheritance
After migrating from Hibernate 3.6 to Hibernate 5.2, our web framework would sometimes fail mysteriously to bind controller method arguments to the corresponding persistent entity. The cause turned out to be an obscure interaction between JPA’s single table inheritance and a misconfigured Hibernate filter.
Smart Mistakes Are the Worst
When writing bad code, playing smarter sometimes gives the worst results.
How to wrap a web REST API
When calling into a third-party REST API, it’s OK to define high- and low-level operations, and leave out the middle.
Lone Heroes on Software Projects
On a software project, communicate to colleagues, managers and clients when you’re in trouble:
Bit Twiddling
Bit operations preserve De Morgan’s laws. Bit values are sequences of symbols. What makes them behave like booleans? Where does the essence of a boolean lie?
Validate like a Native with React
Validating a form doesn’t always require a lot of code. With HTML5 constraint validation, you can describe form validation rules with HTML attributes on the form fields.
For example, to enforce that an
<input>
element should not be empty, add therequired
attribute to the element:<input id="name" name="name" required/>
You can use HTML5 constraint validation even with React. Let’s look at an example.
How to turn an HTML form into JSON
To send data from an HTML form to a JSON API, you can extract all the form data in one go with
form-serialize
.React and refs, an eternal love story
As in our previous exercise, we want to get the DOM node for a form component to validate the form with the HTML constraint validation API.
Since React 16.3, we can store references to DOM nodes with
React.createRef()
. Let’s repeat the exercise with the new API.