Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the arrayVar helper #26

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,36 @@ export function boolVar<R>(
);
}

/**
* Splits an env var using the provided delimiter (defaults to ',') into an array of individual string values.
* Optionally accepts an array of allowed values for the array.
*/
export const arrayVar = <S extends string>(
name: string,
{
delimiter = ',',
allowedValues,
}: {
delimiter?: string | RegExp;
allowedValues?: S[];
} = {},
): S[] | undefined => {
const rawValue = optionalVar(name);
if (rawValue == null) {
return rawValue;
}
const items = rawValue.split(delimiter);

const allowedValueSet =
allowedValues != null ? new Set<string>(allowedValues) : null;
if (allowedValueSet != null && items.some((v) => !allowedValueSet.has(v))) {
throw new Error(
`'${name}' must be a '${delimiter.toString()}' delimited string with one or more of the allowed values: ${[...allowedValueSet].toString()}`,
);
}
return items as S[];
};

export type HostPort = { host: string; port: number };
/**
* Splits an env var in the format of `${host1}:${port1}, ${host2}:${port2}, ...`
Expand Down
35 changes: 35 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
requiredVar,
hostPortsVar,
trustProxyVar,
arrayVar,
} from '../src';
import type { HostPort } from '../src';

Expand Down Expand Up @@ -118,6 +119,40 @@ for (const [testType, varTest] of varTests) {
});
});

describe('arrayVar', () => {
if (typeof varTest !== 'string') {
return;
}
it('should return undefined array when the env var is missing', () => {
expect(arrayVar(varTest)).to.be.undefined;
});
it('should return an array of one item when there is no delimiter in the string', () => {
setVar('value1');
expect(arrayVar(varTest)).to.deep.equal(['value1']);
});
it('should return an array of two items when the default delimiter is part of the string', () => {
setVar('value1,value2,value3');
expect(arrayVar(varTest)).to.deep.equal(['value1', 'value2', 'value3']);
});
it('should include leading & trailing whitespaces in the array values', () => {
setVar(', value1 , value2 ');
expect(arrayVar(varTest)).to.deep.equal(['', ' value1 ', ' value2 ']);
});
it('should return an array of two items when the provided delimiter is part of the string', () => {
setVar('val,ue1;val,ue2;val,ue3');
expect(arrayVar(varTest, { delimiter: ';' })).to.deep.equal([
'val,ue1',
'val,ue2',
'val,ue3',
]);
});
it('should throw when the array includes items that are not part of the allowed values', () => {
setVar('value1,value2,value3');
expect(() => arrayVar(varTest, { allowedValues: ['value1', 'value2'] }))
.to.throw;
});
});

describe('splitHostPort', () => {
const defaultValue: HostPort[] = [];
it('should throw when the env var is missing', () => {
Expand Down