JavaScript Articles

Node.js REPL history

Node.js has included a persistent history with it’s REPL (real-eval-print loop) for quite some time now. It’s a fantastic quality of life feature, and it even supports reverse-i-search. The other day, I got to wondering, where the heck does this history even live? Turns out, it’s just living in a […]

How to convert JSON to CSV in JavaScript

Last week we discussed converting CSV to JSON in JavaScript. This week, we’re going to talk about going in the opposite direction, converting JSON back to CSV. Similar to last week, we’re going to use the immensely popular papaparse library. As it turns out, even though it’s touted as the […]

How to convert CSV to JSON in JavaScript

Parsing CSV data isn’t as easy as splitting the string up into individual lines, then splitting it by the delimited (in this case commas). Some of the values may have quotes around them, sometimes the values themselves have line breaks. Then there’s the potential that the data itself is malformed. […]

How to display a JavaScript array in HTML

From time to time, I see a search query that made it to my blog that piques my interest and inspires a post. This is one such post. How does one take an array (presumably in JavaScript) and display it in HTML? Now in this day in age, I would […]

Splitting strings and keeping the separator in JavaScript

When splitting strings, the majority of the time, I don’t care about the separator (or delimiter), just the strings that exist between it. In JavaScript, that’s very simply with .split(): 'one,two,three,four,five'.split(','); Which will yield an array of strings like this: ['one', 'two', 'three', 'four', 'five'] Great! But if we want […]

Repeating Strings in JavaScript

While cleaning up some of my old notes today, specifically stuff I jotted down that would make for a good blog topic, I happened upon the topic of repeating strings in JavaScript. Things are really easy in ES6+, but if you happen to still support Internet Explorer for some crazy […]

Overengineering Challenge: Is Number Even or Odd in JavaScript

Code golfing is fun, and makes for great blog posts. The problem is, the world is flooded with posts attempting to show you how to write the best / most concise code imaginable. With that, I decided that it would be a fun challenge to attempt to take a simple […]

How to set the background color of a Google Chart

As luck would have it a Google Chart I had created some time last year, which was looking great for a while, recently decided that the background color of the chart itself was going to switch from the color I had set to the default color, white. This was quite […]

Recursive functions in TypeScript

In working with the Slate framework for building rich text editors recently, I found myself faced with a scenario where I needed to loop through the editor’s value. The value that comes out the editor is an array of objects that is nestable, I assume infinitely, by way of the […]

Error: Cannot resolve a DOM point from Slate point

By way of recommendation (thanks Brewer), I’ve been messing around with Slate.js a ton. Such is life, I did have my fair share of issues, particularly in terms of getting it to function like a chat message input where hitting Enter will submit the form and reset it to it’s […]

Resetting a Slate.js editor to an empty state

I’ve been messing with Slate.js and happen to be using it in the context of having a persistent editor on the page that can be used post from. It’s sort of like a chat’s message input, it’s always there, and when you submit it, it resets back to it’s original […]

Resetting the undo/redo history of a Slate.js editor

Slate.js has some wonderful undo/redo history under the hood, by way of the React hook useMemo. Out of the box is works great, but if you want to reset your editor back to an empty state (in scenarios where the component is persistent on the page), the undo/redo history will […]

Submit on enter except when shift is pressed in JavaScript

Been working on something recently where I wanted to implement a submit handler when a user hits the enter key in an input. That’s not that big of a deal, sniff out the key being pressed, and if it’s the Enter key, go ahead and execute the submit handler. Since […]

Detecting modifier keys being pressed with JavaScript

Been hot on a new [old] project recently and I’ve not only been coding my ass off, but delving into some territory that I haven’t delved into as of late. The latest is in terms of determining if a modifier key has been pressed in conjunction with another key press. […]

Protected routes with React Router v5

React Router makes it extremely easy to define routing in your React application, but out of the box doesn’t concern itself with which routes should require authentication and which ones should now. I’ve solved this problem in a past life, using React Router v4 and looking back at the code […]

Converting integers to Roman numerals with TypeScript

My buddy Robert has been seeking new opportunities in the software development realm, and recently ran into a coding challenge that I haven’t thought about in a good long time: converting an integer to Roman numeral. Now, I actually hate these kinds of brain teasers in the context of interviewing […]

Padding strings and numbers with JavaScript

Programming languages can change pretty regularly, and I try to carve out some time periodically to catch up on the latest and greatest the languages I love have to offer. With that, recently I was poking around on JavaScript’s string prototype and came to realize that there were some string […]

How to remove an array element by index in JavaScript

Recently I was faced with the need to remove an item from an array, by index in JavaScript and I came to realize it had been quite a while since I had to do something like that. My initial, and extremely incorrect thought was that I could just use delete […]

The most efficient way to check the last character of a string with JavaScript

Recently I was writing some code that needed to detect if a certain character was at the end of the string to determine if some additional logic should be executed. The reason for this was because the additional logic to run was pretty hefty and for pre-mature optimization’s sake, I […]

How to check if a string contains another string with JavaScript

Checking whether or not a string contains another string, or just a single character. It’s a been a pretty unavoidable sanity check in my programming career. It’s not quite a problem for the ages, but it comes up regularly and can be approached a few different ways. ES5 and prior […]

Chunking an array in JavaScript

Recently I was working with some data coming out of a Redis pipeline and I wanted to take the data and split it up into an array of X items per key. I was working with PHP, this wouldn’t have been a big deal, just use array_chunk and get on […]

Days until Christmas in JavaScript

Time again for one of my holiday cop out posts :) Here’s a couple of ways to calculate the number of days until Christmas: Vanilla JavaScript Math.floor((+new Date('2018-12-25') - +new Date()) / 86400000) Using momentjs moment('2018-12-25').diff(moment(), 'days') Merry Christmas! […]

Setting defaults for all your SuperAgent requests

SuperAgent is an HTTP request library for both Node.js and client-side JavaScript. It’s our current choice here at CrowdSync for making requests because it’s lightweight and easy to use. Unfortunately, out of the box, it doesn’t support setting defaults on all of your requests. The simplest approach would be to […]

React Draft Wysiwyg with MongoDB

We love open source software here at CrowdSync. Sometimes though, we run into issues with the software we’ve deemed as the “right” package for us. A recent incident has been with React Draft Wysiwyg which we decided to use as part of our action to compose and send emails to […]

How to calculate Mother’s Day in JavaScript

One of my favorite features of PHP is the strtotime function. It takes a string and gives you the equivalent time in return. Unfortunately, there’s not something similar for other languages. There are other ATTEMPTS to duplicate the function, but nothing I’ve ran into that works quite as well. With […]