From 7c36ff8331d4544d6547845a3bee88a32bf30fa8 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 13 Feb 2016 22:54:07 -0500 Subject: [PATCH] Introduce "date" helper. --- dist/helpers/generic-helpers.js | 26 +++++++++++++++++++++++++- src/helpers/generic-helpers.coffee | 16 +++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/dist/helpers/generic-helpers.js b/dist/helpers/generic-helpers.js index 6dcebd7..cd29267 100644 --- a/dist/helpers/generic-helpers.js +++ b/dist/helpers/generic-helpers.js @@ -57,6 +57,30 @@ Generic template helper definitions for HackMyResume / FluentCV. return datetime || (typeof fallback === 'string' ? fallback : (fallback === true ? 'Present' : null)); }, + /** Display a formatted date. */ + date: function(dateValue, dateFormat, dateDefault) { + var dateValueMoment, dateValueSafe, reserved; + if (arguments.length < 4 || !dateDefault || !String.is(dateDefault)) { + dateDefault = 'Current'; + } + if (arguments.length < 3 || !dateFormat || !String.is(dateFormat)) { + dateFormat = 'YYYY-MM'; + } + if (!dateValue) { + return dateDefault; + } + reserved = ['current', 'present', 'now']; + dateValueSafe = dateValue.trim().toLowerCase(); + if (_.contains(reserved, dateValueSafe)) { + return dateValue; + } + dateValueMoment = moment(dateValue, dateFormat); + if (dateValueMoment.isValid()) { + return dateValueMoment.format(dateFormat); + } + return dateValue; + }, + /** Given a resume sub-object with a start/end date, format a representation of the date range. @@ -580,7 +604,7 @@ Generic template helper definitions for HackMyResume / FluentCV. dateFrom = dateTemp.format(fmt); } if (_.contains(reserved, dateBTrim)) { - dateTo = fallback || 'Current'; + dateTo = fallback || 'Present'; } else { dateTemp = FluentDate.fmt(dateB); dateTo = dateTemp.format(fmt); diff --git a/src/helpers/generic-helpers.coffee b/src/helpers/generic-helpers.coffee index 5930985..c31f43f 100644 --- a/src/helpers/generic-helpers.coffee +++ b/src/helpers/generic-helpers.coffee @@ -47,6 +47,20 @@ GenericHelpers = module.exports = + ###* Display a formatted date. ### + date: (dateValue, dateFormat, dateDefault) -> + dateDefault = 'Current' if arguments.length < 4 or !dateDefault or !String.is dateDefault + dateFormat = 'YYYY-MM' if arguments.length < 3 or !dateFormat or !String.is dateFormat + return dateDefault if !dateValue + reserved = ['current', 'present', 'now']; + dateValueSafe = dateValue.trim().toLowerCase(); + return dateValue if _.contains reserved, dateValueSafe + dateValueMoment = moment dateValue, dateFormat + return dateValueMoment.format dateFormat if dateValueMoment.isValid() + dateValue + + + ###* Given a resume sub-object with a start/end date, format a representation of the date range. @@ -542,7 +556,7 @@ _fromTo = ( dateA, dateB, fmt, sep, fallback ) -> dateFrom = dateTemp.format( fmt ) if _.contains( reserved, dateBTrim ) - dateTo = fallback || 'Current' + dateTo = fallback || 'Present' else dateTemp = FluentDate.fmt( dateB ) dateTo = dateTemp.format( fmt )