Files
remark42/frontend/app/store/comments/utils.ts
T
Pavel MineevandUmputun 5825a55b69 Update frontend
* change root dit and change way to import modules
* update deps to latest versions
* use latest tools for building bundles
* rewrite webpack config
* use nomodule technique for loading modern bundle
* inject polyfills by babel usebuiltins
* update eslint rules
* rename all style files to CSS
* use postcss preset env for building styles
* proper typescript typing
* put all html files to templates folder
* etc
2021-01-12 13:39:26 -06:00

55 lines
1.3 KiB
TypeScript

import { Comment, Node, Sorting } from 'common/types';
import { LS_SORT_KEY, DEFAULT_SORT } from 'common/constants';
import { getItem } from 'common/local-storage';
/**
* Filters tree node
*/
export function filterTree(tree: Node[], fn: (node: Node) => boolean): Node[] {
let filtered = false;
const newTree = tree.reduce<Node[]>((tree, node) => {
if (!fn(node)) {
filtered = true;
return tree;
}
const newNode: Node = !node.replies ? node : { ...node, replies: filterTree(node.replies, fn) };
if (newNode !== node) {
filtered = true;
}
tree.push(newNode);
return tree;
}, []);
if (!filtered) return tree;
return newTree;
}
export function findPinnedComments(thread: Node): Comment[] {
let result: Comment[] = [];
if (thread.comment.pin) {
result = result.concat(thread.comment);
}
if (thread.replies) {
result = result.concat(
thread.replies.reduce((acc: Comment[], thread: Node) => acc.concat(findPinnedComments(thread)), [])
);
}
return result;
}
export function getPinnedComments(threads: Node[]): Comment[] {
return threads.reduce((acc: Comment[], thread: Node) => acc.concat(findPinnedComments(thread)), []);
}
export function getInitialSort() {
const sort = getItem(LS_SORT_KEY) as Sorting;
if (sort) {
return sort;
}
return DEFAULT_SORT;
}