Skip to content

Commit

Permalink
Merge pull request #80 from mullwaden/fix/parse-start-and-end-dates
Browse files Browse the repository at this point in the history
Issue #78
  • Loading branch information
joshuatz committed Nov 27, 2022
2 parents 489f1c6 + e67ecd8 commit 0181a8d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
lazyCopy,
liDateToJSDate,
noNullOrUndef,
parseDate,
parseStartDate,
promptDownload,
setQueryParams,
urlToBase64,
Expand Down Expand Up @@ -719,7 +719,7 @@ window.LinkedinToResumeJson = (() => {
// profileView vs dash key
const issueDateObject = award.issueDate || award.issuedOn;
if (issueDateObject && typeof issueDateObject === 'object') {
parsedAward.date = parseDate(issueDateObject);
parsedAward.date = parseStartDate(issueDateObject);
}
_outputJsonLegacy.awards.push(parsedAward);
_outputJsonStable.awards.push(parsedAward);
Expand All @@ -740,7 +740,7 @@ window.LinkedinToResumeJson = (() => {
// profileView vs dash key
const publicationDateObj = publication.date || publication.publishedOn;
if (publicationDateObj && typeof publicationDateObj === 'object' && typeof publicationDateObj.year !== 'undefined') {
parsedPublication.releaseDate = parseDate(publicationDateObj);
parsedPublication.releaseDate = parseStartDate(publicationDateObj);
}
_outputJsonLegacy.publications.push(parsedPublication);
_outputJsonStable.publications.push({
Expand Down
70 changes: 48 additions & 22 deletions src/utilities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {{[key: number]: number}} */
/** @type {Record<number,number>} */
export const maxDaysOfMonth = {
1: 31,
2: 28,
Expand Down Expand Up @@ -28,39 +28,65 @@ export function zeroLeftPad(n) {
}

/**
* Returns month, padded to two digits
* @param {Number} [m] month
* @returns {string} month, padded to two digits
* Gets day, 1 if isStart=true else the last day of the month
* @param {boolean} isStart
* @returns {number} month
*/
export function getMonthPadded(m) {
if (!m) return `12`;
function getDefaultMonth(isStart) {
return isStart ? 1 : 12;
}

return zeroLeftPad(m);
/**
* Gets day, 1 if isStart=true else the last day of the month
* @param {Number} month
* @param {boolean} isStart
* @returns {number} day
*/
function getDefaultDay(month, isStart) {
return isStart ? 1 : maxDaysOfMonth[month];
}

/**
* Gets day, padded to two digits
* @param {Number} d day
* @param {Number} m month
* @returns {string} day, padded to two digits
* Parses an object with year, month and day and returns a string with the date.
* @param {LiDate} dateObj
* @param {boolean} isStart
* @returns {string} Date, as string, formatted for JSONResume
*/
export function getDayPadded(d, m) {
if (!d) {
if (!m) return `31`;
return maxDaysOfMonth[m].toString();
function parseDate(dateObj, isStart) {
const year = dateObj?.year;

if (year === undefined) {
return '';
}

return zeroLeftPad(d);
const month = dateObj.month ?? getDefaultMonth(isStart);
const day = dateObj.day ?? getDefaultDay(month, isStart);

return `${year}-${zeroLeftPad(month)}-${zeroLeftPad(day)}`;
}

/**
* Parses an object with year, month and day and returns a string with the date.
* If month is not present, should return 12, and if day is not present, should return last month day.
* - If month is not present, should return 1.
* - If day is not present, should return 1.
*
* @param {LiDate} dateObj
* @returns {string} Date, as string, formatted for JSONResume
*/
export function parseStartDate(dateObj) {
return parseDate(dateObj, true);
}

/**
* Parses an object with year, month and day and returns a string with the date.
* - If month is not present, should return 12.
* - If day is not present, should return last month day.
*
* @param {LiDate} dateObj
* @returns {string} Date, as string, formatted for JSONResume
*/
export function parseDate(dateObj) {
return dateObj && dateObj.year ? `${dateObj.year}-${getMonthPadded(dateObj.month)}-${getDayPadded(dateObj.day, dateObj.month)}` : '';
export function parseEndDate(dateObj) {
return parseDate(dateObj, false);
}

/**
Expand All @@ -70,7 +96,7 @@ export function parseDate(dateObj) {
*/
export function liDateToJSDate(liDateObj) {
// This is a cheat; by passing string + 00:00, we can force Date to not offset (by timezone), and also treat month as NOT zero-indexed, which is how LI uses it
return new Date(`${parseDate(liDateObj)} 00:00`);
return new Date(`${parseStartDate(liDateObj)} 00:00`);
}

/**
Expand Down Expand Up @@ -269,10 +295,10 @@ export function parseAndAttachResumeDates(resumeObj, liEntity) {
const start = timePeriod.startDate || timePeriod.start;
const end = timePeriod.endDate || timePeriod.end;
if (end) {
resumeObj.endDate = parseDate(end);
resumeObj.endDate = parseEndDate(end);
}
if (start) {
resumeObj.startDate = parseDate(start);
resumeObj.startDate = parseStartDate(start);
}
}
}

0 comments on commit 0181a8d

Please sign in to comment.