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

32 lines
856 B
JavaScript
Raw Normal View History

2020-09-20 09:26:08 +05:00
import fastsort from "fast-sort";
2020-11-21 10:19:44 +05:00
export function groupBy(arr, key, sortSelector, sortDirection) {
if (sortSelector)
2020-11-21 10:19:44 +05:00
fastsort(arr).by([
{ desc: (t) => t.pinned },
{ [sortDirection]: sortSelector },
]);
2020-09-20 09:26:08 +05:00
let groups = new Map();
2020-09-15 10:10:05 +05:00
arr.forEach((item) => {
2021-02-19 17:36:33 +05:00
let groupTitle = item.pinned ? "Pinned" : key(item);
let arr = groups.get(groupTitle) || [];
arr.push(item);
groups.set(groupTitle, arr);
2020-09-15 10:10:05 +05:00
});
2019-12-18 15:40:49 +05:00
2020-09-15 10:10:05 +05:00
let items = [];
groups.forEach((groupItems, groupTitle) => {
items.push({ title: groupTitle, type: "header" });
groupItems.forEach((item) => items.push(item));
});
2020-09-15 10:10:05 +05:00
return items;
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;
}