Recently I began updating TidyFork to be written in Aurelia 2. As such, I took the opportunity to change how the repositories were loaded in the UI. There are numerous Octokit libraries provided by GitHub which let you make requests to the GitHub API.
Using the request.js package, I make calls to the GitHub API. I also use the Parse Link Header package to parse the pagination headers that GitHub returns on its API responses.
Working with dates in 2020 are still a mess. Presumably, they’ll also be a mess to work within 2021, 2022 and for a good while after that. Many (myself included) reach for a date library to fill the gaps.
For years Moment.js reigned supreme and for good reason, it can do everything with dates, including working with different time zones. Unfortunately, Moment can also result in bundle bloat.
And then came along date-fns. It could do almost everything Moment did and was good enough for 95% of all date and time related use cases. However, depending on the task, you can still use native vanilla Javascript to work with dates.
I do quite a lot of work with Firebase and when you are working with authentication claims, they will be returned as an object containing your simple values (usually booleans).
Thankfully, since ES2015 landed, each Javascript release has introduced a new and easier way to work with objects and convert them into arrays. You don’t need any libraries like Lodash, this is all native and well-supported vanilla Javascript.
To convert an object of properties and values, you can use Object.entries however, it will return an array of arrays, which isn’t terrible but might confuse Javascript novices as to how you would even work with these.
Feature flags are a great way to prevent stale branches by regularly shipping features in your code without officially enabling them. A feature flag let’s you turn code on and off, in the case of features, a feature flag means you can regularly merge branches and release them.
While there are many different ways you can approach this, one of my favourite and most simple approaches is a features.json file in your application.
Recently whilst working on my Aurelia 2 book, for the example application where you checkout I needed to add in the ability to provide a card number and when it saves, the last 4 digits of the card number get saved.
This is one of those things that as developers we won’t do too often, but it’s easy using substr to trim a text string to a certain length.
let ccNumber = '4560265043620583'; let lastFourDigits = ccNumber.substr(-4); By providing -4 as the value to substr you’re telling it to take the last 4 characters from the string. The result from our example will be 0583. You can also provide non-negative numbers to go from the beginning as well.
I used to be vehemently against using `reduce` in Javascript. It wasn’t because I thought there was a better way, I actually think the functional programming purists and their aggressive approach to advocacy are what really turned me off. That and those who advocate for using it over a basic for loop as well.
Now, there are instances where I truly believe that filter, map and reduce should not be used. If you’re just wanting to quickly iterate over a collection of values, in many instances. a for..loop will beat all of those aforementioned methods in performance every single time. And if you’re dealing with large collections of items, you will notice.
Recently whilst working on some blockchain specific code, I needed to write a random number generator to simulate generating a range of numbers, except it needed to be predictable. The same input values produce the same output when provided.
In my case, I used values from immutable blockchain data such as the blockchain number, reference block number and transaction ID. I then allow the user to provide a client seed and generate a unique server seed on each roll as well.
Whilst doing some work in a library I maintain, I needed to add in the ability to calculate the hash of an included file for an integrity check feature I was adding in. The resulting solution is simple and not boast-worthy, but given others might encounter a situation where they need a hash of a file, this might help.
We use the fs module to open up the file we want to calculate the hash for, use the createHash method on the crypto package to then pass in our file buffer from the readFileSync method, and that’s it.
Every so often thought-pieces will go around proclaiming that you are writing code the wrong way and that you should be writing your code this way instead.
One such opinion I have seen (and will not link because this isn’t a takedown) is recommending the use of Ternary Operators in Javascript over if statements.
While ternaries can make your code look cleaner in some cases by replacing multi-line if statements with one-liners, there are instances where they fall apart quite quickly. Ternary operators exist in all programming languages and the problems they can introduce into a codebase are universal.
Here is a nice bug-not-bug to close out in 2019. One of my Trello cards detailed what sounded like an error:
When toggling between two options (yes and no) in a dropdown, entering “y” changes to yes and quickly entering “n” does not switch to no. However, waiting a second you can change between them.
Some initial debugging suggested this was not actually a bug in our application. But, I knew if I was going to get the ticket closed off as not a bug, I had to have an explanation.