Content deleted Content added
add more docs |
eslint cleanup |
||
Line 1:
/**
* COMMENTS IN LOCAL TIME
Line 30 ⟶ 29:
function convertMonthToNumber(month) {
return new Date(`${month} 1, 2001`).getMonth();
}
function getDates(time) {
const [, oldHour, oldMinute, oldDay, oldMonth, oldYear] = time;
// Today
const today = new Date();
// Yesterday
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
// Tomorrow
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
// Set the date entered.
const newTime = new Date();
newTime.setUTCFullYear(oldYear, convertMonthToNumber(oldMonth), oldDay);
newTime.setUTCHours(oldHour);
newTime.setUTCMinutes(oldMinute);
return { time: newTime, today, tomorrow, yesterday };
}
Line 86 ⟶ 111:
adjustTime(originalTimestamp, search) {
const { time, today, tomorrow, yesterday } = getDates(
originalTimestamp.match(search)
);
// A string matching the date pattern was found, but it cannot be
Line 114 ⟶ 121:
}
const date = this.determineDateText({
time,
tomorrow,
yesterday,
});
const minute = addLeadingZero(time.getMinutes());
const finalTime = `${hour}:${minute}${ampm}`;
let returnDate;
// Determine the time offset.
const utcValue = (-1 * time.getTimezoneOffset()) / 60;
const utcOffset =
utcValue >= 0 ? `+${utcValue}` : `−${Math.abs(utcValue.toFixed(1))}`;
if (this.LocalComments.timeFirst) {
Line 221 ⟶ 144:
}
return
}
Line 239 ⟶ 162:
this.language.December,
][number];
}
createDateText({ day, month, time, today, year }) {
// calculate day of week
const dayNames = [
this.language.Sunday,
this.language.Monday,
this.language.Tuesday,
this.language.Wednesday,
this.language.Thursday,
this.language.Friday,
this.language.Saturday,
];
const dayOfTheWeek = dayNames[time.getDay()];
let descriptiveDifference = '';
let last = '';
// Create a relative descriptive difference
if (this.LocalComments.dateDifference) {
({ descriptiveDifference, last } = this.createRelativeDate(
today,
time
));
}
const monthName = this.convertNumberToMonth(time.getMonth());
// format the date according to user preferences
let formattedDate = '';
switch (this.LocalComments.dateFormat.toLowerCase()) {
case 'dmy':
formattedDate = `${day} ${monthName} ${year}`;
break;
case 'mdy':
formattedDate = `${monthName} ${day}, ${year}`;
break;
default:
formattedDate = `${year}-${month}-${addLeadingZero(day)}`;
}
let formattedDayOfTheWeek = '';
if (this.LocalComments.dayOfWeek) {
formattedDayOfTheWeek = `, ${last}${dayOfTheWeek}`;
}
return formattedDate + formattedDayOfTheWeek + descriptiveDifference;
}
Line 248 ⟶ 221:
* @returns {Object.<string, *>} Relative date data
*/
// eslint-disable-next-line max-statements
createRelativeDate(today, time) {
/**
Line 263 ⟶ 237:
*/
let daysAgo = Math.abs(Math.round(millisecondsAgo / 1000 / 60 / 60 / 24));
millisecondsAgo,
});
//
// it's better than the previous method that used 1 January + delta days.
// That was usually quite off because it mapped the second delta month to
// February, which has only 28 days. This method is usually not more than
// one day off, except perhaps over very distant dates.
/**
Line 331 ⟶ 290:
const descriptiveParts = [];
// There is years text to add.
if (yearsAgo > 0) {
descriptiveParts.push(
)}`
}
// There is months text to add.
if (monthsAgo > 0) {
descriptiveParts.push(
)}`
}
// There is days text to add.
if (daysAgo > 0) {
descriptiveParts.push(
)}`
}
Line 369 ⟶ 331:
}
determineDateText({ time, today, tomorrow, yesterday }) {
// Set the date bits to output.
const year = time.getFullYear();
const month = addLeadingZero(time.getMonth() + 1);
const day = time.getDate();
// return 'today' or 'yesterday' if that is the case
if (
year === today.getFullYear() &&
month === addLeadingZero(today.getMonth() + 1) &&
day === today.getDate()
) {
return this.language.Today;
}
if (
year === yesterday.getFullYear() &&
month === addLeadingZero(yesterday.getMonth() + 1) &&
day === yesterday.getDate()
) {
return this.language.Yesterday;
}
if (
year === tomorrow.getFullYear() &&
month === addLeadingZero(tomorrow.getMonth() + 1) &&
day === tomorrow.getDate()
) {
return this.language.Tomorrow;
}
return this.createDateText({ day, month, time, today, year });
}
getHour(time) {
let ampm;
let hour = parseInt(time.getHours(), 10);
if (this.LocalComments.twentyFourHours) {
ampm = '';
hour = addLeadingZero(hour);
} else {
// Output am or pm depending on the date.
ampm = hour <= 11 ? ' am' : ' pm';
if (hour > 12) {
hour -= 12;
} else if (hour === 0) {
hour = 12;
}
}
return { ampm, hour };
}
relativeText({ daysAgo, millisecondsAgo }) {
let differenceWord = '';
let last = '';
// The date is in the past.
if (millisecondsAgo >= 0) {
differenceWord = this.language.ago;
if (daysAgo <= 7) {
last = `${this.language.last} `;
}
// The date is in the future.
} else {
differenceWord = this.language['from now'];
if (daysAgo <= 7) {
last = `${this.language.this} `;
}
}
return { differenceWord, last };
}
// eslint-disable-next-line max-statements
replaceText(node, search) {
if (!node) {
Line 398 ⟶ 440:
const beforeMatch = value.substring(0, position);
const afterMatch = value.substring(position + stringLength);
const
search
);
const timestamp = time ? time.getTime() : '';
// Is the "timestamp" attribute used for microformats?
Line 409 ⟶ 454:
span.setAttribute('timestamp', timestamp);
span.title = match;
span.append(document.createTextNode(
parent = node.parentNode;
|