Friday, February 6, 2015

Using underscore.js to simplify a JavaScript code

I'd like to show how using underscore.js library can simplify a JavaScript code.

This is the original code:

It wasn't tested, so I wrote some tests before start refactoring it:

Once I had the tests in place, I separated the responsibility of converting strings into dates from the traversing of the object:

and extracted it to another factory:

that I could directly test: Finally I used underscore.js library to simplify all the JavaScript plumbing that was happening in the object traversing inside DateStringsInsideObjectsIntoDates convert function: By separating the traversing and conversion responsibilities and by using underscore.js, I managed to get to a much more readable version of the code.

No comments:

Post a Comment