From d5afb3eb2e700c209b4490d26be822b0f9716d0c Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sun, 3 Jan 2016 23:17:36 -0500 Subject: [PATCH] Handle missing dates during gap inspection. --- src/inspectors/gap-inspector.js | 39 ++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/inspectors/gap-inspector.js b/src/inspectors/gap-inspector.js index f87e4ed..5ab9568 100644 --- a/src/inspectors/gap-inspector.js +++ b/src/inspectors/gap-inspector.js @@ -12,6 +12,7 @@ Employment gap analysis for HackMyResume. var _ = require('underscore'); var FluentDate = require('../core/fluent-date'); + var moment = require('moment'); @@ -42,25 +43,41 @@ Employment gap analysis for HackMyResume. // where each element in the array is a start date or an end date of a // job -- it doesn't matter which. var new_e = rez.employment.history.map( function( job ){ - var obj = _.pairs( _.pick( job, ['start', 'end'] ) ); - obj[0][1] = FluentDate.fmt( obj[0][1] ); - if( obj.length > 1 ) - obj[1][1] = FluentDate.fmt( obj[1][1] ); + var obj = _.pick( job, ['start', 'end'] ); + if( obj && (obj.start || obj.end)) { + 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; }); // Flatten the array. new_e = _.flatten( new_e, true ); + // Remove empties (objects that had no .start or .end date) + new_e = _.omit( new_e, function(v) { + var isEmpty = ( !v || !v.length || !v[0] || !v[0].length ); + if( isEmpty ) console.log('Found empty'); + return isEmpty; + }); + // Sort the array, mixing start dates and end dates together new_e = _.sortBy( new_e, function( elem ) { return elem[1].unix(); }); + var num_gaps = 0 + , ref_count = 0 + , total_days = 0 + , total_work_days = 0 + , coverage = { gaps: [], overlaps: [] } + , gap_start; + // Iterative over elements in the array. Each time a start date is found, // increment a reference count. Each time an end date is found, decrement // the reference count. When the reference count reaches 0, we have a gap. // When the reference count is > 0, the candidate is employed. - var num_gaps = 0, ref_count = 0, gap_start, total_days = 0, total_work_days = 0, - coverage = { gaps: [], overlaps: [] }; + new_e.forEach( function(point) { var inc = point[0] === 'start' ? 1 : -1; ref_count += inc; @@ -95,10 +112,12 @@ Employment gap analysis for HackMyResume. // _.each( coverage.overlaps, function(ol) { // return ol.end = ol.end || now; // }); - if( !_.last( coverage.overlaps ).end ) { - var l = _.last( coverage.overlaps ); - l.end = moment(); - l.duration = l.end.diff( l.start, 'days' ); + if( coverage.overlaps.length ) { + if( !_.last( coverage.overlaps ).end ) { + var l = _.last( coverage.overlaps ); + l.end = moment(); + l.duration = l.end.diff( l.start, 'days' ); + } } coverage.duration = total_days;