Skip to content

Commit

Permalink
Merge pull request #15 from isfopo:issue/Infer-bool-type-from-value-t…
Browse files Browse the repository at this point in the history
…rue-or-false

infers boolean type, falls back to string
  • Loading branch information
isfopo authored Jul 10, 2024
2 parents 929431d + 3846ca4 commit d65b5e6
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions src/helpers/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down

0 comments on commit d65b5e6

Please sign in to comment.