Read my book

I wrote books about Webpack and React. Check them out!

Tuesday, August 23, 2011

New JavaScript Libraries - colorjs + bunit.js

After a long while of doing some real work I've been able to spend some time on my own, personal development. As I happen to like to have nice tools at my disposal whenever the time comes, I've focused on tools. I still have some ideas to implement. These two should provide a nice start, though.

In this post I'm going to introduce two new libraries of mine: colorjs and bunit.js. The former provides a simple color abstraction while the latter one is handy for testing.

colorjs


I've used personally a terrible hack based on Stoyan Stefanov's RGB color parser. The code itself is fine for basic usage. I rather not expand it further, though.

Based on the experience gained from my hacked together version I decided to create a new library with a clean API suitable for my little purposes. In the process I ended up implementing a testing library, bunit.js. More on that later.

colorjs provides a minimal API that may be used to instantiate new colors based on given definition (ie. color name, hex value, float values), get/set individual color channels, perform conversions (toArray, toHex, toCSS, to another color system). In addition it provides a chaining API (color.r(0.3).g(0.4)) for setters. Getters and setters use an uniform API (ie. color.r() gets while color.r(0.4) sets).

If that sounded like fun to you, check out the project page. The library is available under a permissive MIT license (retain attribution!). Further ideas for development are appreciated as always. I'll likely develop this one based on my own needs but I'm willing to implement features you might have in mind.

bunit.js


As mentioned before bunit.js is something that was born out of colorjs development. It is largely based on chaining assert and runner concepts I developed earlier this year. In addition it provides a neat API for defining and running test suites in web browser.

I guess the big feature for me in addition to light syntax is the ability to run tests continuously in your web browser. This makes it possible to refactor without mercy. If you mess up something, you'll know about it instantly. Highly useful!

Note that this is something I implemented just because I could not get LiveReload work properly. The application makes it possible to reload the contents of your browser automatically on file change. Essentially it does what I do but in a lot more sophisticated way.

Just like colorjs also bunit.js is also available under MIT license. The project page contains more accurate description and some code snippets to get started. You'll likely want to inspect both projects to get a better idea of how to use it in practice.

Conclusion


I think I'll benefit quite a bit from colorjs and bunit.js in the future. It's fun sometimes just to spend some time on tool development. Nothing's more frustrating that having to work with inadequate tools. :)