From 02ea1ce294f848719f2a76eadf3c6a2a2475f319 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fernando=20Lo=CC=81pez=20Guevara?= Date: Sun, 8 Jan 2023 13:13:45 -0300 Subject: [PATCH] feat(envconfig): add prefix override --- src/envconfig.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/envconfig.ts b/src/envconfig.ts index d6a75fd..a5ce66d 100644 --- a/src/envconfig.ts +++ b/src/envconfig.ts @@ -451,9 +451,10 @@ export const parse = < >( vars: T, config: C, - options: { debug?: boolean; prefix?: string[] } = { + options: { debug?: boolean; prefix?: string[]; suffix?: string[] } = { debug: false, prefix: undefined, + suffix: undefined, }, ): any => { if (typeof vars !== "object") { @@ -480,13 +481,29 @@ export const parse = < { suffix, prefix }: { suffix?: string[]; prefix?: string[] }, ) => { const varKey = varsKeys.find((k) => { - let tempKey = `${prefix?.join("") || ""}${key}${suffix?.join("") || ""}`; + let _prefix = undefined as string[] | undefined; + let _suffix = undefined as string[] | undefined; + + if (prefix?.length) { + _prefix = prefix?.filter(Boolean); + } + + if (suffix?.length) { + _suffix = suffix?.filter(Boolean); + } + + let tempKey = `${_prefix?.join("") || ""}${key}${ + _suffix?.join("") || "" + }`; if (k.toLowerCase() === tempKey.toLowerCase()) { return true; } - tempKey = `${prefix?.join("_") || ""}_${key}_${suffix?.join("_") || ""}`; + tempKey = `${_prefix?.join("_") || ""}_${key}_${ + _suffix?.join("_") || "" + }`; + tempKey = tempKey.endsWith("_") ? tempKey.substring(0, tempKey.length - 1) : tempKey; @@ -504,10 +521,17 @@ export const parse = < for (const [key, conf] of configs) { try { if (typeof conf === "object") { + const __prefix = conf.__prefix ?? key; + const __suffix = conf.__suffix ?? ""; + delete conf.__prefix; + delete conf.__suffix; + const child = parse({ ...__env }, conf, { ...options, - prefix: [...options.prefix || [], key] as string[], + prefix: [...options.prefix || [], __prefix] as string[], + suffix: [...options?.suffix || [], __suffix] as string[], }); + env[key] = child; } else { if (!conf) { @@ -1402,4 +1426,29 @@ describe("complex marks", () => { assertEquals(parsed.myapp.super.nest.port, 3003); }); + + it("should parse super nested `number with prefix override", () => { + const env = { + MYAPP_SUB_P1: "1", + MYAPP_SUB_P2: "2", + MYAPP_SUB_P3: "3", + }; + + const config = { + myapp: { + ignoredPrefix: { + __prefix: "", + sub: { + p1: "`number", + p2: "`number", + p3: "`number", + }, + }, + }, + }; + + const parsed = parse(env, config); + + assertEquals(parsed.myapp.ignoredPrefix.sub, { p1: 1, p2: 2, p3: 3 }); + }); });