Files
notesnook/packages/core/utils/index.js

77 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-12-14 16:17:18 +05:00
var tfun = require("transfun/transfun.js").tfun;
2020-01-06 16:17:23 +05:00
if (!tfun) {
tfun = global.tfun;
}
2019-12-14 16:17:18 +05:00
export function extractValues(obj) {
const t = [];
2019-12-05 15:58:11 +05:00
for (let key in obj) {
t[t.length] = obj[key];
}
return t;
}
2019-12-12 11:49:06 +05:00
2019-12-18 15:40:49 +05:00
export function groupBy(arr, key, special = false) {
if (special) {
return groupBySpecial(arr, key);
}
2019-12-17 16:52:45 +05:00
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;
2019-12-12 11:49:06 +05:00
}
2019-12-18 15:40:49 +05:00
function groupBySpecial(arr, key) {
let retVal = [];
2020-01-06 15:46:49 +05:00
let _groups = { "": 0 };
2019-12-18 16:14:09 +05:00
let groups = [];
2019-12-18 15:40:49 +05:00
let groupCounts = [];
var i = -1;
2020-01-06 16:17:23 +05:00
let pinned = [];
2019-12-18 15:40:49 +05:00
for (let val of arr) {
2020-01-06 16:17:23 +05:00
if (val.pinned) {
pinned[pinned.length] = val;
continue;
}
2019-12-18 15:40:49 +05:00
i++;
2020-01-06 16:17:23 +05:00
let groupTitle = key(val);
let index =
_groups[groupTitle] === undefined ? i : _groups[groupTitle].index;
2019-12-18 15:40:49 +05:00
let groupIndex =
2020-01-06 16:17:23 +05:00
_groups[groupTitle] == undefined
? groupCounts.length
: _groups[groupTitle].groupIndex;
2020-01-03 16:13:19 +05:00
retVal.splice(index + 1, 0, val);
2019-12-18 15:40:49 +05:00
groupCounts[groupIndex] =
groupCounts.length == groupIndex ? 1 : groupCounts[groupIndex] + 1;
2020-01-06 16:17:23 +05:00
groups[groupIndex] = { title: groupTitle };
_groups[groupTitle] = {
2019-12-18 15:40:49 +05:00
index: i,
groupIndex
};
}
2020-01-06 16:17:23 +05:00
let g = { items: retVal, groups, groupCounts };
g.items.splice(0, 0, ...pinned);
g.groupCounts.splice(0, 0, pinned.length);
2020-01-06 16:18:55 +05:00
g.groups.splice(0, 0, { title: "Pinned" });
2020-01-06 16:17:23 +05:00
return g;
2019-12-18 15:40:49 +05:00
}
2020-03-21 11:15:24 +05:00
var hexPattern = /([A-F]|[a-f]|\d)*/;
export function isHex(input) {
if (typeof input !== "string") return false;
2020-03-21 11:15:24 +05:00
if (!input.match || input.length < 16) return false;
return input.match(hexPattern)[0] === input;
}