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 […
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]
array.sort()
//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…
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
There are more ways to traverse an array than…
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…
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:
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:
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
If you want to install a specific version of Node.js, you need to be more involved.
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:
node -e
tells Node.js to evaluate a script, in this case, a Javascript stringcrypto
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.randomBytes()
is a function that generates cryptographically strong pseudo-random data. It will return a Buffer object.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.
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.
A few cases where streams really shine:
Software developer