diff --git a/juration.js b/juration.js index 3833a9b..da19bec 100644 --- a/juration.js +++ b/juration.js @@ -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); diff --git a/test/index.html b/test/index.html index 1dae4db..1546941 100644 --- a/test/index.html +++ b/test/index.html @@ -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"); + }); + });