From 86dc31d7786eb3478310b32ef3a02178811c6dbf Mon Sep 17 00:00:00 2001 From: thecodrr Date: Tue, 15 Sep 2020 10:10:05 +0500 Subject: [PATCH] feat: remove special case grouping --- packages/core/__tests__/notes.test.js | 10 ---- packages/core/__tests__/utils/index.js | 11 +--- packages/core/collections/notes.js | 39 ++++++-------- packages/core/utils/index.js | 72 +++++--------------------- 4 files changed, 30 insertions(+), 102 deletions(-) diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.js index 1b84ada33..74d3baf70 100644 --- a/packages/core/__tests__/notes.test.js +++ b/packages/core/__tests__/notes.test.js @@ -124,24 +124,14 @@ test("get pinned notes", () => test("get grouped notes by abc", () => groupedTest("abc")); -test("get grouped notes by abc (special)", () => groupedTest("abc", true)); - test("get grouped notes by month", () => groupedTest("month")); -test("get grouped notes by month (special)", () => groupedTest("month", true)); - test("get grouped notes by year", () => groupedTest("year")); -test("get grouped notes by year (special)", () => groupedTest("year", true)); - test("get grouped notes by weak", () => groupedTest("week")); -test("get grouped notes by weak (special)", () => groupedTest("week", true)); - test("get grouped notes default", () => groupedTest()); -test("get grouped notes default (special)", () => groupedTest("", true)); - test("pin note", () => noteTest().then(async ({ db, id }) => { let note = db.notes.note(id); diff --git a/packages/core/__tests__/utils/index.js b/packages/core/__tests__/utils/index.js index ea1e7de14..e5bd41562 100644 --- a/packages/core/__tests__/utils/index.js +++ b/packages/core/__tests__/utils/index.js @@ -60,17 +60,8 @@ const groupedTest = (type, special = false) => dateCreated: getLastWeekTimestamp() - 604800000 * 2, }); let grouped = db.notes.group(type, special); - if (special) { - expect(grouped.items.length).toBeGreaterThan(0); - expect(grouped.groups.length).toBeGreaterThan(0); - expect(grouped.groupCounts.length).toBeGreaterThan(0); - // check order - expect(grouped.groups[0].title).toBe("Pinned"); - return; - } expect(grouped.length).toBeGreaterThan(0); - expect(grouped[0].data.length).toBeGreaterThan(0); - expect(grouped[0].title.length).toBeGreaterThan(0); + expect(grouped.some((i) => i.type === "header")).toBe(true); }); export { diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.js index 17265b39c..430c6b569 100644 --- a/packages/core/collections/notes.js +++ b/packages/core/collections/notes.js @@ -141,46 +141,39 @@ export default class Notes extends Collection { .map((id) => this._collection.getItem(id)); } - group(by, special = false) { - let notes = !special + group(by) { + let notes = this.all; + /* !special ? tfun.filter(".pinned === false")(this.all) - : this.all; + : */ notes = sort(notes).desc((t) => t.dateCreated); switch (by) { case "abc": - return groupBy(notes, (note) => note.title[0].toUpperCase(), special); + return groupBy(notes, (note) => note.title[0].toUpperCase()); case "month": return groupBy( notes, - (note) => months[new Date(note.dateCreated).getMonth()], - special + (note) => months[new Date(note.dateCreated).getMonth()] ); case "week": - return groupBy( - notes, - (note) => getWeekGroupFromTimestamp(note.dateCreated), - special + return groupBy(notes, (note) => + getWeekGroupFromTimestamp(note.dateCreated) ); case "year": - return groupBy( - notes, - (note) => new Date(note.dateCreated).getFullYear().toString(), - special + return groupBy(notes, (note) => + new Date(note.dateCreated).getFullYear().toString() ); default: let timestamps = { recent: getLastWeekTimestamp(7), lastWeek: getLastWeekTimestamp(7) - get7DayTimestamp(), //seven day timestamp value }; - return groupBy( - notes, - (note) => - note.dateCreated >= timestamps.recent - ? "Recent" - : note.dateCreated >= timestamps.lastWeek - ? "Last week" - : "Older", - special + return groupBy(notes, (note) => + note.dateCreated >= timestamps.recent + ? "Recent" + : note.dateCreated >= timestamps.lastWeek + ? "Last week" + : "Older" ); } } diff --git a/packages/core/utils/index.js b/packages/core/utils/index.js index 8b47c1e4b..fdc922dce 100644 --- a/packages/core/utils/index.js +++ b/packages/core/utils/index.js @@ -1,64 +1,18 @@ -var tfun = require("transfun/transfun.js").tfun; -if (!tfun) { - tfun = global.tfun; -} +export function groupBy(arr, key) { + let groups = {}; + arr.forEach((item) => { + let groupTitle = key(item); + let group = groups[groupTitle] + ? groups[groupTitle] + : (groups[groupTitle] = []); + group.push(item); + }); -export function groupBy(arr, key, special = false) { - if (special) { - return groupBySpecial(arr, key); + let items = []; + for (let group in groups) { + items = [...items, { title: group, type: "header" }, ...groups[group]]; } - let retVal = []; - for (let val of arr) { - let v = key(val); - let index = retVal.findIndex((a) => a.title === v); - if (index === -1) { - index = retVal.length; - retVal[retVal.length] = { - title: v, - data: [], - }; - } - retVal[index].data.push(val); - } - return retVal; -} - -function groupBySpecial(arr, key) { - let retVal = []; - let _groups = { "": 0 }; - let groups = []; - let groupCounts = []; - var i = -1; - let pinned = []; - for (let val of arr) { - if (val.pinned) { - //TODO test - pinned[pinned.length] = val; - continue; - } - i++; - let groupTitle = key(val); - let index = - _groups[groupTitle] === undefined ? i : _groups[groupTitle].index; - let groupIndex = - _groups[groupTitle] == undefined - ? groupCounts.length - : _groups[groupTitle].groupIndex; - retVal.splice(index + 1, 0, val); - groupCounts[groupIndex] = - groupCounts.length == groupIndex ? 1 : groupCounts[groupIndex] + 1; - groups[groupIndex] = { title: groupTitle }; - _groups[groupTitle] = { - index: i, - groupIndex, - }; - } - - let g = { items: retVal, groups, groupCounts }; - g.items.splice(0, 0, ...pinned); - g.groupCounts.splice(0, 0, pinned.length); - g.groups.splice(0, 0, { title: "Pinned" }); - return g; + return items; } var hexPattern = /([A-F]|[a-f]|\d)*/;