A common pitfall with the constructor of Javascript’s Date object. It uses a base 0 for monthIndex parameter, but base 1 for others.

Photo by Charles Deluvio on Unsplash

So I needed to perform some date operations in Javascript, let’s take 8th March 2021 for this example. It was a sunny ☀️ day here in the UK. And it was International Women’s Day, which was a big deal in many places. Anyway’s let’s get back to Javascript.

I needed to convert the date to a Date object in Javascript so I could do things like calculating the day of the week, or how many days have passed since then.

Javascript has four constructors for the Date object:

new Date()
new Date(value)
new Date(dateString)
new Date(year, monthIndex [, day [, hours [, minutes…

The default behaviour of Array.prototype.sort() in Javascript may not be what you expect if no compare function is provided.

Photo by Faris Mohammed on Unsplash

I needed to sort an array of integers in Javascript in ascending order recently and reached for Array.prototype.sort().

const array = [0, 2, 1, -2, -1]
//expected result: [-2, -1, 0, 1, 2]

I have used this function many times over and have never encountered any problem. I am aware that you can pass in a compare function as an optional parameter to customise the sorting behaviour, i.e. array.sort([compareFunction]). But for plain old sorting integers ascendingly, the assumption was it was an overkill.

How wrong I was 🤦‍♂! The value of array after being sorted was:

[ -1, -2…

Array is one of the most important and elegant features in Javascript. Developers, especially those coming from statically typed languages like Java or C# tend to overlook the built-in methods of Javascript’s array.

Photo by Junsheng Chen on Unsplash

There are two main ways to create an array in Javascript.

Using Array class:

let a = new Array
let b = new Array(1, 2, 3)

and using [] syntax:

let c = []
let d = ['a', 'b', 'c']

Javascript is a dynamically typed language, so each item in the array can be of a different type. Think of it as an ArrayList in Java, or List<object> in C#.

let a = [1, '🐓', '🥚', () => {}]
console.log(typeof a[0]) //number
console.log(typeof a[1]) //string
console.log(typeof a[2]) //string
console.log(typeof a[3]) //function

Four ways to loop through an array

There are more ways to traverse an array than…

An explanation about how AWS resources can be tagged using the Serverless framework. Code sample is available in GitHub.

Resources tagging is an important mechanism in the AWS ecosystem. They can be used for DevOps automation and cost allocation, among other things. A tag is a pair of key and value that can assigned to most resources. A resource can have a maximum of 50 tags. AWS automatically assigns some tags with aws: prefix.

Whilst you work with the Serverless framework to develop a solution for AWS, the framework provisions the resources on your behalf. Some of those resources are provisioned explicitly, i.e. as a result of CloudFormation script inside the resources section of serverless.yml. Other resources are provisioned…

In AWS CloudFormation, all resource types have a DeletionPolicy attribute with possible values of Delete, Retain and Snapshot. Setting it to Retain would save the resource from being deleted by mistake.

If no deletion policy is specified for a resource, the default value is Delete. It means the resource will be removed as part of the CloudFormation stack removal.

For resources with a Retain deletion policy, when the stack is deleted, AWS CloudFormation leaves the resource without deleting it.

However, it does NOT stop the resource from being deleted directly:

  • Programmatically, i.e. using AWS CLI tool.
  • Manually, i.e. using the AWS web console.

With the DeletionPolicy attribute you can preserve or (in some cases) backup a resource when its stack is deleted. Resources that support Snapshot deletion policy include:

Solutions to install any versions of Node.js on a RedHat-based Linux, including RedHat, Fedora, Oracle Linux and Centos.

Installing latest version of a specific Node.js stream

The latest Long-Term-Support (LTS) version of Node.js can be installed to RedHat-based Linux using the dnf package manager:

dnf module -y install nodejs:<stream>

where <stream> corresponds a major LTS version of Node.js, e.g. 10, 12 or 14. You can list the available streams using:

dnf module list nodejs

This method will only allow you to install the latest LTS versions, e.g. at the time of writing

  • v8.17.0 for Node.js 8
  • v10.23.1 for Node.js 10
  • v12.20.1 for Node.js 12
  • v14.15.4 for Node.js 14

If you want to install a specific version of Node.js, you need to be more involved.

Installing any Node.js version

This is…

Run this script on the terminal:

node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

This script will generate a random 64 bit ASCII string, e.g. that can be used for encrypting JWT tokens.

Let’s explain the magic:

  1. node -e tells Node.js to evaluate a script, in this case, a Javascript string
  2. crypto is the cryptographic module forming part of Node.js core. It is already installed as part of Node.js, no extra npm package is involved.
  3. randomBytes() is a function that generates cryptographically strong pseudo-random data. It will return a Buffer object.
  4. toString() is a method of the Buffer class that decodes the object to a…

Let’s talk quickly about what it is. According to wikipedia:

lint, or a linter, is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs.

Its biggest benefit is to remove the opinionated personal preferences from codebases with multiple contributors. A function written by a developer should looks similar to another one written by someone else. It ensures homogeneity which in turn make the repository easier to maintain.

Code linting is important for any projects that are more than the trivial hello world example.

The the Node.js ecosystem, ESLint is by far and away…

This article describes a journey to make a Node.js program inserting ~1.7M records to DynamoDB faster, reducing the running time from the initial ~52 hours down to ~5 minutes. The source code for this program is available here.

Problem to solve

As part of an on-going project, I need to create a look-up database of geo locations for all UK postcodes which number ~1.7M records. Being a key-value data store that can scale horizontally on demand, AWS DynamoDB is the perfect candidate for the use case.

Information about each postcode is stored as an object in DynamoDB, with postcode being the key and…

Stream is a powerful concept but it’s not widely understood by developers.

I have found myself googling and learning the concept again every time I needed to work with streams in Node.js. This article is very much to remind my future self.

This article focuses on applying streams with text content. Code in this article is available in this GitHub repo.

Why streams?

A few cases where streams really shine:

  • Processing large files that do not fit into computer’s memory. Using streams allows you to read a file and process each chunk as the data arrive into your program.
  • All the input…

Quynh Nguyen

Software developer

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