Tuesday 11 November 2014

getSeconds

This is ridiculously useful:
function getSeconds(str) {
  var seconds = 0;
  var days = str.match(/(\d+)\s*d/);
  var hours = str.match(/(\d+)\s*h/);
  var minutes = str.match(/(\d+)\s*m/);
  if (days) { seconds += parseInt(days[1])*86400; }
  if (hours) { seconds += parseInt(hours[1])*3600; }
  if (minutes) { seconds += parseInt(minutes[1])*60; }
  return seconds;
}

Tuesday 4 November 2014

Ordering Objects in an Object for ngRepeat

I'm dipping my toes into the world of Angular and diving into the world of Firebase - I love their philosophy about everything being an object! That does mean, though, that I'm left trying to figure out how to ng-repeat a set of objects in another object (the mind boggles!), thankfully I came across this post: AngularJS Filter for Ordering Objects (Associative Arrays or Hashes) with ngRepeat by Justin Klemm and it got me working on something like this:

app.filter('orderObjectByUKDate', function() {
    return function(items, field, reverse) {
        var filtered = [];
        angular.forEach(items, function(item) {
            filtered.push(item);
        });
        filtered.sort(function (a, b) {
            return (parseInt(moment(a[field], "DD/MM/YYYY").format("X"), 10) > parseInt(moment(b[field], "DD/MM/YYYY").format("X"), 10) ? 1 : -1);
        });
        if(reverse) filtered.reverse();
        return filtered;
    };
});

It's only a little bit like this as I've got to AES decrypt the date first, but you get the idea I guess. Then it's simply a question of using it like this:

ng-repeat="thing in things | orderObjectByUKDate:'when':true"

Saturday 1 November 2014

150th donation!

This came in the post this morning. Couldn't be more pleased :-)

Still, shame they stopped giving out whiskey glasses - I've only got one left - but I guess this is better for my health!