pin-diary-6/template
code:mod.ts
import {
getDay,
getWeek,
addDays,
subDays,
subYears,
addWeeks,
subWeeks,
getYear,
lightFormat,
getDayOfYear,
getDaysInYear,
intervalToDuration,
startOfWeek,
endOfWeek
} from "./deps.ts";
const format = "yyyy/MM/dd";
export function makeWeeklyDiary(date: Date): {
title: string;
header: string[];
footer: string[];
} {
date = endOfWeek(date, { weekStartsOn: 1 })
/** /blu3moで日記が始まってからの年数 */
const intervalY = intervalToDuration({ start: new Date(2021, 4, 10), end: date }).years;
return {
title: toTitle(date),
header: [
第${getWeek(date, {weekStartsOn: 1})}週,
${getYear(date)}年 ${(getDayOfYear(date) * 100 / getDaysInYear(date)).toFixed(2)}%経過,
"",
"今週のn年前",
(i) => [~${lightFormat(endOfWeek(subYears(date, i + 1), { weekStartsOn: 1 }), "yyyyMMdd")}],
"先週のレビュー"
),
],
footer: [
toTitle(date)
[private.icon]
],
};
}
const dateRegExp = /^~\d{8}$/;
export function isOldWeeklyDiary(title: string, today: Date): boolean {
if (!dateRegExp.test(title)) return false;
return toTitle(today) !== title;
}
function toTitle(date: Date): string {
return "~" + lightFormat(endOfWeek(date, { weekStartsOn: 1 }), "yyyyMMdd");
}
code:deps.ts
// 一年の経過率を計算するのに必要
// 日付計算に使う
// 文字列変換に使う
export { default as intervalToDuration } from "./intervalToDuration.ts";
code:intervalToDuration.ts
// and fix type declaration errors
import type {
Duration,
Interval,
/**
* @name intervalToDuration
* @category Common Helpers
* @summary Convert interval to duration
*
* @description
* Convert a interval object to a duration object.
*
* @param {Interval} interval - the interval to convert to duration
*
* @returns {Duration} The duration Object
* @throws {TypeError} Requires 2 arguments
* @throws {RangeError} start must not be Invalid Date
* @throws {RangeError} end must not be Invalid Date
*
* @example
* // Get the duration between January 15, 1929 and April 4, 1968.
* intervalToDuration({
* start: new Date(1929, 0, 15, 12, 0, 0),
* end: new Date(1968, 3, 4, 19, 5, 0)
* })
* // => { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }
*/
export default function intervalToDuration({ start, end }: Interval): Duration {
requiredArgs(1, arguments);
const dateLeft = toDate(start);
const dateRight = toDate(end);
if (!isValid(dateLeft)) {
throw new RangeError("Start Date is invalid");
}
if (!isValid(dateRight)) {
throw new RangeError("End Date is invalid");
}
const duration = {
years: 0,
months: 0,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
};
const sign = compareAsc(dateLeft, dateRight);
duration.years = Math.abs(differenceInYears(dateLeft, dateRight));
const remainingMonths = sub(dateLeft, { years: sign * duration.years });
duration.months = Math.abs(differenceInMonths(remainingMonths, dateRight));
const remainingDays = sub(remainingMonths, {
months: sign * duration.months,
});
duration.days = Math.abs(differenceInDays(remainingDays, dateRight));
const remainingHours = sub(remainingDays, { days: sign * duration.days });
duration.hours = Math.abs(differenceInHours(remainingHours, dateRight));
const remainingMinutes = sub(remainingHours, {
hours: sign * duration.hours,
});
duration.minutes = Math.abs(differenceInMinutes(remainingMinutes, dateRight));
const remainingSeconds = sub(remainingMinutes, {
minutes: sign * duration.minutes,
});
duration.seconds = Math.abs(differenceInSeconds(remainingSeconds, dateRight));
return duration;
}