Handling File Uploads on the Frontend With JS: Preview, Size, Dimensions, and Format

What I hate most when uploading a file is being told five minutes later that the file is too big or the dimensions are wrong! We usually leave upload validation to the backend, so a file has to be uploaded before we can check whether it meets the requirements. Images in particular also need their aspect ratio checked. If all of this is left entirely to the backend, the user experience can suffer. When I was researching profile picture uploads at NewTalk, I found that all of this can actually be handled on the frontend. With JS and HTML5 APIs, you can check the file’s format, size (20MB), and dimensions (100x100). You can even do previews and client-side image cropping and conversion! ...

February 9, 2017 · 3 min

The Art of Naming in Programming

Naming is an old, familiar topic in programming. From variable names to function and object names, not many people get it right. Do you really understand why variable names matter? Beyond making sure basic variable names are meaningful, how should you choose between common naming styles like Hungarian notation and camelCase? Here I’ll briefly share some of my thoughts on variable naming. ...

February 6, 2017 · 4 min

How to Use Web Inspector on iOS Safari

When doing front-end development on a computer, browsers provide handy developer tools that let us check error messages, storage contents, step through JS, and more. But how can we open these powerful developer tools when testing a web page on a mobile device? After all, you cannot fit them on a tiny screen! This article shares how, for iOS devices, you can connect to a Mac and use Mac Safari’s developer tools to remotely control the web page running in iOS Safari, which makes testing on iOS much easier. ...

February 3, 2017 · 2 min

Measuring Function Execution Time in JavaScript

To measure a function’s execution time in JavaScript, you can subtract the timestamp taken before the function runs from the timestamp taken after it finishes. Most people use new Date().getTime() to get a timestamp, but if your function runs in less than 1 millisecond, this won’t work, because the smallest unit of a timestamp is the millisecond. So I recommend the Window.Performance API. It gives you the number of milliseconds since the page started loading, with precision down to a thousandth of a millisecond. ...

January 24, 2017 · 1 min

Rails Capybara Feature Testing: A Diary of Pitfalls

If I had to describe today in one line, it would be “I learned Capybara for the first time and immediately stepped on a landmine.” I spent a full six hours (or more) writing two simple tests, and ran into all sorts of funny landmines along the way. If I didn’t write a post about this disastrous day, I’d be too angry to sleep tonight. To be clear up front: this is my first time writing tests, so some of my understanding may be wrong or out of date. Please leave a comment and I’ll fix it right away. This isn’t a technical article. It’s a rant (angry!). ...

January 16, 2017 · 3 min

JavaScript String Replace All

In JavaScript, String.replace may behave differently than you expect. When you want to replace a string, you’ll find that replace only replaces the first occurrence. For example, “AAA”.replace(“A”, “B”) gives “BAA”, not the “BBB” you might expect. This is because the first argument of JS replace is actually a regular expression, not a string. If you pass in a string, only the first match is replaced, which leads to results you didn’t expect. ...

January 16, 2017 · 1 min

JavaScript: Manipulating Browser History, Changing URLs, and the Back Button Event (Essential for SPAs)

Nowadays JavaScript is everywhere. Using JS to manipulate web pages, for things like Ajax, page switching, and pop-up windows, is more and more common. There are even JS frameworks such as React JS and Vue JS that help you develop Single Page Applications (SPAs). When you manipulate a web page with JS, the thing you fear most is URL changes and history. For example, if you switch pages with jQuery, the URL doesn’t change, because it’s a JS operation. And when the user clicks the browser’s Back button in the top left corner, they go straight to another page, because JS operations aren’t recorded in the history (in fact, they can’t be). To solve this series of problems, you can use history.pushState, history.replaceState, and the onpopstate event provided by HTML5. ...

January 14, 2017 · 6 min

Never Delete Items from a List Inside a Loop

“The foolish mistake I never get tired of stepping on, no matter how many times.” I always thought that once you make a mistake, you will remember it for good. But today I lost three hours to a bug at work. I could not figure out where the problem was. Finally, I carefully reread the code and realized… it was the same problem again. In one sentence: “Never delete any array element inside a loop.” ...

January 12, 2017 · 2 min

Implementing a Round-Up-to-Whole-Number Feature: Ideas and Methods

Recently I came across a “round up” feature on Zeczec. For example, if a checkout total is NT$1359, pressing the round-up button changes it to NT$1400. It’s a fun feature, but when I actually tried to implement it, I found that rounding up isn’t easy. There are many non-technical concerns. Here I’ll record the problems I ran into while implementing it, the process I went through, and the final solution! ...

January 11, 2017 · 5 min

Ngrok - The Tool That Lets You Develop Webhooks Locally Without a Deployed Environment

Ngrok is an essential tool for backend developers. Whenever I develop a website on localhost, webhooks, API callbacks, and OAuth are a real headache. Take my earlier experience developing a Mandrill Webhook to receive email open and send events as an example. Because you have to set the URL in the Mandrill admin panel before you can test, you have to upload the program to a server after writing it to see the results. That has a big impact on development speed. Ngrok was born to solve exactly this problem! ...

January 9, 2017 · 3 min