Using enum values as strictly typed object keys

In TypeScript, it’s often useful to define interfaces or complex (structured) types whose properties (or keys) may only be values of a previously defined enum type. Here’s a good example: an object declaring a set of buttons for a modal dialog. Instead of this… type DialogButtons = { yes?: boolean, no?: boolean, cancel?: boolean } interface IDialog { buttons: DialogButtons } const dialog: IDialog = { buttons: { yes: true,

Continue reading

Universal getter for plain JS objects.

Many times in JavaScript we have to reach deep into objects to access fields that could possibly not be there at all; for example let’s assume I have a JS object called options into which I’ve just read an entire JSON configuration file. Trying to access a field like options.application.cache.enable directly will potentially fail (e.g. when any of the intermediate levels is missing)… if (options.application.cache.enable) { // do stuff }

Continue reading

Quickly filtering large lists of texts.

Recently, I had to make a mock-up of a filtering function (something I need for a project I work on) so I can show-case it around. And since this mock-up seems to impress some people, I decided to share this function with everybody so others may enjoy the “loot” :). Briefing The function (i.e. filter()) basically receives a query, a large list of text items and (optionally) some options. It

Continue reading

JS classes for the masses.

Following on an older post of mine, I decided to write a new article about an(other) implementation of OOP Classes in JavaScript. This is something that brings a lot of value from very few lines of code and I want to share it with everyone and see how it can be improved. First let’s see the code (you should check here for the latest version) and then I’ll try and

Continue reading

Lua-TTyrant: a TokyoTyrant binding for Lua.

Some time ago I started a small project to implement a C-to-Lua binding of the TokyoTyrant library, called “lua-ttyrant“. A few days ago, while working with a script that was actually using it, I decided to polish it up, finish it and eventually submit it to be included in the LuaRocks repositories. Given the fact that the “rock” was just accepted by the LuaRocks team, I thought a new post

Continue reading

A simple(r) approach for class-based OOP in JavaScript.

Like others before me, I found JavaScript’s prototype-based OOP support – while very powerful – quite cumbersome in some situations where I needed a certain structure or hierarchy of objects, situations in which one quickly comes to the conclusion that the best answer would be the concept of a Class. However, while I am well aware that the web is filled with various implementations of classical (i.e. class-based) object orientation

Continue reading

Command-line option parser in JavaScript.

Here’s a small but very effective command-line option parser written in JavaScript (uses NodeJs). You can get the up-to-date source code at http://gist.github.com/982499. I wrote this because I needed it for a project and I did not think it was worth it to install some npm package just for this. The comment should explain most things you will need but ask me if you don’t understand something. Note that this

Continue reading

Case study: fixed number of iterations with LPeg

Words seem to fail to describe just how awesome LPeg is. Designed as a Lua implementation of the PEG concept, it is a true programming gem! Please, if you dont’t know what it is, take some time to familiarize yourself with it! It’s not the easiest thing to grasp, but you will not regret it! It is certainly one of the most worthwhile learning efforts you can make in generic

Continue reading

Lua2C, an updated version

I know I have been “missing in action” lately but I am working furiously, and I seem to have too little time for my blog (very sad face). But, just for a breath of fresh air, I thought I’d share something with the world. Entering lua2c.lua Lately I became quite interested in Lua (a lot actually). It has phenomenal speed, exceptional interfacing with C and some features and libraries that

Continue reading