mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 22:49:45 +01:00
feat: add grouping
This commit is contained in:
@@ -2,12 +2,14 @@ import Storage from "../helpers/storage";
|
||||
import fuzzysearch from "fuzzysearch";
|
||||
var tfun = require("transfun/transfun.js").tfun;
|
||||
tfun = global.tfun;
|
||||
import { extractValues } from "../utils";
|
||||
import { extractValues, groupBy } from "../utils";
|
||||
import { getWeekGroupFromTimestamp, months } from "../utils/date";
|
||||
|
||||
const KEYS = {
|
||||
notes: "notes",
|
||||
notebooks: "notebooks"
|
||||
};
|
||||
|
||||
function checkInitialized() {
|
||||
if (!this.isInitialized) {
|
||||
throw new Error(
|
||||
@@ -15,13 +17,7 @@ function checkInitialized() {
|
||||
);
|
||||
}
|
||||
}
|
||||
function groupBy(xs, key) {
|
||||
return tfun.reduce(function(rv, x) {
|
||||
var v = key instanceof Function ? key(x) : x[key];
|
||||
(rv[v] = rv[v] || []).push(x);
|
||||
return rv;
|
||||
})(xs);
|
||||
}
|
||||
|
||||
class Database {
|
||||
constructor(storage) {
|
||||
this.storage = new Storage(storage);
|
||||
@@ -53,12 +49,39 @@ class Database {
|
||||
return extractValues(this.notes).reverse();
|
||||
}
|
||||
|
||||
//TODO
|
||||
groupNotes() {
|
||||
var c = groupBy(this.getNotes(), x =>
|
||||
new Date(x.dateCreated).getFullYear()
|
||||
/**
|
||||
* Group notes by given criteria
|
||||
* @param {string} by One from 'abc', 'month', 'year' or 'week'. Leave it empty for default grouping.
|
||||
*/
|
||||
groupNotes(by) {
|
||||
switch (by) {
|
||||
case "abc":
|
||||
return groupBy(notes, note => note.title[0].toUpperCase());
|
||||
case "month":
|
||||
return groupBy(
|
||||
notes,
|
||||
note => months[new Date(note.dateCreated).getMonth()]
|
||||
);
|
||||
return c;
|
||||
case "week":
|
||||
groupBy(notes, note => getWeekGroupFromTimestamp(note.dateCreated));
|
||||
case "year":
|
||||
groupBy(
|
||||
notes,
|
||||
note => months[new Date(note.dateCreated).getFullYear()]
|
||||
);
|
||||
default:
|
||||
let timestamps = {
|
||||
recent: getLastWeekTimestamp(7),
|
||||
lastWeek: getLastWeekTimestamp(7) - 604800000 //seven day timestamp value
|
||||
};
|
||||
groupBy(notes, note =>
|
||||
note.dateCreated >= timestamps.recent
|
||||
? "Recent"
|
||||
: note.dateCreated >= timestamps.lastWeek
|
||||
? "Last week"
|
||||
: "Older"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
69
packages/core/utils/date.js
Normal file
69
packages/core/utils/date.js
Normal file
@@ -0,0 +1,69 @@
|
||||
function getWeeksInMonth(month, year) {
|
||||
var weeks = [],
|
||||
firstDate = new Date(year, month, 1),
|
||||
lastDate = new Date(year, month + 1, 0),
|
||||
numDays = lastDate.getDate();
|
||||
|
||||
var start = 1;
|
||||
var end = 7 - firstDate.getDay();
|
||||
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 = numDays;
|
||||
}
|
||||
return weeks;
|
||||
}
|
||||
|
||||
function getWeeksInYear(year) {
|
||||
let weeks = [];
|
||||
for (let i = 0; i <= 11; i++) {
|
||||
weeks.concat(...getWeeksInMonth(i, year));
|
||||
}
|
||||
return weeks;
|
||||
}
|
||||
|
||||
function getDayTimestamp(last) {
|
||||
to = new Date().getDate() - last;
|
||||
date = new Date();
|
||||
return date.setDate(to);
|
||||
}
|
||||
|
||||
function getLastWeekTimestamp() {
|
||||
const t = new Date().getDate() + (6 - new Date().getDay() - 1) - 6;
|
||||
const lastSaturday = new Date();
|
||||
return lastSaturday.setDate(t);
|
||||
}
|
||||
export const months = [
|
||||
"January",
|
||||
"February",
|
||||
"March",
|
||||
"April",
|
||||
"May",
|
||||
"June",
|
||||
"July",
|
||||
"August",
|
||||
"September",
|
||||
"October",
|
||||
"November",
|
||||
"December"
|
||||
];
|
||||
let years = {};
|
||||
export function getWeekGroupFromTimestamp(timestamp) {
|
||||
let date = new Date(timestamp);
|
||||
|
||||
if (!years.hasOwnProperty(date.getFullYear())) {
|
||||
years[date.getFullYear()] = weeks;
|
||||
}
|
||||
let weeks = years[date.getFullYear()];
|
||||
|
||||
let week = weeks.find(v => date >= v.start && date <= v.end);
|
||||
|
||||
//Format: {month} {start} - {end}, {year}
|
||||
return `${months[date.getMonth()]} ${week.start} - ${
|
||||
week.end
|
||||
}, ${date.getFullYear()}`;
|
||||
}
|
||||
@@ -5,3 +5,11 @@ export function extractValues(obj) {
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
export function groupBy(xs, key) {
|
||||
return tfun.reduce(function(rv, x) {
|
||||
var v = key instanceof Function ? key(x) : x[key];
|
||||
(rv[v] = rv[v] || []).push(x);
|
||||
return rv;
|
||||
})(xs);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user