Skip to content

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

fix: crash when title is empty & groupBy = abc

thecodrr55a538f279b8
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js
index 10e204e5c..5f564c3b9 100644
--- a/packages/core/__tests__/notes.test.js
+++ b/packages/core/__tests__/notes.test.js
@@ -1,3 +1,4 @@
+import { groupArray } from "../utils/grouping";
import {
StorageInterface,
databaseTest,
@@ -272,3 +273,13 @@ test("deleting a colored note should remove it from that color", () =>
expect(color).toBeFalsy();
// TODO expect(color.noteIds.indexOf(id)).toBe(-1);
}));
+
+test("grouping items where item.title is empty or undefined shouldn't throw", () => {
+ expect(
+ groupArray([{ title: "" }], {
+ groupBy: "abc",
+ sortBy: "title",
+ sortDirection: "asc",
+ })
+ ).toBeTruthy();
+});
diff --git a/packages/core/utils/grouping.js b/packages/core/utils/grouping.js
index 843dac66c..588d0c015 100644
--- a/packages/core/utils/grouping.js
+++ b/packages/core/utils/grouping.js
@@ -13,14 +13,14 @@ const getSortSelectors = (options) => [
{ desc: (t) => t.pinned },
{
[options.sortDirection]: (item) => {
- if (options.sortBy === "title") return item.title[0].toUpperCase();
+ if (options.sortBy === "title") return getFirstCharacter(item);
return item[options.sortBy];
},
},
];
const KEY_SELECTORS = {
- abc: (item) => item.title[0].toUpperCase(),
+ abc: (item) => getFirstCharacter(item),
month: (item, groupBy) => dayjs(item[groupBy]).format("MMMM"),
week: (item, groupBy) => getWeekGroupFromTimestamp(item[groupBy]),
year: (item, groupBy) => dayjs(item[groupBy]).year(),
@@ -72,3 +72,9 @@ export function groupArray(
});
return items;
}
+
+function getFirstCharacter(item) {
+ const title = item.title && item.title.trim();
+ if (!title || title.length <= 0) return "-";
+ return title[0].toUpperCase();
+}