1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00

Add IIFE.

This commit is contained in:
hacksalot 2016-01-18 14:10:25 -05:00
parent 712cba57b8
commit c8d8e566f8

View File

@ -1,84 +1,95 @@
/** /**
The HackMyResume date representation. The HackMyResume date representation.
@license MIT. Copyright (c) 2015 James Devlin / FluentDesk. @license MIT. See LICENSE.md for details.
@module fluent-date.js @module core/fluent-date
*/ */
var moment = require('moment'); (function(){
/** var moment = require('moment');
Create a FluentDate from a string or Moment date object. There are a few date
formats to be aware of here.
1. The words "Present" and "Now", referring to the current date
2. The default "YYYY-MM-DD" format used in JSON Resume ("2015-02-10")
3. Year-and-month only ("2015-04")
4. Year-only "YYYY" ("2015")
5. The friendly HackMyResume "mmm YYYY" format ("Mar 2015" or "Dec 2008")
6. Empty dates ("", " ")
7. Any other date format that Moment.js can parse from
Note: Moment can transparently parse all or most of these, without requiring us
to specify a date format...but for maximum parsing safety and to avoid Moment
deprecation warnings, it's recommended to either a) explicitly specify the date
format or b) use an ISO format. For clarity, we handle these cases explicitly.
@class FluentDate
*/
function FluentDate( dt ) {
this.rep = this.fmt( dt );
}
FluentDate/*.prototype*/.fmt = function( dt ) { /**
if( (typeof dt === 'string' || dt instanceof String) ) { Create a FluentDate from a string or Moment date object. There are a few date
dt = dt.toLowerCase().trim(); formats to be aware of here.
if( /^(present|now|current)$/.test(dt) ) { // "Present", "Now" 1. The words "Present" and "Now", referring to the current date
return moment(); 2. The default "YYYY-MM-DD" format used in JSON Resume ("2015-02-10")
} 3. Year-and-month only ("2015-04")
else if( /^\D+\s+\d{4}$/.test(dt) ) { // "Mar 2015" 4. Year-only "YYYY" ("2015")
var parts = dt.split(' '); 5. The friendly HackMyResume "mmm YYYY" format ("Mar 2015" or "Dec 2008")
var month = (months[parts[0]] || abbr[parts[0]]); 6. Empty dates ("", " ")
var temp = parts[1] + '-' + (month < 10 ? '0' + month : month.toString()); 7. Any other date format that Moment.js can parse from
return moment( temp, 'YYYY-MM' ); Note: Moment can transparently parse all or most of these, without requiring us
} to specify a date format...but for maximum parsing safety and to avoid Moment
else if( /^\d{4}-\d{1,2}$/.test(dt) ) { // "2015-03", "1998-4" deprecation warnings, it's recommended to either a) explicitly specify the date
return moment( dt, 'YYYY-MM' ); format or b) use an ISO format. For clarity, we handle these cases explicitly.
} @class FluentDate
else if( /^\s*\d{4}\s*$/.test(dt) ) { // "2015" */
return moment( dt, 'YYYY' ); function FluentDate( dt ) {
} this.rep = this.fmt( dt );
else if( /^\s*$/.test(dt) ) { // "", " " }
var defTime = {
isNull: true, FluentDate/*.prototype*/.fmt = function( dt, throws ) {
isBefore: function( other ) {
return( other && !other.isNull ) ? true : false; throws = (throws === undefined || throws === null) || throws;
},
isAfter: function( other ) { if( (typeof dt === 'string' || dt instanceof String) ) {
return( other && !other.isNull ) ? false : false; dt = dt.toLowerCase().trim();
}, if( /^(present|now|current)$/.test(dt) ) { // "Present", "Now"
unix: function() { return 0; }, return moment();
format: function() { return ''; }, }
diff: function() { return 0; } else if( /^\D+\s+\d{4}$/.test(dt) ) { // "Mar 2015"
}; var parts = dt.split(' ');
return defTime; var month = (months[parts[0]] || abbr[parts[0]]);
var temp = parts[1] + '-' + (month < 10 ? '0' + month : month.toString());
return moment( temp, 'YYYY-MM' );
}
else if( /^\d{4}-\d{1,2}$/.test(dt) ) { // "2015-03", "1998-4"
return moment( dt, 'YYYY-MM' );
}
else if( /^\s*\d{4}\s*$/.test(dt) ) { // "2015"
return moment( dt, 'YYYY' );
}
else if( /^\s*$/.test(dt) ) { // "", " "
var defTime = {
isNull: true,
isBefore: function( other ) {
return( other && !other.isNull ) ? true : false;
},
isAfter: function( other ) {
return( other && !other.isNull ) ? false : false;
},
unix: function() { return 0; },
format: function() { return ''; },
diff: function() { return 0; }
};
return defTime;
}
else {
var mt = moment( dt );
if(mt.isValid())
return mt;
if( throws )
throw 'Invalid date format encountered.';
return null;
}
} }
else { else {
var mt = moment( dt ); if( !dt ) {
if(mt.isValid()) return moment();
return mt; }
throw 'Invalid date format encountered.'; else if( dt.isValid && dt.isValid() )
return dt;
if( throws )
throw 'Unknown date object encountered.';
return null;
} }
} };
else {
if( !dt ) {
return moment();
}
else if( dt.isValid && dt.isValid() )
return dt;
throw 'Unknown date object encountered.';
}
};
var months = {}, abbr = {}; var months = {}, abbr = {};
moment.months().forEach(function(m,idx){months[m.toLowerCase()]=idx+1;}); moment.months().forEach(function(m,idx){months[m.toLowerCase()]=idx+1;});
moment.monthsShort().forEach(function(m,idx){abbr[m.toLowerCase()]=idx+1;}); moment.monthsShort().forEach(function(m,idx){abbr[m.toLowerCase()]=idx+1;});
abbr.sept = 9; abbr.sept = 9;
module.exports = FluentDate; module.exports = FluentDate;
}());