1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-06-26 05:50:04 +01:00

Improve date handling.

This commit is contained in:
hacksalot 2016-01-30 20:06:04 -05:00
parent 8e806dc04f
commit 27c7a0264a
7 changed files with 90 additions and 50 deletions

View File

@ -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;
}

View File

@ -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()) {

View File

@ -104,7 +104,6 @@ Implementation of the 'validate' verb for HackMyResume.
shouldExit: true
};
}
console.log('1111');
return ret;
}, this);
};

View File

@ -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

View File

@ -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()

View File

@ -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
, @

View File

@ -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 );
});
});
});