From e1cfedffc00d8945c210a733b1492952a867ad2f Mon Sep 17 00:00:00 2001 From: Francois Botha Date: Sun, 22 Jun 2014 00:27:53 +0200 Subject: [PATCH 1/3] Add max_units option to stringify, so that the maximum unit is specified --- juration.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/juration.js b/juration.js index 47bd453..58751b0 100644 --- a/juration.js +++ b/juration.js @@ -92,13 +92,21 @@ 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); + for(var i = 0, len = units.length; i < len; i++) { if(i === 0) { values[i] = Math.floor(seconds / UNITS[units[i]].value); From 88c17185cb7ec65a855c8f4ad9b56da24e281de3 Mon Sep 17 00:00:00 2001 From: Francois Botha Date: Sun, 22 Jun 2014 00:27:53 +0200 Subject: [PATCH 2/3] Add max_units option to stringify, so that the maximum unit is specified --- juration.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/juration.js b/juration.js index 3833a9b..d7d6b3c 100644 --- a/juration.js +++ b/juration.js @@ -92,13 +92,20 @@ 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]]; From 2419d1515c2e05c9232aa32cbc6b10e529ed6c73 Mon Sep 17 00:00:00 2001 From: Francois Botha Date: Thu, 25 Sep 2014 13:54:29 +0200 Subject: [PATCH 3/3] Add test case --- test/index.html | 5 +++++ 1 file changed, 5 insertions(+) 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"); + }); + });