Checking whether a file/directory exists without using fs.exists

Anyone who has spent any time writing Node has most likely used the file system module. One of the methods in this core module though is not only different from all the others, but according to most experts is not even useful at all, despite its popularity with a host of other developers. Because of this, the method has been deprecated for years now. I talk, of course, about fs.exists (and fs.existsSync).

For some people, it’s a very contentious issue - see for example this issue thread or this one. The reason for its deprecation (way back in the iojs days of yore) is that, unlike all other file system methods, it doesn’t use the canonical nodeback API. Furthermore, its main use case scenario, checking whether a file exists before opening, is actually an antipattern and should be replaced by just trying to open the file and possibly handling the error.

node logo

But despite its long-standing (in JS terms at least) deprecation, tons of people still continue to use fs.exists, and some newcomers or relative newcomers are surely confused about what to use instead. At least, I myself was when I first ran into this issue.

While I agree that the method is often superfluous, and sometimes entirely pointless (checking whether a file exists before doing something with that file is not helpful when race conditions may occur and the file may be deleted in the interim), I and I am sure many others would argue that there are legitimate use cases for this method, or at least for what this method should have been, had it been designed better.

So if you are looking for a replacement for fs.exists(Sync), read on, for what follows is a short discussion of how to check whether a file or directory exists without using this deprecated method.

Read More

Writing a Fibonacci implementation in JavaScript

I was just looking through some of the notes I’d taken a few months ago, and was inspired to write a quick post on how to write a good JavaScript solution for finding the nth number in a Fibonacci sequence.

I’m sure most of this information came right from some other blog or book, so credit really goes to wherever I originally learned these insights, but sadly I didn’t include sources in my notes. Just google around if you want to know more though, because it’s all over the internet, really.

Just in case you’ve never heard of the Fibonacci sequence, it’s a mathematical sequence where every number is the sum of the previous two numbers in the sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Or, equally common:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

Now, I’m not going to go over the importance of this sequence, not only for computer science but for all of the natural world (but trust that it is). All you need to know is that “find the nth Fibonacci number” is a very common interview question for programmers of all persuasions, including the JavaScript kind.

Read More

Back from holiday and how to use dead time

It’s been while since my last activity here. The reason is I’ve been out visiting my parents across the English Channel, hiking with my girlfriend in German-speaking Belgium, and then I topped it all off with a long weekend trip with friends in the sunny south of Spain.

It was my original intention to keep working and posting while abroad, but while there I ended up deciding against it, partly due to just being too tired at the end of every day, and partly to give my mind a rest from JS on my time away from the PC – though I did lug around a Chromebook with SSH access to a dev server everywhere I went.

So I’ve not been coding much for over two weeks now and I feel a little rusty. After I submit this post, I will gather my thoughts and once again attack my now neglected to-do list. Said list will need a few more tasks added first though, because I discovered that travel-time abroad, while not good for doing actual sit-down work, is actually great for other things important to a budding coder.

Read More

markdown2troff: regular expressions ftw

I published another npm module on GitHub and npm. It’s a simple converter that takes markdown as an input and spits out troff.

For those not in the know, troff (and its GNU implementation, groff) is a somewhat arcane text format from the seventies, still used today in linux man pages - and maybe other things? It’s not quite the most pleasant format to work with, but it does the job well, which is why we still have it around.

markdown -> troff conversion was already possible in node with remark-man. This package works by first creating an abstract syntax tree (AST) from the markdown and using that as a base for manipulation or conversion to other formats.

Now, if you want the best (as in, most accurate) possible conversion from markdown to troff, I can’t think of a better approach than the one taken by remark-man, because you are guaranteed to have a perfect representation of the source (inasmuch as it is possible to represent markdown syntax in troff of course).

However, this approach is rather involved, which I noticed when adding the package as a dependency to my arch-wiki-man project. I wanted a much lighter solution and was willing to give up a little accuracy in conversion to achieve it.

So I got to work and made markdown2troff. It uses nothing but regular expressions (well, and JavaScript of course) to go from source to output so it’s blazing fast and has no dependencies at all and it currently handles all conversion except for tables and some special characters.

I wrote it in a hurry and with a very specific purpose in mind so right now I admit it has a bit of a spaghetti-code vibe to it and I haven’t added any tests. When I get some time off from being ill later, I’ll go back and make it nicer to look at.

But as far as I can see, it works and does almost exactly what I want. If you need a markdown -> troff converter in your NodeJS (though it’s easily portable to other languages) project, consider my package. And if you do, also consider contributing and making it better! Thanks for reading :-)

awman 1.1.1: AUR, multi-language support, better formatting and more

It’s been a while since my last update. Some of that is due to falling ill yet again. The rest is because my arch-wiki-man/awman project turned out to be quite a bit more in demand than I thought it would be.

My cautious announcement about it was greeted by a pretty enthusiastic response, a number of questions, a few feature requests and even a bug report. As someone who is still relatively new to programming, I have to say I felt rather overwhelmed - and overjoyed!

Just in case some of you are interested, here’s what’s changed in the past week and a half:

Read More