WiP on segments
This commit is contained in:
parent
6cc34136f5
commit
f3ff89c536
21 changed files with 945 additions and 352 deletions
67
shared/date.js
Normal file
67
shared/date.js
Normal file
|
@ -0,0 +1,67 @@
|
|||
'use strict';
|
||||
|
||||
const moment = require('moment');
|
||||
|
||||
const birthdayYear = 2000;
|
||||
|
||||
const DateFormat = {
|
||||
US: 'us',
|
||||
EU: 'eur',
|
||||
INTL: 'intl'
|
||||
};
|
||||
|
||||
const dateFormatStrings = {
|
||||
'us': 'MM/DD/YYYY',
|
||||
'eur': 'DD/MM/YYYY',
|
||||
'intl': 'YYYY-MM-DD'
|
||||
};
|
||||
|
||||
const birthdayFormatStrings = {
|
||||
'us': 'MM/DD',
|
||||
'eur': 'DD/MM',
|
||||
'intl': 'MM/DD'
|
||||
};
|
||||
|
||||
function parseDate(format, text) {
|
||||
const date = moment.utc(text, dateFormatStrings[format]);
|
||||
|
||||
if (date.isValid()) {
|
||||
return date.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
function parseBirthday(format, text) {
|
||||
const fullDateStr = format === DateFormat.INTL ? birthdayYear + '-' + text : text + '-' + birthdayYear;
|
||||
const date = moment.utc(fullDateStr, dateFormatStrings[format]);
|
||||
|
||||
if (date.isValid()) {
|
||||
return date.toDate();
|
||||
}
|
||||
}
|
||||
|
||||
function formatDate(format, date) {
|
||||
return moment.utc(date).format(dateFormatStrings[format]);
|
||||
}
|
||||
|
||||
function formatBirthday(format, date) {
|
||||
return moment.utc(date).format(birthdayFormatStrings[format]);
|
||||
}
|
||||
|
||||
function getDateFormatString(format) {
|
||||
return dateFormatStrings[format];
|
||||
}
|
||||
|
||||
function getBirthdayFormatString(format) {
|
||||
return birthdayFormatStrings[format];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
DateFormat,
|
||||
birthdayYear,
|
||||
parseDate,
|
||||
parseBirthday,
|
||||
formatDate,
|
||||
formatBirthday,
|
||||
getDateFormatString,
|
||||
getBirthdayFormatString
|
||||
};
|
|
@ -1,65 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
function parseDate(type, text) {
|
||||
const isUs = type === 'us';
|
||||
const trimmedText = text.trim();
|
||||
|
||||
// try international format first YYYY-MM-DD
|
||||
const parts = trimmedText.match(/^(\d{4})\D+(\d{2})(?:\D+(\d{2})\b)$/);
|
||||
let day, month, year;
|
||||
let value;
|
||||
|
||||
if (parts) {
|
||||
year = Number(parts[1]) || 2000;
|
||||
month = Number(parts[2]) || 0;
|
||||
day = Number(parts[3]) || 0;
|
||||
value = new Date(Date.UTC(year, month - 1, day));
|
||||
} else {
|
||||
const parts = trimmedText.match(/^(\d+)\D+(\d+)(?:\D+(\d+)\b)$/);
|
||||
if (!parts) {
|
||||
value = null;
|
||||
} else {
|
||||
day = Number(parts[isUs ? 2 : 1]);
|
||||
month = Number(parts[isUs ? 1 : 2]);
|
||||
year = Number(parts[3]);
|
||||
|
||||
if (!day || !month) {
|
||||
value = null;
|
||||
} else {
|
||||
value = new Date(Date.UTC(year, month - 1, day));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseBirthday(type, text) {
|
||||
const isUs = type === 'us';
|
||||
const trimmedText = text.trim();
|
||||
|
||||
let day, month, year;
|
||||
let value;
|
||||
|
||||
const parts = trimmedText.match(/^(\d+)\D+(\d+)$/);
|
||||
if (!parts) {
|
||||
value = null;
|
||||
} else {
|
||||
day = Number(parts[isUs ? 2 : 1]);
|
||||
month = Number(parts[isUs ? 1 : 2]);
|
||||
|
||||
if (!day || !month) {
|
||||
value = null;
|
||||
} else {
|
||||
value = new Date(Date.UTC(2000, month - 1, day));
|
||||
}
|
||||
}
|
||||
console.log(value);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
parseDate,
|
||||
parseBirthday
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue