Show node by frequency on empty query (#878)

This commit is contained in:
Chenlei Hu
2024-09-19 09:35:22 +09:00
committed by GitHub
parent efa2fa269d
commit 6c4143ca94
8 changed files with 2711 additions and 8 deletions

View File

@@ -39,3 +39,23 @@ export function highlightQuery(text: string, query: string) {
const regex = new RegExp(`(${query})`, 'gi')
return text.replace(regex, '<span class="highlight">$1</span>')
}
export function formatNumberWithSuffix(
num: number,
{
precision = 1,
roundToInt = false
}: { precision?: number; roundToInt?: boolean } = {}
): string {
const suffixes = ['', 'k', 'm', 'b', 't']
const absNum = Math.abs(num)
if (absNum < 1000) {
return roundToInt ? Math.round(num).toString() : num.toFixed(precision)
}
const exp = Math.min(Math.floor(Math.log10(absNum) / 3), suffixes.length - 1)
const formattedNum = (num / Math.pow(1000, exp)).toFixed(precision)
return `${formattedNum}${suffixes[exp]}`
}