Skip to content

feat: add support for filtering & sorting values #820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/formatValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function formatValue(value: any, valueToHtml: (value: any) => string, max
if (isObject(value)) {
let content = '';

const {title, image, ...rest} = value as any;
const {title, image, ...rest} = value as any;

if (title) {
content += `<h2>${valueToHtml(title)}</h2>`;
Expand All @@ -27,7 +27,15 @@ export function formatValue(value: any, valueToHtml: (value: any) => string, max
const keys = Object.keys(rest);
if (keys.length > 0) {
content += '<table>';
var kv_list = [];
var sort_tooltip: string | undefined = undefined
for (const key of keys) {
// Handle sort placeholder.
if (key === "tooltip_sort_placeholder") {
sort_tooltip = (rest as any)[key];
continue;
}

let val = (rest as any)[key];

// ignore undefined properties
Expand All @@ -39,7 +47,17 @@ export function formatValue(value: any, valueToHtml: (value: any) => string, max
val = stringify(val, maxDepth);
}

content += `<tr><td class="key">${valueToHtml(key)}</td><td class="value">${valueToHtml(val)}</td></tr>`;
const kv: [string, any] = [key, val];
kv_list.push(kv);
}

// Sort tooltip if specified.
if (sort_tooltip !== undefined) {
const order: number = sort_tooltip === "0" ? 1 : -1; // order = 1: ascending, order = -1: descending
kv_list = kv_list.sort((n1,n2) => order * (n1[1] - n2[1])); // Sort by values.
}
for (const kv of kv_list) {
content += `<tr><td class="key">${valueToHtml(kv[0])}:</td><td class="value">${valueToHtml(kv[1])}</td></tr>`;
}
content += `</table>`;
}
Expand Down