Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion juration.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,22 @@
throw "juration.stringify(): format cannot be '" + options.format + "', and must be either 'micro', 'short', or 'long'";
}

if((typeof options === 'object' && options.max_units !== undefined) && (options.max_units !== 'years' && options.max_units !== 'months' && options.max_units !== 'days' && options.max_units !== 'hours' && options.max_units !== 'minutes' && options.max_units !== 'seconds')) {
throw "juration.stringify(): max_units cannot be '" + options.max_units + "', and must be either 'years', 'months', 'days', 'hours', 'minutes', or 'seconds'";
}

var defaults = {
format: 'short'
format: 'short',
max_units: 'years'
};

var opts = _extend(defaults, options);

var units = ['years', 'months', 'days', 'hours', 'minutes', 'seconds'], values = [];
var index = units.indexOf(opts.max_units);
units = units.slice(index);
var remaining = seconds;

for(var i = 0, len = units.length; i < len; i++) {
var unit = UNITS[units[i]];
values[i] = Math.floor(remaining / unit.value);
Expand Down
5 changes: 5 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@
raises(function(){juration.stringify(8400, { format: 'horses' });});
});

test("stringify and specify largest unit", function() {
var parsed = juration.stringify(129600, { max_units: 'hours' });
equal(parsed, "36 hrs", "We expect 129600 to be 36 hrs");
});

});

</script>
Expand Down