Skip to content
This repository has been archived by the owner on Nov 29, 2019. It is now read-only.

Commit

Permalink
replaced date formatter with moment.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Andric committed Feb 25, 2017
1 parent f3b0734 commit fa09f76
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 54 deletions.
54 changes: 4 additions & 50 deletions lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
var formatDate = function(str) {
var d = new Date(str),
date = d.getUTCDate().toString(),
month = Number(d.getUTCMonth() + 1).toString(),
year = d.getUTCFullYear();
if (!d.getUTCDate()) { // failed to parse
return str;
}
if (date.length === 1) {
date = `0${date}`;
}
if (month.length === 1) {
month = `0${month}`;
}
return `${year}-${date}-${month}`;
};
var moment = require('moment');

module.exports = {
name: function (name, opts) {
Expand Down Expand Up @@ -56,40 +41,9 @@ module.exports = {
},
/*
* Return the string date format: YYYY-MM-DD
* All calculations assume GMT. This should be upgraded to use moment.
* All calculations assume GMT.
*/
date: function(str) {
var match;
// 2015
if (/^\d{4}$/.test(str)) {
return `${str}-01-01`;
}
// 25/5/2004 and 5/25/2004
if (/^\d+\/\d+\/\d+$/.test(str)) {
if (new Date(str).getDate()) {
return formatDate(str);
}
match = str.match(/^(\d+)\/(\d+)\/(\d+)$/);
return formatDate(`${match[2]}/${match[1]}/${match[3]}`);
}
// /10/2015
// throw away bad data, only use year.
if (/^\/\d+\/\d{4}$/.test(str)) {
match = str.match(/\/(\d{4})$/);
return formatDate(match[1]);
}
// 13//2016
// throw away bad data, only use year.
if (/^[\/]?\d+\/\/\d{4}$/.test(str)) {
match = str.match(/^\d+\/\/(\d{4})$/);
return formatDate(match[1]);
}
// Apr-12
// We have a date, assume it's this year.
if (/^\w+-\d+$/.test(str)) {
return formatDate(`${str}-${(new Date()).getUTCFullYear()}`);
}
// Can't do nothing for ya man.
return str;
date: function(str, inFormat, outFormat) {
return moment.utc(str, inFormat).format(outFormat || 'YYYY-MM-DD');
}
};
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
},
"dependencies": {
"fast-csv": "^2.1.0",
"moment": "^2.17.1",
"password-generator": "^2.0.2",
"uuid": "^2.0.2",
"underscore": "^1.8.3"
"underscore": "^1.8.3",
"uuid": "^2.0.2"
},
"devDependencies": {
"doctoc": "^1.2.0"
Expand Down
2 changes: 1 addition & 1 deletion project-templates/import-contacts/concessions-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var config = {
},
date_of_birth: {
use: 'Date de naissance',
format: [normalize.date]
format: [normalize.date, 'YYYY']
},
age: 'Age',
menage: 'Menage',
Expand Down
2 changes: 1 addition & 1 deletion project-templates/import-contacts/people-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var config = {
},
date_of_birth: {
use: 'Date de naissance',
format: [normalize.date]
format: [normalize.date, 'YYYY']
},
menage: 'Menage',
sex: 'Sexe',
Expand Down

0 comments on commit fa09f76

Please sign in to comment.