From 3846ca4d170f4dd65fc2f8d026ed61af468531f8 Mon Sep 17 00:00:00 2001 From: Isaac Poole <55164207+isfopo@users.noreply.github.com> Date: Wed, 10 Jul 2024 12:32:46 -0400 Subject: [PATCH] infers boolean type, falls back to string --- src/helpers/parse.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/helpers/parse.ts b/src/helpers/parse.ts index e25ae28..f5208e7 100644 --- a/src/helpers/parse.ts +++ b/src/helpers/parse.ts @@ -34,33 +34,30 @@ export const parseEnvironmentContent = (lines: string): EnvironmentContent => { // Add to object obj[key] = { value, - type: parseArg(match[3], "type") ?? "string", - options: parseArg(match[3], "options")?.split(",") ?? [], + type: inferType(value), + options: parseOptions(match[3]), }; } return obj; }; -export const parseArg = ( - input: string | undefined, - key: string -): EnvironmentKeyValueType | null => { - if (!input) return null; +export const inferType = (value: string): EnvironmentKeyValueType => { + if (value === "true" || value === "false") { + return "bool"; + } else { + return "string"; + } +}; +export const parseOptions = (input: string | undefined): string[] => { // Create a regex that finds the key followed by a colon and captures the value - const regex = new RegExp(`${key}:([^\\s]*)`, "i"); + const regex = new RegExp(`options:([^\\s]*)`, "i"); // Execute the regex on the input string - const match = input.match(regex); - - // If a match is found, return the captured group (the value) - if (match && match[1]) { - return match[1] as EnvironmentKeyValueType; - } + const match = input?.match(regex); - // If no match is found, return null - return null; + return match?.[1].split(",") ?? []; }; export const replace = (content: string, key: string, value: string): string =>