Skip to content

Instantly share code, notes, and snippets.

@andresmatasuarez
Created March 20, 2015 19:12
Show Gist options
  • Save andresmatasuarez/9552b61271da7ca9289d to your computer and use it in GitHub Desktop.
Save andresmatasuarez/9552b61271da7ca9289d to your computer and use it in GitHub Desktop.
NodeJS | Module: HumanizeDuration | Humanizes MomentJS duration object up to 'week' unit
/**
* // USAGE
* var humanizer = new HumanizeDuration();
*
* humanizer.humanize(10080, 'minutes' ); // Returns: '1 week'
* humanizer.humanize(21344, 'minutes' ); // Returns: '2 weeks, 19 hours, 44 minutes'
* humanizer.humanize(2101080000, 'milliseconds'); // Returns: '3 weeks, 3 days, 7 hours, 38 minutes'
* humanizer.humanize(59, 'days' ); // Returns: '8 weeks, 3 days'
* humanizer.humanize(211777, 'seconds' ); // Returns: '2 days, 10 hours, 49 minutes, 37 seconds'
* humanizer.humanize(10080, 'hours' ); // Returns: '60 weeks'
*
* humanizer.humanize(10080, 'minutes', '-week'); // Returns: ''
* humanizer.humanize(10080, 'minutes', '-week +day +millisecond'); // Returns: '0 days, 0 milliseconds'
* humanizer.humanize(211777, 'seconds', '-hour +week'); // Returns: '0 week, 2 days, 49 minutes, 37 seconds'
*
*/
'use strict';
var MILLISECOND = 'millisecond';
var MILLISECONDS = 'milliseconds';
var SECOND = 'second';
var SECONDS = 'seconds';
var MINUTE = 'minute';
var MINUTES = 'minutes';
var HOUR = 'hour';
var HOURS = 'hours';
var DAY = 'day';
var DAYS = 'days';
var WEEK = 'week';
var WEEKS = 'weeks';
var _ = require('lodash');
var moment = require('moment');
var getValue = function(duration, unit){
switch(unit){
case 'w':
case 'week':
case 'weeks':
return Math.floor(duration.as(unit));
case 'd':
case 'day':
case 'days':
return Math.floor(duration.as(unit) % 7);
default:
return duration.get(unit);
}
};
var shouldInclude = function(duration, fields, unit){
var value = getValue(duration, unit);
return _.includes(fields, '+' + unit) || (!_.includes(fields, '-' + unit) && value !== 0);
};
var valueString = function(duration, labels, unit){
var value = getValue(duration, unit);
return value + ' ' + labels[value === 1 ? 'single' : 'plural'][unit];
};
function HumanizeDuration(labels){
this.labels = _.merge({
single: {
millisecond : MILLISECOND,
second : SECOND,
minute : MINUTE,
hour : HOUR,
day : DAY,
week : WEEK
},
plural: {
millisecond : MILLISECONDS,
second : SECONDS,
minute : MINUTES,
hour : HOURS,
day : DAYS,
week : WEEKS
}
}, labels);
}
HumanizeDuration.prototype.humanize = function(value, unit, fields){
fields = _.isEmpty(fields) ? [] : (_.isArray(fields) ? fields : fields.split(' '));
var duration = moment.duration(value, unit);
var strings = [];
if (shouldInclude(duration, fields, 'week' )){ strings.push(valueString(duration, this.labels, 'week' )); }
if (shouldInclude(duration, fields, 'day' )){ strings.push(valueString(duration, this.labels, 'day' )); }
if (shouldInclude(duration, fields, 'hour' )){ strings.push(valueString(duration, this.labels, 'hour' )); }
if (shouldInclude(duration, fields, 'minute' )){ strings.push(valueString(duration, this.labels, 'minute' )); }
if (shouldInclude(duration, fields, 'second' )){ strings.push(valueString(duration, this.labels, 'second' )); }
if (shouldInclude(duration, fields, 'millisecond')){ strings.push(valueString(duration, this.labels, 'millisecond')); }
return strings.join(', ');
};
module.exports = HumanizeDuration;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment