Skip to content

Commit

Permalink
Add support for basic types only: strings, booleans, numbers, and null
Browse files Browse the repository at this point in the history
  • Loading branch information
yankovs committed Nov 4, 2023
1 parent db29d6e commit 55664ff
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
21 changes: 6 additions & 15 deletions mwdb/web/src/components/RichAttribute/MarkedMustache.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
renderTokens,
Token,
} from "@mwdb-web/commons/helpers";
import { JSONValue, filter, filters } from "./filters";

/**
* Markdown with Mustache templates for React
Expand All @@ -24,10 +25,7 @@ function appendToLastElement(array: string[], value: string) {
return [...array.slice(0, -1), array[array.length - 1] + value];
}

const filters: { [key: string]: (arg: string) => string } = {
"upper": (arg: string) => arg.toUpperCase(),
"lower": (arg: string) => arg.toLowerCase(),
};
const availableFilters: string[] = filters.map((f: filter) => f.name);

function splitName(name: string) {
if (name === ".")
Expand Down Expand Up @@ -146,24 +144,17 @@ class MustacheContext extends Mustache.Context {
lookup(name: string) {
const filter_expr = name.split("|");
const variable = filter_expr.shift()!.trim();
var original: string | string[] | undefined = this._lookup(variable);
var original: JSONValue = this._lookup(variable);
if (!original) {
return undefined;
}

for (let filter of filter_expr) {
if (!(filter in filters)) {
if (!(filter in availableFilters)) {
continue;
}

const func = filters[filter];
if (Array.isArray(original)) {
original = original.map((value: string) => {
return func(value);
})
} else {
original = func(original);
}
const action = filters.filter((f: filter) => f.name == filter)[0].action;
original = action(original);
}

return original;
Expand Down
22 changes: 22 additions & 0 deletions mwdb/web/src/components/RichAttribute/filters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export type JSONValue =
| string
| number
| boolean
| null

export type filter = {
name: string,
action: (value: JSONValue) => JSONValue,
}

export const filters: filter[] = [
{
name: "upper",
action: (value: JSONValue) => {
if (typeof value === "string") {
return value.toUpperCase();
}
return value;
}
}
];

0 comments on commit 55664ff

Please sign in to comment.