mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 06:29:29 +01:00
84 lines
2.1 KiB
JavaScript
84 lines
2.1 KiB
JavaScript
import "../types";
|
|
import fastsort from "fast-sort";
|
|
import {
|
|
getWeekGroupFromTimestamp,
|
|
months,
|
|
getLastWeekTimestamp,
|
|
get7DayTimestamp,
|
|
} from "./date";
|
|
|
|
/**
|
|
*
|
|
* @param {GroupOptions} options
|
|
* @returns sort selectors
|
|
*/
|
|
const getSortSelectors = (options) => [
|
|
{ desc: (t) => t.conflicted },
|
|
{ desc: (t) => t.pinned },
|
|
{
|
|
[options.sortDirection]: (item) => {
|
|
if (options.sortBy === "title") return item.title[0].toUpperCase();
|
|
return item[options.sortBy];
|
|
},
|
|
},
|
|
];
|
|
|
|
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()],
|
|
week: (item, groupBy) => getWeekGroupFromTimestamp(item[groupBy]),
|
|
year: (item, groupBy) => new Date(item[groupBy]).getFullYear().toString(),
|
|
default: (item, groupBy) => {
|
|
if (groupBy === "title") groupBy = "dateEdited";
|
|
return item[groupBy] >= TIMESTAMPS.recent()
|
|
? "Recent"
|
|
: item[groupBy] >= TIMESTAMPS.lastWeek()
|
|
? "Last week"
|
|
: "Older";
|
|
},
|
|
};
|
|
|
|
/**
|
|
* @param {any[]} array
|
|
* @param {GroupOptions} options
|
|
* @returns Grouped array
|
|
*/
|
|
export function groupArray(
|
|
array,
|
|
options = {
|
|
groupBy: undefined,
|
|
sortBy: "dateEdited",
|
|
sortDirection: "desc",
|
|
}
|
|
) {
|
|
const keySelector = KEY_SELECTORS[options.groupBy || "default"];
|
|
if (options.sortBy && options.sortDirection)
|
|
fastsort(array).by(getSortSelectors(options));
|
|
|
|
let groups = new Map();
|
|
array.forEach((item) => {
|
|
let groupTitle = item.pinned
|
|
? "Pinned"
|
|
: item.conflicted
|
|
? "Conflicted"
|
|
: keySelector(item, options.sortBy);
|
|
|
|
let group = groups.get(groupTitle) || [];
|
|
group.push(item);
|
|
groups.set(groupTitle, group);
|
|
});
|
|
|
|
let items = [];
|
|
groups.forEach((groupItems, groupTitle) => {
|
|
let group = { title: groupTitle, type: "header" };
|
|
items.push(group);
|
|
groupItems.forEach((item) => items.push(item));
|
|
});
|
|
return items;
|
|
}
|