From 27c7a0264a340e3c23c96e5093b294a526ec7c9a Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 30 Jan 2016 20:06:04 -0500 Subject: [PATCH] Improve date handling. --- dist/core/abstract-resume.js | 2 +- dist/core/fluent-date.js | 30 +----------- dist/verbs/validate.js | 1 - src/core/abstract-resume.coffee | 11 ++--- src/core/fluent-date.coffee | 11 +---- src/verbs/validate.coffee | 4 +- test/scripts/test-dates.js | 81 +++++++++++++++++++++++++++++++++ 7 files changed, 90 insertions(+), 50 deletions(-) create mode 100644 test/scripts/test-dates.js diff --git a/dist/core/abstract-resume.js b/dist/core/abstract-resume.js index 4955970..00ec0d1 100644 --- a/dist/core/abstract-resume.js +++ b/dist/core/abstract-resume.js @@ -29,7 +29,7 @@ Definition of the AbstractResume class. AbstractResume.prototype.duration = function(collKey, startKey, endKey, unit) { var firstDate, hist, lastDate, new_e; unit = unit || 'years'; - hist = hist || __.get(this, collKey); + hist = __.get(this, collKey); if (!hist || !hist.length) { return 0; } diff --git a/dist/core/fluent-date.js b/dist/core/fluent-date.js index 10ff22f..971df34 100644 --- a/dist/core/fluent-date.js +++ b/dist/core/fluent-date.js @@ -60,7 +60,7 @@ The HackMyResume date representation. module.exports = FluentDate; FluentDate.fmt = function(dt, throws) { - var defTime, month, mt, parts, ref, temp; + var month, mt, parts, ref, temp; throws = (throws === void 0 || throws === null) || throws; if (typeof dt === 'string' || dt instanceof String) { dt = dt.toLowerCase().trim(); @@ -78,33 +78,7 @@ The HackMyResume date representation. } else if (/^\s*\d{4}\s*$/.test(dt)) { return moment(dt, 'YYYY'); } else if (/^\s*$/.test(dt)) { - defTime = { - isNull: true, - isBefore: function(other) { - if (other && !other.isNull) { - return true; - } else { - return false; - } - }, - isAfter: function(other) { - if (other && !other.isNull) { - return false; - } else { - return false; - } - }, - unix: function() { - return 0; - }, - format: function() { - return ''; - }, - diff: function() { - return 0; - } - }; - return defTime; + return moment(); } else { mt = moment(dt); if (mt.isValid()) { diff --git a/dist/verbs/validate.js b/dist/verbs/validate.js index 66a5f37..89ffde0 100644 --- a/dist/verbs/validate.js +++ b/dist/verbs/validate.js @@ -104,7 +104,6 @@ Implementation of the 'validate' verb for HackMyResume. shouldExit: true }; } - console.log('1111'); return ret; }, this); }; diff --git a/src/core/abstract-resume.coffee b/src/core/abstract-resume.coffee index 81e2588..1670eda 100644 --- a/src/core/abstract-resume.coffee +++ b/src/core/abstract-resume.coffee @@ -19,7 +19,7 @@ class AbstractResume ### duration: (collKey, startKey, endKey, unit) -> unit = unit || 'years' - hist = hist || __.get(this, collKey) + hist = __.get @, collKey return 0 if !hist or !hist.length # BEGIN CODE DUPLICATION --> src/inspectors/gap-inspector.coffee (TODO) @@ -27,24 +27,21 @@ class AbstractResume # Convert the candidate's employment history to an array of dates, # where each element in the array is a start date or an end date of a # job -- it doesn't matter which. - new_e = hist.map( ( job ) -> + new_e = hist.map ( job ) -> obj = _.pick( job, [startKey, endKey] ) # Synthesize an end date if this is a "current" gig obj[endKey] = 'current' if !_.has obj, endKey if obj && (obj[startKey] || obj[endKey]) - obj = _.pairs( obj ) + obj = _.pairs obj obj[0][1] = FluentDate.fmt( obj[0][1] ) if obj.length > 1 obj[1][1] = FluentDate.fmt( obj[1][1] ) - return obj - ) + obj # Flatten the array, remove empties, and sort new_e = _.filter _.flatten( new_e, true ), (v) -> return v && v.length && v[0] && v[0].length - return 0 if !new_e or !new_e.length - new_e = _.sortBy new_e, ( elem ) -> return elem[1].unix() # END CODE DUPLICATION diff --git a/src/core/fluent-date.coffee b/src/core/fluent-date.coffee index f08d156..92472cf 100644 --- a/src/core/fluent-date.coffee +++ b/src/core/fluent-date.coffee @@ -59,16 +59,7 @@ FluentDate.fmt = ( dt, throws ) -> else if /^\s*\d{4}\s*$/.test(dt) # "2015" return moment dt, 'YYYY' else if /^\s*$/.test(dt) # "", " " - defTime = - isNull: true - isBefore: ( other ) -> - if other and !other.isNull then true else false - isAfter: ( other ) -> - if other and !other.isNull then false else false - unix: () -> 0 - format: () -> '' - diff: () -> 0 - return defTime + return moment() else mt = moment dt if mt.isValid() diff --git a/src/verbs/validate.coffee b/src/verbs/validate.coffee index 4e655e0..68d6654 100644 --- a/src/verbs/validate.coffee +++ b/src/verbs/validate.coffee @@ -85,8 +85,6 @@ validate = (sources, unused, opts) -> if opts.assert and !ret.isValid throw fluenterror: HMSTATUS.invalid, shouldExit: true - console.log '1111' - - return ret + ret , @ diff --git a/test/scripts/test-dates.js b/test/scripts/test-dates.js new file mode 100644 index 0000000..93f58ec --- /dev/null +++ b/test/scripts/test-dates.js @@ -0,0 +1,81 @@ +/** +@module test-dates.js +*/ + +var chai = require('chai') + , expect = chai.expect + , should = chai.should() + , path = require('path') + , _ = require('underscore') + , FRESHResume = require('../../dist/core/fresh-resume') + , FCMD = require( '../../dist/index') + , validator = require('is-my-json-valid') + , EXTEND = require('extend'); + +chai.config.includeStack = true; + +var gig = { + employer: 'E1' +}; + +var r = { + name: 'John Doe', + meta: { + format: 'FRESH@0.6.0' + }, + employment: { + history: [ null ] + } +}; + +var tests = [ + // single job, concrete start, no end + [ { start: '2010-01-01' } , { val: 6, unit: 'year' } ], + [ { start: '2010-01' } , { val: 6, unit: 'year' } ], + [ { start: '2010' } , { val: 6, unit: 'year' } ], + + // single job, concrete start, concrete end + [ { start: '2010-01-01', end: '2015-01-01' } , { val: 5, unit: 'year' } ], + [ { start: '2010-01', end: '2015-01' } , { val: 5, unit: 'year' } ], + [ { start: '2010', end: '2015' } , { val: 5, unit: 'year' } ], + + // single job, falsy start, falsy end + [ { } , { val: 0, unit: 'year' } ], + [ { start: null } , { val: 0, unit: 'year' } ], + [ { end: null } , { val: 0, unit: 'year' } ], + [ { start: undefined } , { val: 0, unit: 'year' } ], + [ { end: undefined } , { val: 0, unit: 'year' } ], + [ { start: null, end: null } , { val: 0, unit: 'year' } ], + [ { start: '', end: '' } , { val: 0, unit: 'year' } ], + [ { start: ' ', end: ' ' } , { val: 0, unit: 'year' } ], + [ { start: undefined, end: undefined } , { val: 0, unit: 'year' } ], + + // two jobs (concrete start + end) -> ( concrete start ) + [ { start: '2000-01', end: '2013-01' }, { start: '2013-01' }, { val: 16, unit: 'year' } ], + [ { start: '2000-01', end: '2013-01' }, { start: '2013-01', end: '' }, { val: 16, unit: 'year' } ], + [ { start: '2000-01', end: '2013-01' }, { start: '2013-01', end: null }, { val: 16, unit: 'year' } ], + [ { start: '2000-01', end: '2013-01' }, { start: '2013-01', end: 'current' }, { val: 16, unit: 'year' } ] + +]; + +tests.forEach(function(t){ + _.initial( t ).forEach(function(t){ t.employer = 'E1' }); +}) + +describe('Testing DATES', function () { + + tests.forEach( function(t) { + + it( JSON.stringify( _.initial(t) ), function () { + r.employment.history = _.initial( t ); + var rObj = new FRESHResume(); + rObj.parseJSON( r ); + var dur = rObj.duration(); + expect( dur ).to.equal( _.last(t).val ); + }); + + }); + + + +});