mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-24 07:29:30 +01:00
feat: remove special case grouping
This commit is contained in:
@@ -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)*/;
|
||||
|
||||
Reference in New Issue
Block a user