From 913d94e31b46a412dc5f1c960a8a1b93e637c6f1 Mon Sep 17 00:00:00 2001 From: Andrew Michael McNutt Date: Mon, 3 Jun 2024 14:32:28 -0700 Subject: [PATCH 1/2] Bug: make string conversion more robust --- src/controls/GetColorsFromString.svelte | 47 ++++++------ src/controls/NewPal.svelte | 31 +++++--- src/lib/Color.ts | 52 ++++++++------ src/lib/Utils.test.ts | 96 +++++++++++++++++++++++++ src/lib/utils.ts | 19 +++++ 5 files changed, 190 insertions(+), 55 deletions(-) create mode 100644 src/lib/Utils.test.ts diff --git a/src/controls/GetColorsFromString.svelte b/src/controls/GetColorsFromString.svelte index 498ccbbd..993ed5e3 100644 --- a/src/controls/GetColorsFromString.svelte +++ b/src/controls/GetColorsFromString.svelte @@ -2,44 +2,56 @@ import { Color } from "../lib/Color"; import type { ColorWrap } from "../types"; import configStore from "../stores/config-store"; - import { wrapInBlankSemantics } from "../lib/utils"; + import { wrapInBlankSemantics, processBodyTextToColors } from "../lib/utils"; let state: "idle" | "error" = "idle"; export let onChange: (colors: ColorWrap[]) => void; export let colors: ColorWrap[]; export let colorSpace: string; + let errorMsg = ""; function processBodyInput(body: string) { + console.log("asd"); try { - const newColors = body - .split(",") - .map((x) => x.replace(/"/g, "").trim()) - // remove all parens and brackets - .map((x) => x.replace(/[\(\)\[\]]/g, "")) - .filter((x) => x.length > 0) - .map((x) => Color.colorFromString(x, colorSpace as any)) - .map((x, idx) => { + const newColors = processBodyTextToColors(body, colorSpace).map( + (x, idx) => { if (colors[idx]) { return { ...colors[idx], color: x }; } else { return wrapInBlankSemantics(x); } - }); + } + ); onChange(newColors); state = "idle"; } catch (e) { console.error(e); + errorMsg = e.message; state = "error"; return; } } $: includeQuotes = $configStore.includeQuotes; + $: console.log("state", state);
+
+ +
+ + configStore.setIncludeQuotes(!includeQuotes)} + /> +
+
{#if state === "error"} -
Error parsing colors
+
Error parsing colors. {errorMsg}
{/if}