diff --git a/src/core/templateParser.ts b/src/core/templateParser.ts index fbf29eb8..2ce382c3 100644 --- a/src/core/templateParser.ts +++ b/src/core/templateParser.ts @@ -1,8 +1,9 @@ +import * as E from 'fp-ts/Either'; import * as P from 'parser-ts/Parser' import { run } from 'parser-ts/code-frame' import * as C from 'parser-ts/char' import * as S from 'parser-ts/string' -import { Either, pipe } from '@std'; +import { A, Either, O, pipe } from '@std'; type TemplateText = { _tag: 'text', value: string } type TemplateVariable = { _tag: 'variable', value: string } type Token = TemplateText | TemplateVariable @@ -59,3 +60,17 @@ export function parseTemplate(template: string): Either return run((Template), template) // return S.run(template)(P.many(Template)) } + +export function templateVariables(parsedTemplate: ReturnType): string[] { + return pipe( + parsedTemplate, + E.fold( + () => [], + A.filterMap((token) => { + if (token._tag === 'variable') { + return O.some(token.value) + } + return O.none + })) + ) +} diff --git a/src/std/index.ts b/src/std/index.ts index 80327691..0ce28c75 100644 --- a/src/std/index.ts +++ b/src/std/index.ts @@ -1,5 +1,5 @@ import { pipe as p, flow as f } from "fp-ts/function"; -import { partitionMap, findFirst, findFirstMap, partition, map as mapArr, filter, compact } from "fp-ts/Array"; +import { partitionMap, findFirst, findFirstMap, partition, map as mapArr, filter, compact, filterMap } from "fp-ts/Array"; import { map as mapO, getOrElse as getOrElseOpt, some, none, fromNullable as fromNullableOpt } from 'fp-ts/Option' import { isLeft, isRight, tryCatchK, map, getOrElse, fromNullable, right, left, mapLeft, Either, bimap, tryCatch, flatMap } from "fp-ts/Either"; import { BaseSchema, Output, ValiError, parse as parseV } from "valibot"; @@ -16,7 +16,8 @@ export const A = { findFirst, findFirstMap, map: mapArr, - filter + filter, + filterMap } /** * Non empty array diff --git a/src/views/components/TemplateEditor.svelte b/src/views/components/TemplateEditor.svelte index 465a4d9a..46d7f4c2 100644 --- a/src/views/components/TemplateEditor.svelte +++ b/src/views/components/TemplateEditor.svelte @@ -1,4 +1,5 @@
@@ -27,7 +30,10 @@ Available fields:
    {#each fieldNames as field} -
  • {field}
  • +
  • + {field} + {usedVariables.includes(field) ? "✅" : ""} +
  • {/each}