Skip to content

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

feat: groupBySpecial for React app

thecodrr1ff319de287e
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index 160c8f4c1..eb842d0be 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -62,38 +62,46 @@ class Database {
/**
* Group notes by given criteria
* @param {string} by One from 'abc', 'month', 'year' or 'week'. Leave it empty for default grouping.
+ * @param {boolean} special Should only be used in the React app.
*/
- groupNotes(by) {
+ groupNotes(by, special = false) {
//TODO add tests
let notes = this.getNotes();
switch (by) {
case "abc":
- return groupBy(notes, note => note.title[0].toUpperCase());
+ return groupBy(notes, note => note.title[0].toUpperCase(), special);
case "month":
return groupBy(
notes,
- note => months[new Date(note.dateCreated).getMonth()]
+ note => months[new Date(note.dateCreated).getMonth()],
+ special
);
case "week":
- return groupBy(notes, note =>
- getWeekGroupFromTimestamp(note.dateCreated)
+ return groupBy(
+ notes,
+ note => getWeekGroupFromTimestamp(note.dateCreated),
+ special
);
case "year":
return groupBy(
notes,
- note => months[new Date(note.dateCreated).getFullYear()]
+ note => months[new Date(note.dateCreated).getFullYear()],
+ special
);
default:
let timestamps = {
recent: getLastWeekTimestamp(7),
lastWeek: getLastWeekTimestamp(7) - 604800000 //seven day timestamp value
};
- return groupBy(notes, note =>
- note.dateCreated >= timestamps.recent
- ? "Recent"
- : note.dateCreated >= timestamps.lastWeek
- ? "Last week"
- : "Older"
+ return groupBy(
+ notes,
+ note =>
+ note.dateCreated >= timestamps.recent
+ ? "Recent"
+ : note.dateCreated >= timestamps.lastWeek
+ ? "Last week"
+ : "Older",
+ special
);
}
}
diff --git a/packages/core/utils/index.js b/packages/core/utils/index.js
index 5c6d9944b..755a4e2a9 100644
--- a/packages/core/utils/index.js
+++ b/packages/core/utils/index.js
@@ -9,7 +9,10 @@ export function extractValues(obj) {
return t;
}
-export function groupBy(arr, key) {
+export function groupBy(arr, key, special = false) {
+ if (special) {
+ return groupBySpecial(arr, key);
+ }
let retVal = [];
for (let val of arr) {
let v = key(val);
@@ -25,3 +28,26 @@ export function groupBy(arr, key) {
}
return retVal;
}
+
+function groupBySpecial(arr, key) {
+ let retVal = [];
+ let groups = {};
+ let groupCounts = [];
+ var i = -1;
+ for (let val of arr) {
+ i++;
+ let k = key(val);
+ let index = groups[k] === undefined ? i : groups[k].index;
+ let groupIndex =
+ groups[k] == undefined ? groupCounts.length : groups[k].groupIndex;
+ retVal.splice(index, 0, val);
+ groupCounts[groupIndex] =
+ groupCounts.length == groupIndex ? 1 : groupCounts[groupIndex]++;
+ groups[k] = {
+ title: k,
+ index: i,
+ groupIndex
+ };
+ }
+ return { items: retVal, groups, groupCounts };
+}