Pages

Tuesday, May 17, 2016

Learning Meteor.js [Part 20]: A shout out to fraction.js

This post to thank Erik Garrison for his fraction.js library. Ingredients in recipes are often expressed in fraction, like 'half a pound of pork' or something. Thanks to this library I was able to easily display the data that I had in floating point number as fractions.

After adding the fractional.js package, expose a helper to do the conversion and call it from the view's markup.
import fractional from 'fractional';

Template.registerHelper('fraction', function(num) {
    return new fractional.Fraction(num).toString();
});

That is all, check out the docs for the full API that is available.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.