mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 23:19:40 +01:00
feat: use dayjs for all date/time related logic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import DB from "../../api";
|
||||
import StorageInterface from "../../__mocks__/storage.mock";
|
||||
import { getLastWeekTimestamp } from "../../utils/date";
|
||||
import dayjs from "dayjs";
|
||||
import { groupArray } from "../../utils/grouping";
|
||||
|
||||
const TEST_NOTEBOOK = {
|
||||
@@ -48,19 +48,19 @@ const groupedTest = (type) =>
|
||||
await db.notes.add({
|
||||
...TEST_NOTE,
|
||||
title: "Some title",
|
||||
dateCreated: getLastWeekTimestamp() - 604800000,
|
||||
dateCreated: dayjs().startOf("week").subtract(1, "day").unix(),
|
||||
});
|
||||
await db.notes.add({
|
||||
...TEST_NOTE,
|
||||
title: "Some title and title title",
|
||||
dateCreated: getLastWeekTimestamp() - 604800000 * 2,
|
||||
dateCreated: dayjs().subtract(2, "weeks").unix(),
|
||||
});
|
||||
let grouped = groupArray(db.notes.all, {
|
||||
groupBy: type,
|
||||
sortDirection: "desc",
|
||||
sortBy: "dateCreated",
|
||||
});
|
||||
expect(grouped.length).toBeGreaterThan(0);
|
||||
expect(grouped.length).toBeGreaterThan(1);
|
||||
expect(grouped.some((i) => i.type === "header")).toBe(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import sort from "fast-sort";
|
||||
import { get7DayTimestamp } from "../utils/date";
|
||||
import dayjs from "dayjs";
|
||||
|
||||
export default class Trash {
|
||||
/**
|
||||
@@ -19,9 +19,8 @@ export default class Trash {
|
||||
}
|
||||
|
||||
async cleanup() {
|
||||
const sevenDayPreviousTimestamp = Date.now() - get7DayTimestamp();
|
||||
this.all.forEach(async (item) => {
|
||||
if (item.dateDeleted < sevenDayPreviousTimestamp) {
|
||||
if (dayjs(item.dateDeleted).add(7, "days").isBefore(dayjs())) {
|
||||
await this.delete(item.id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@stablelib/blake2s": "^1.0.1",
|
||||
"dayjs": "^1.10.6",
|
||||
"fast-sort": "^2.0.1",
|
||||
"lean-he": "^2.1.2",
|
||||
"no-internet": "^1.5.2",
|
||||
|
||||
@@ -1,102 +1,14 @@
|
||||
function getWeeksInMonth(month, year, _start) {
|
||||
let weeks = [],
|
||||
firstDate = new Date(year, month, 1),
|
||||
lastDate = new Date(year, month + 1, 0),
|
||||
numDays = lastDate.getDate();
|
||||
import dayjs from "dayjs";
|
||||
|
||||
let start = 1;
|
||||
let end = 7 - firstDate.getDay();
|
||||
if (firstDate.getDay() === _start) {
|
||||
end = 7;
|
||||
} else {
|
||||
let preMonthEndDay = new Date(year, month, 0);
|
||||
start = preMonthEndDay.getDate() + 1 - firstDate.getDay() + _start;
|
||||
end = 7 - firstDate.getDay() + _start;
|
||||
weeks.push({
|
||||
start: new Date(
|
||||
preMonthEndDay.getFullYear(),
|
||||
preMonthEndDay.getMonth(),
|
||||
start
|
||||
),
|
||||
end: new Date(year, month, end),
|
||||
});
|
||||
start = end + 1;
|
||||
end = end + 7;
|
||||
}
|
||||
while (start <= numDays) {
|
||||
weeks.push({
|
||||
start: new Date(year, month, start),
|
||||
end: new Date(year, month, end),
|
||||
});
|
||||
start = end + 1;
|
||||
end = end + 7;
|
||||
if (end > numDays) {
|
||||
end = end - numDays + _start;
|
||||
weeks.push({
|
||||
start: new Date(year, month, start),
|
||||
end: new Date(year, month + 1, end),
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
return weeks;
|
||||
}
|
||||
|
||||
function getWeeksInYear(year) {
|
||||
let weeks = [];
|
||||
for (let i = 0; i <= 11; i++) {
|
||||
weeks = weeks.concat(...getWeeksInMonth(i, year, 0));
|
||||
}
|
||||
return weeks;
|
||||
}
|
||||
|
||||
export function get7DayTimestamp() {
|
||||
return 604800000;
|
||||
}
|
||||
|
||||
export function getLastWeekTimestamp() {
|
||||
const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 6;
|
||||
const lastSaturday = new Date();
|
||||
return lastSaturday.setDate(t);
|
||||
}
|
||||
export const months = [
|
||||
"Jan",
|
||||
"Feb",
|
||||
"Mar",
|
||||
"Apr",
|
||||
"May",
|
||||
"Jun",
|
||||
"Jul",
|
||||
"Aug",
|
||||
"Sep",
|
||||
"Oct",
|
||||
"Nov",
|
||||
"Dec",
|
||||
];
|
||||
let years = {};
|
||||
export function getWeekGroupFromTimestamp(timestamp) {
|
||||
let date = new Date(timestamp);
|
||||
date.setHours(0, 0, 0, 0);
|
||||
if (!years.hasOwnProperty(date.getFullYear())) {
|
||||
years[date.getFullYear()] = getWeeksInYear(date.getFullYear());
|
||||
}
|
||||
let weeks = years[date.getFullYear()];
|
||||
|
||||
let week = weeks.find((v) => date >= v.start && date <= v.end);
|
||||
|
||||
const startMonth =
|
||||
week.start.getMonth() === week.end.getMonth()
|
||||
? ""
|
||||
: months[week.start.getMonth()];
|
||||
const startYear =
|
||||
week.start.getFullYear() === week.end.getFullYear()
|
||||
? ""
|
||||
: `, ${week.start.getFullYear()}`;
|
||||
|
||||
//Format: {start} {month}, {year} - {end} {month}, {year}
|
||||
return `${week.start.getDate()} ${startMonth}${startYear} - ${week.end.getDate()} ${
|
||||
months[week.end.getMonth()]
|
||||
}, ${week.end.getFullYear()}`;
|
||||
const date = dayjs(timestamp);
|
||||
const start = date.startOf("week");
|
||||
const end = date.endOf("week");
|
||||
const startMonth = start.month() === end.month() ? "" : " MMM";
|
||||
const startYear = start.year() === end.year() ? "" : ", YYYY";
|
||||
return `${start.format(`DD${startMonth}${startYear}`)} - ${end.format(
|
||||
"DD MMM, YYYY"
|
||||
)}`;
|
||||
}
|
||||
|
||||
export function formatDate(date) {
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import "../types";
|
||||
import fastsort from "fast-sort";
|
||||
import {
|
||||
getWeekGroupFromTimestamp,
|
||||
months,
|
||||
getLastWeekTimestamp,
|
||||
get7DayTimestamp,
|
||||
} from "./date";
|
||||
import dayjs from "dayjs";
|
||||
import { getWeekGroupFromTimestamp, months } from "./date";
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -23,21 +19,16 @@ const getSortSelectors = (options) => [
|
||||
},
|
||||
];
|
||||
|
||||
const TIMESTAMPS = {
|
||||
recent: () => getLastWeekTimestamp(7),
|
||||
lastWeek: () => getLastWeekTimestamp(7) - get7DayTimestamp(), //seven day timestamp value
|
||||
};
|
||||
|
||||
const KEY_SELECTORS = {
|
||||
abc: (item) => item.title[0].toUpperCase(),
|
||||
month: (item, groupBy) => months[new Date(item[groupBy]).getMonth()],
|
||||
month: (item, groupBy) => dayjs(item[groupBy]).format("MMMM"),
|
||||
week: (item, groupBy) => getWeekGroupFromTimestamp(item[groupBy]),
|
||||
year: (item, groupBy) => new Date(item[groupBy]).getFullYear().toString(),
|
||||
year: (item, groupBy) => dayjs(item[groupBy]).year(),
|
||||
default: (item, groupBy) => {
|
||||
if (groupBy === "title") groupBy = "dateEdited";
|
||||
return item[groupBy] >= TIMESTAMPS.recent()
|
||||
const date = dayjs(item[groupBy]);
|
||||
return date.isAfter(dayjs().subtract(1, "week"))
|
||||
? "Recent"
|
||||
: item[groupBy] >= TIMESTAMPS.lastWeek()
|
||||
: date.isAfter(dayjs().subtract(2, "weeks"))
|
||||
? "Last week"
|
||||
: "Older";
|
||||
},
|
||||
@@ -51,7 +42,7 @@ const KEY_SELECTORS = {
|
||||
export function groupArray(
|
||||
array,
|
||||
options = {
|
||||
groupBy: undefined,
|
||||
groupBy: "default",
|
||||
sortBy: "dateEdited",
|
||||
sortDirection: "desc",
|
||||
}
|
||||
|
||||
@@ -2225,6 +2225,11 @@ data-urls@^2.0.0:
|
||||
whatwg-mimetype "^2.3.0"
|
||||
whatwg-url "^8.0.0"
|
||||
|
||||
dayjs@^1.10.6:
|
||||
version "1.10.6"
|
||||
resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.10.6.tgz#288b2aa82f2d8418a6c9d4df5898c0737ad02a63"
|
||||
integrity sha512-AztC/IOW4L1Q41A86phW5Thhcrco3xuAA+YX/BLpLWWjRcTj5TOt/QImBLmCKlrF7u7k47arTnOyL6GnbG8Hvw==
|
||||
|
||||
debug@^2.1.1, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9:
|
||||
version "2.6.9"
|
||||
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
|
||||
|
||||
Reference in New Issue
Block a user