Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add grouping

thecodrr52caba7eed28
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index 739b2b620..551c3f75d 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -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()
- );
- return c;
+ /**
+ * 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()]
+ );
+ 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"
+ );
+ }
}
/**
diff --git a/packages/core/utils/date.js b/packages/core/utils/date.js
new file mode 100644
index 000000000..377a5e31d
--- /dev/null
+++ b/packages/core/utils/date.js
@@ -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()}`;
+}
diff --git a/packages/core/utils/index.js b/packages/core/utils/index.js
index 4ce3bbb58..92169ac50 100644
--- a/packages/core/utils/index.js
+++ b/packages/core/utils/index.js
@@ -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);
+}