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";
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();
});
import { groupArray } from "../utils/grouping";
test("group alphabetically", () => {
const sortedAlphabet = "abcdefghijlmnopqrstuvwxyz".split("");
const alphabet = "nopqrstuvwxyzabcdefghijlm".split("");
let ret = groupBy(
alphabet,
(x) => x[0],
(x) => x[0],
"asc"
).filter((v) => v.title);
const sortedAlphabet = "abcdefghijlmnopqrstuvwxyz"
.split("")
.map((a) => ({ title: a }));
const alphabet = "nopqrstuvwxyzabcdefghijlm"
.split("")
.map((a) => ({ title: a, item: true }));
let ret = groupArray(alphabet, {
groupId: "abc",
sortDirection: "asc",
sortBy: "title",
}).filter((v) => !v.item);
expect(
sortedAlphabet.every((alpha, index) => {
return ret[index].title === alpha;
return ret[index].title === alpha.title.toUpperCase();
})
).toBeTruthy();
});

View File

@@ -1,6 +1,7 @@
import DB from "../../api";
import StorageInterface from "../../__mocks__/storage.mock";
import { getLastWeekTimestamp } from "../../utils/date";
import { groupArray } from "../../utils/grouping";
const TEST_NOTEBOOK = {
title: "Test Notebook",
@@ -54,7 +55,12 @@ const groupedTest = (type) =>
title: "Some title and title title",
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.some((i) => i.type === "header")).toBe(true);
});

View File

@@ -27,7 +27,7 @@ class Settings {
}
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,
item.pins

View File

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