From 4d054245a353d347e6c3f2ed826371a5296753ea Mon Sep 17 00:00:00 2001 From: thecodrr Date: Mon, 6 Jan 2020 15:46:49 +0500 Subject: [PATCH] feat: add pinned group --- packages/core/api/database.js | 5 +++-- packages/core/utils/index.js | 19 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/core/api/database.js b/packages/core/api/database.js index ca6d5fdc3..820d82f8d 100644 --- a/packages/core/api/database.js +++ b/packages/core/api/database.js @@ -68,8 +68,9 @@ class Database { * @param {boolean} special Should only be used in the React app. */ groupNotes(by, special = false) { - //TODO add tests - let notes = tfun.filter(".pinned == false")(this.getNotes()); + let notes = !special + ? tfun.filter(".pinned == false")(this.getNotes()) + : this.getNotes(); switch (by) { case "abc": return groupBy( diff --git a/packages/core/utils/index.js b/packages/core/utils/index.js index 411cb8b80..3adb7c290 100644 --- a/packages/core/utils/index.js +++ b/packages/core/utils/index.js @@ -1,5 +1,7 @@ var tfun = require("transfun/transfun.js").tfun; -tfun = global.tfun; +if (!tfun) { + tfun = global.tfun; +} export function extractValues(obj) { const t = []; @@ -31,21 +33,22 @@ export function groupBy(arr, key, special = false) { function groupBySpecial(arr, key) { let retVal = []; - let _groups = {}; + let _groups = { "": 0 }; let groups = []; let groupCounts = []; var i = -1; for (let val of arr) { i++; - let k = key(val); - let index = _groups[k] === undefined ? i : _groups[k].index; - let groupIndex = - _groups[k] == undefined ? groupCounts.length : _groups[k].groupIndex; + let groupTitle = val.pinned ? "" : key(val); + let index = !_groups[groupTitle] ? i : _groups[groupTitle].index; + let groupIndex = !_groups[groupTitle] + ? groupCounts.length + : _groups[groupTitle].groupIndex; retVal.splice(index + 1, 0, val); groupCounts[groupIndex] = groupCounts.length == groupIndex ? 1 : groupCounts[groupIndex] + 1; - groups[groupIndex] = { title: k }; - _groups[k] = { + groups[groupIndex] = { title: groupTitle }; + _groups[groupTitle] = { index: i, groupIndex };