I wanted to write my own date library. And I wanted it to be easy to use. And use it with another app I was writing. There is no date library to find unique dates such as the second Tuesday of November for the next five years.
I also wanted to build my own library and publish it.
Javascript
Rollup
Jest
Node
Rollup makes it easy to create a UMD, library ready package.
The Nullish Coalescing Operator (??) is very handy for default settings. It checks for null, undefined and zero. The zero value tricks developers all the time, it's so easy to make mistake because it's falsy.
const default2 = { days: settings.days ?? 5, title: settings.title ?? "No title", subtitle: settings.subtitle ?? "No subtitle" }
If settings.days is 0, the Nullish Coalescing Operator will give the correct answer - days will be 0. But with a ternary operator it would incorrectly give 5 as the result.
BlueMoon has a feature to get a specific date eg to return a date exactly a month from now would be:
const oneMonthFromNow = { month: "+1" }
If I was to put:
const oneMonthFromNow = { month: +1 }
This would return absolute month one. To distinguish between absolute 1 and relative add 1 in an object, quotes are needed. The parser makes it { month: 1 }. Even .toString() can't recover the +. So put it in a string if you want to preserve the +. Relative mode must be in quotes.
I learnt about Jest unit testing. Really good unit testing for custom functions is especially important. Once the custom function is written, and while writing, develop unit tests and always have tests for edge cases. Especially for a project like this. And it's important to propogate around all the options and structure tests in a logical way.
Creating a library like this in UMD format is much easier to do in Rollup than Webpack.
I learnt about Luxon and some other date libraries and how Blue Moon is different.
Coming up with a good syntax to get the dates the user wants took some brainstorming and user testing. I started off with a messy syntax. If you want to generate a report for last week Monday to Sunday, it would have been dMonw-1 to Sunw-1. After asking people and doing some preliminary testing, I abandoned that and chose an object. let startOfWeek = { day: "Mon" }. Javascript developers are very familiar with objects so it made a lot of sense. Instead of "d" for days and d10m10 for 10/10/<current year>, I decided to make it very intuitive, eg So end of next month would be:
const endOfNextMonth = { day: "monthend", month: "+1" }