Commit 082de109 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate thread to TypeScript

parent 835f9bef
import type { Annotation } from '../../types/api';
import { notNull } from '../util/typing'; import { notNull } from '../util/typing';
import type { Thread } from './build-thread';
/** @typedef {import('../../types/api').Annotation} Annotation */
/** @typedef {import('./build-thread').Thread} Thread */
/** /**
* Count the number of annotations/replies in the `thread` whose `visible` * Count the number of annotations/replies in the `thread` whose `visible`
* property matches `visibility`. * property matches `visibility`.
* *
* @param {Thread} thread * @param visibility — `true`: count visible annotations
* @param {boolean} visibility — `true`: count visible annotations
* `false`: count hidden annotations * `false`: count hidden annotations
* @return {number}
*/ */
function countByVisibility(thread, visibility) { function countByVisibility(thread: Thread, visibility: boolean): number {
const matchesVisibility = thread.visible === visibility; const matchesVisibility = thread.visible === visibility;
return thread.children.reduce( return thread.children.reduce(
(count, reply) => count + countByVisibility(reply, visibility), (count, reply) => count + countByVisibility(reply, visibility),
...@@ -22,19 +19,15 @@ function countByVisibility(thread, visibility) { ...@@ -22,19 +19,15 @@ function countByVisibility(thread, visibility) {
/** /**
* Count the hidden annotations/replies in the `thread` * Count the hidden annotations/replies in the `thread`
*
* @param {Thread} thread
*/ */
export function countHidden(thread) { export function countHidden(thread: Thread): number {
return countByVisibility(thread, false); return countByVisibility(thread, false);
} }
/** /**
* Count the visible annotations/replies in the `thread` * Count the visible annotations/replies in the `thread`
*
* @param {Thread} thread
*/ */
export function countVisible(thread) { export function countVisible(thread: Thread): number {
return countByVisibility(thread, true); return countByVisibility(thread, true);
} }
...@@ -64,11 +57,8 @@ export function countVisible(thread) { ...@@ -64,11 +57,8 @@ export function countVisible(thread) {
* - reply 6 * - reply 6
* *
* Return [reply 1, reply 4, reply 5] * Return [reply 1, reply 4, reply 5]
*
* @param {Thread[]} threads
* @return {Annotation[]}
*/ */
export function rootAnnotations(threads) { export function rootAnnotations(threads: Thread[]): Annotation[] {
// If there are any threads at this level with extant annotations, return // If there are any threads at this level with extant annotations, return
// those annotations // those annotations
const threadAnnotations = threads const threadAnnotations = threads
...@@ -80,8 +70,7 @@ export function rootAnnotations(threads) { ...@@ -80,8 +70,7 @@ export function rootAnnotations(threads) {
} }
// Else, search across all children at once (an entire hierarchical level) // Else, search across all children at once (an entire hierarchical level)
/** @type {Thread[]} */ const allChildren: Thread[] = [];
const allChildren = [];
threads.forEach(thread => { threads.forEach(thread => {
if (thread.children) { if (thread.children) {
allChildren.push(...thread.children); allChildren.push(...thread.children);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment