0

I have this sample code(a .js library needs to be consumed) for formatting the date. Have never used partial functions before so not quite sure how to use it. How does one call them? Here is the utility library code:

 define(function() {
    'use strict';

     function _formatDate(justNowText, todayFormat, thisWeekFormat, thisYearFormat, veryOldFormat, date) {
       if (!date) { return ""; }

         var formattedMoment = moment(date);
         var now = moment();

       if (now.diff(formattedMoment, "minutes") < 15) {
         return justNowText || "Just now";
       }

       var today = now.startOf('day');
       var dateFormat;

       if (today <= formattedMoment) {
         dateFormat = todayFormat;
       } else if (now.diff(formattedMoment, "days") < 7) {
         dateFormat = thisWeekFormat;
       } else if (formattedMoment.year() >= now.year()) {
         dateFormat = thisYearFormat;
       } else {
         dateFormat = veryOldFormat;
       }

      return formattedMoment.format(dateFormat);

    }

    function asShortTimeStampFilter(gettext) {
     return _.partial(_formatDate,
       gettext("Just now"),
       "h:mm A",
       "ddd",
       "MMM D",
       "M/D/YY"
     );
   }

  function asMediumTimeStampFilter(gettext) {
    return _.partial(_formatDate,
      gettext("Just now"),
      "[" + gettext("Today at") + "] h:mm a",
      "ddd h:mm a",_formatDate
      "MMM D h:mm a",
      "M/D/YYYY h:mm a"
    );
  }

  function asDateTimeFormatFilter(gettext) {
    return function(date, format) {
      if (!date) { return; }

      return moment(date).format(format);
    };
  }

  return {
    asShortTimeStamp: ['gettext', asShortTimeStampFilter],
    asMediumTimeStamp: ['gettext', asMediumTimeStampFilter],
    asDateTimeFormat: ['gettext', asDateTimeFormatFilter]
  };
});
duplode
  • 33,731
  • 7
  • 79
  • 150
Surily
  • 121
  • 2
  • 10

1 Answers1

3

Partial application means that you would pass the first argument only to get a function back, which you then could call (multiple times) with the final arguments.

In your case, the library expects a text getter function (e.g. for localisation), and returns the actual formatter function to which you'd pass the date.

function id(x) { return x } // just echo the input
var shortTimeStamp = library.asShortTimeStamp[1](id);

console.log(shortTimeStamp(Date.now()))
console.log(shortTimeStamp(Date.now() - 30000)) // 30s ago
console.log(shortTimeStamp(Date.now() - 720000)) // 2h ago

var mediumGermanTimeStamp = library.asMediumTimeStamp[1](function(t) {
    return {"Just now":"Grad vorhin", "Today at":"Heut um"}[t];
});

console.log(mediumGermanTimeStamp(Date.now()))
console.log(mediumGermanTimeStamp(Date.now() - 30000)) // 30s ago
console.log(mediumGermanTimeStamp(Date.now() - 720000)) // 2h ago
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • @bergi..thanks for the reply..whats the id for? sorry bit unclear – Surily Dec 11 '14 at 08:59
  • `id` is the identity function. When it's called with the string `"Just now"`, it will just return that - the "default" localisation so to say. – Bergi Dec 11 '14 at 09:01
  • i tried logging the last 3 lines of your cod..it just returns 'Grad vorhin' each time. – Surily Dec 11 '14 at 09:06
  • sorry my 'Just Now' logic is built for under 15 mins..therefore it printed repeatedly. Thanks – Surily Dec 11 '14 at 09:09
  • Yeah, the last one should use the "todayFormat" – Bergi Dec 11 '14 at 09:11
  • why do i have to pass [1] even if i change the function name? – Surily Dec 11 '14 at 09:52
  • It does not make any sense to me, but that utility library exports an objects whose members are *arrays* whose second item is the function. – Bergi Dec 11 '14 at 09:57