mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 06:59:31 +01:00
feat: add grouping
This commit is contained in:
@@ -2,12 +2,14 @@ import Storage from "../helpers/storage";
|
|||||||
import fuzzysearch from "fuzzysearch";
|
import fuzzysearch from "fuzzysearch";
|
||||||
var tfun = require("transfun/transfun.js").tfun;
|
var tfun = require("transfun/transfun.js").tfun;
|
||||||
tfun = global.tfun;
|
tfun = global.tfun;
|
||||||
import { extractValues } from "../utils";
|
import { extractValues, groupBy } from "../utils";
|
||||||
|
import { getWeekGroupFromTimestamp, months } from "../utils/date";
|
||||||
|
|
||||||
const KEYS = {
|
const KEYS = {
|
||||||
notes: "notes",
|
notes: "notes",
|
||||||
notebooks: "notebooks"
|
notebooks: "notebooks"
|
||||||
};
|
};
|
||||||
|
|
||||||
function checkInitialized() {
|
function checkInitialized() {
|
||||||
if (!this.isInitialized) {
|
if (!this.isInitialized) {
|
||||||
throw new Error(
|
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 {
|
class Database {
|
||||||
constructor(storage) {
|
constructor(storage) {
|
||||||
this.storage = new Storage(storage);
|
this.storage = new Storage(storage);
|
||||||
@@ -53,12 +49,39 @@ class Database {
|
|||||||
return extractValues(this.notes).reverse();
|
return extractValues(this.notes).reverse();
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO
|
/**
|
||||||
groupNotes() {
|
* Group notes by given criteria
|
||||||
var c = groupBy(this.getNotes(), x =>
|
* @param {string} by One from 'abc', 'month', 'year' or 'week'. Leave it empty for default grouping.
|
||||||
new Date(x.dateCreated).getFullYear()
|
*/
|
||||||
|
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;
|
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