Skip to content

Commit

Permalink
feat(envconfig): add aliases string[], number[], date[], bool[]
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolguevara committed Jan 10, 2023
1 parent 32ff255 commit eb9428f
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 2 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ this library is based on
- `format
```

### alises

```md
- `string[] =`string `split_values
- `bool[] =`bool `split_values
- `date[] =`date `split_values
- `number[] =`number `split_values
```

## Example

```typescript
Expand Down
168 changes: 166 additions & 2 deletions src/envconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ const invalidValueError = (r: string, k: string, c: string, v: unknown) =>
`should have a valid value ex: \`${r}:my-val, val:${JSON.stringify(v)}`,
);

const trueValues = ["y", "yes", "true", "t", "on", "1", true, 1];

const types = ["number", "date", "string", "bool"];

const aliases = {
"string[]": { type: "string", split_values: true },
"number[]": { type: "number", split_values: true },
"bool[]": { type: "bool", split_values: true },
"date[]": { type: "date", split_values: true },
};

const toggleMarks = Object.entries({
"split_values": undefined,
"uppercase": (key: string, conf: string, metadata: any) => {
Expand Down Expand Up @@ -107,6 +116,7 @@ const parseMarksConfig = (
} else if (typeof conf === "string") {
const metadata = {
type: "unkown",
alias: "unkown",
// key marks
key: key as string,
prefix: undefined as string[] | undefined,
Expand Down Expand Up @@ -198,6 +208,17 @@ const parseMarksConfig = (
}

const type = rune.substring(1);
const alias = aliases[type as keyof typeof aliases];
if (alias) {
metadata.alias = type;

for (const [key, value] of Object.entries(alias)) {
(metadata as any)[key] = value as any;
}

continue;
}

if (types.some((t) => type.startsWith(t))) {
if (metadata.type !== "unkown") {
throw duplicateMarkError(rune, key, conf);
Expand Down Expand Up @@ -332,7 +353,64 @@ describe("parseMarksConfig()", () => {
});
}

describe("`aliases", () => {
it("when use myEnvKey`string[], output should be {type:'string',split_values:true}", () => {
const var_key = "myEnvKey";
const metadataig = "`string[]";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.type, "string");
assertEquals(metadata.split_values, true);
assertEquals(metadata.alias, "string[]");
});

it("when use myEnvKey`bool[], output should be {type:'bool',split_values:true}", () => {
const var_key = "myEnvKey";
const metadataig = "`bool[]";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.type, "bool");
assertEquals(metadata.split_values, true);
assertEquals(metadata.alias, "bool[]");
});

it("when use myEnvKey`number[], output should be {type:'bool',split_values:true}", () => {
const var_key = "myEnvKey";
const metadataig = "`number[]";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.type, "number");
assertEquals(metadata.split_values, true);
assertEquals(metadata.alias, "number[]");
});

it("when use myEnvKey`date[], output should be {type:'date',split_values:true}", () => {
const var_key = "myEnvKey";
const metadataig = "`date[]";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.type, "date");
assertEquals(metadata.split_values, true);
assertEquals(metadata.alias, "date[]");
});
});

describe("`types modifiers", () => {
describe("`format mark", () => {
it("when use myEnvKey`format:my-format, output should be {format:'my-format'}", () => {
const var_key = "myEnvKey";
const metadataig = "`format:my-format";

const metadata = parseMarksConfig(var_key, metadataig);

assertEquals(metadata.format, "my-format");
});
});

describe("`string mark", () => {
it("when `string should parse", () => {
const var_key = "myEnvKey";
Expand Down Expand Up @@ -575,10 +653,12 @@ export const parse = <
case type === "bool":
if (split_values) {
env[key] = (val?.split(",") || "").map((v: string) =>
v === "true"
trueValues.includes(
v.toLowerCase(),
)
);
} else {
env[key] = ["y", "yes", "true", "t", "on", "1", true, 1].includes(
env[key] = trueValues.includes(
val.toLowerCase(),
);
}
Expand Down Expand Up @@ -1529,4 +1609,88 @@ describe("complex marks", () => {
},
});
});

describe("`aliases", () => {
it("should parse alias `number[] from env", () => {
const env = {
MYAPP_PORT: "1,2,3,4",
};

const config = {
myapp: {
port: "`number[]",
},
};

const parsed = parse(env, config);

assertEquals(parsed.myapp.port, [1, 2, 3, 4]);
});

it("should parse alias `string[] from env", () => {
const env = {
MYAPP_PORT: "1,2,3,4",
};

const config = {
myapp: {
port: "`string[]",
},
};

const parsed = parse(env, config);

assertEquals(parsed.myapp.port, ["1", "2", "3", "4"]);
});

it("should parse alias `bool[] from env", () => {
const env = {
MYAPP_PORT: "1,t,true,y,yes,on,0,off,no,n,false,f",
};

const config = {
myapp: {
myFlags: "`env:PORT`bool[]",
},
};

const parsed = parse(env, config);

assertEquals(parsed.myapp.myFlags, [
true,
true,
true,
true,
true,
true,
false,
false,
false,
false,
false,
false,
]);
});

it("should parse alias `date[] from env", () => {
const env = {
MYAPP_PORT:
"1989-05-19T00:00:00.000Z,1989-05-20T00:00:00.000Z,1989-05-21T00:00:00.000Z",
};

const config = {
myapp: {
myDates: "`env:PORT`date[]",
},
};

const parsed = parse(env, config);

assertEquals(parsed.myapp.myDates, [
new Date("1989-05-19T00:00:00.000Z"),
new Date("1989-05-20T00:00:00.000Z"),
new Date("1989-05-21T00:00:00.000Z"),
]);
});
});
});

0 comments on commit eb9428f

Please sign in to comment.