Skip to content

Commit

Permalink
feat(envconfig): add boolean alias
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolguevara committed Jan 17, 2023
1 parent 46206e9 commit 0bb6179
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
4 changes: 1 addition & 3 deletions examples/envconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ const envconfig = {
},
} as const;

const out = parse(env, envconfig);

type OutType = typeof out;
const out = parse<any, typeof envconfig>(env, envconfig);

// bool[]
out.myapp.bool.optionalAliasArray;
Expand Down
29 changes: 13 additions & 16 deletions src/envconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const aliases = {
"number[]": { type: "number", split_values: true },
"bool[]": { type: "bool", split_values: true },
"date[]": { type: "date", split_values: true },
"boolean": { type: "bool" },
};

const toggleMarks = Object.entries({
Expand Down Expand Up @@ -361,6 +362,16 @@ describe("parseMarksConfig()", () => {
}

describe("`aliases", () => {
it("when use myEnvKey`boolean, output should be {type:'bool'}", () => {
const var_key = "myEnvKey";
const metadataig = "`boolean";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.type, "bool");
assertEquals(metadata.alias, "boolean");
});

it("when use myEnvKey`string[], output should be {type:'string',split_values:true}", () => {
const var_key = "myEnvKey";
const metadataig = "`string[]";
Expand Down Expand Up @@ -599,21 +610,6 @@ type DateMarkExtractor<Code extends string> = GenericTypeMarkExtractor<
Date
>;

// type Bool2MarkExtractor<Code extends string> = IncludesMark<
// `${Code}`,
// "`bool[]",
// RequiredMark<`${Code}`, boolean[], boolean[] | undefined>,
// IncludesMark<
// `${Code}`,
// "`bool",
// RequiredMark<
// `${Code}`,
// SplitValuesMark<`${Code}`, boolean[], boolean>,
// SplitValuesMark<`${Code}`, boolean[] | undefined, boolean | undefined>
// >
// >
// >;

type TypeMarkExtractor<Code extends string> =
& BoolMarkExtractor<`${Code}`>
& NumberMarkExtractor<`${Code}`>
Expand All @@ -631,6 +627,7 @@ type Output<T extends Config = Config> = {
export const parse = <
T = unknown,
C extends Config = Config,
O extends Output<C> = Output<C>,
>(
vars?: T,
config?: C,
Expand All @@ -639,7 +636,7 @@ export const parse = <
prefix: undefined,
suffix: undefined,
},
): Output<C> => {
): O => {
if (typeof vars !== "object") {
throw new ArgumentInvalidError("vars argument need to be an object");
}
Expand Down

0 comments on commit 0bb6179

Please sign in to comment.