fix: minor bugs with grouping logic

This commit is contained in:
thecodrr
2021-07-12 14:00:22 +05:00
parent cc93e4f1c9
commit 285a5cae13
4 changed files with 24 additions and 22 deletions

View File

@@ -1,25 +1,20 @@
import { groupBy } from "../utils"; import { groupArray } from "../utils/grouping";
test("groupBy should work", () => {
let ret = groupBy([1, 2, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3], (x) => x.toString());
expect(ret.length).toBe(15);
expect(ret.some((i) => i.title === "1")).toBeTruthy();
expect(ret.some((i) => i.title === "2")).toBeTruthy();
expect(ret.some((i) => i.title === "3")).toBeTruthy();
});
test("group alphabetically", () => { test("group alphabetically", () => {
const sortedAlphabet = "abcdefghijlmnopqrstuvwxyz".split(""); const sortedAlphabet = "abcdefghijlmnopqrstuvwxyz"
const alphabet = "nopqrstuvwxyzabcdefghijlm".split(""); .split("")
let ret = groupBy( .map((a) => ({ title: a }));
alphabet, const alphabet = "nopqrstuvwxyzabcdefghijlm"
(x) => x[0], .split("")
(x) => x[0], .map((a) => ({ title: a, item: true }));
"asc" let ret = groupArray(alphabet, {
).filter((v) => v.title); groupId: "abc",
sortDirection: "asc",
sortBy: "title",
}).filter((v) => !v.item);
expect( expect(
sortedAlphabet.every((alpha, index) => { sortedAlphabet.every((alpha, index) => {
return ret[index].title === alpha; return ret[index].title === alpha.title.toUpperCase();
}) })
).toBeTruthy(); ).toBeTruthy();
}); });

View File

@@ -1,6 +1,7 @@
import DB from "../../api"; import DB from "../../api";
import StorageInterface from "../../__mocks__/storage.mock"; import StorageInterface from "../../__mocks__/storage.mock";
import { getLastWeekTimestamp } from "../../utils/date"; import { getLastWeekTimestamp } from "../../utils/date";
import { groupArray } from "../../utils/grouping";
const TEST_NOTEBOOK = { const TEST_NOTEBOOK = {
title: "Test Notebook", title: "Test Notebook",
@@ -54,7 +55,12 @@ const groupedTest = (type) =>
title: "Some title and title title", title: "Some title and title title",
dateCreated: getLastWeekTimestamp() - 604800000 * 2, dateCreated: getLastWeekTimestamp() - 604800000 * 2,
}); });
let grouped = db.notes.group(type); let grouped = groupArray(db.notes.all, {
groupId: type,
sortDirection: "desc",
sortBy: "dateCreated",
groupBy: "dateCreated",
});
expect(grouped.length).toBeGreaterThan(0); expect(grouped.length).toBeGreaterThan(0);
expect(grouped.some((i) => i.type === "header")).toBe(true); expect(grouped.some((i) => i.type === "header")).toBe(true);
}); });

View File

@@ -27,7 +27,7 @@ class Settings {
} }
async merge(item) { async merge(item) {
if (this.settings.dateEdited > (await this._db.lastSynced())) { if (this._settings.dateEdited > (await this._db.lastSynced())) {
this._settings.pins = setManipulator.union( this._settings.pins = setManipulator.union(
this._settings.pins, this._settings.pins,
item.pins item.pins

View File

@@ -42,8 +42,9 @@ const KEY_SELECTORS = {
* @returns Grouped array * @returns Grouped array
*/ */
export function groupArray(array, options) { export function groupArray(array, options) {
const keySelector = KEY_SELECTORS[options.groupId]; const keySelector = KEY_SELECTORS[options.groupId || "default"];
if (sortSelector) fastsort(array).by(getSortSelectors(options)); if (options.sortBy && options.sortDirection)
fastsort(array).by(getSortSelectors(options));
let groups = new Map(); let groups = new Map();
array.forEach((item) => { array.forEach((item) => {