config: added config.has()

This commit is contained in:
Patrik Oldsberg
2020-08-08 11:56:30 +02:00
parent 1f2216fd77
commit 12827a29dc
2 changed files with 30 additions and 7 deletions
+22 -7
View File
@@ -30,6 +30,7 @@ const DATA = {
worstStrings: ['string1', 'string2', {}] as string[],
nested: {
one: 1,
null: null,
string: 'string',
strings: ['string1', 'string2'],
},
@@ -39,6 +40,13 @@ const DATA = {
function expectValidValues(config: ConfigReader) {
expect(config.keys()).toEqual(Object.keys(DATA));
expect(config.get('zero')).toBe(0);
expect(config.has('zero')).toBe(true);
expect(config.has('false')).toBe(true);
expect(config.has('null')).toBe(true);
expect(config.has('missing')).toBe(false);
expect(config.has('nested.one')).toBe(true);
expect(config.has('nested.missing')).toBe(false);
expect(config.has('nested.null')).toBe(true);
expect(config.getNumber('zero')).toBe(0);
expect(config.getNumber('one')).toBe(1);
expect(config.getOptional('true')).toBe(true);
@@ -50,6 +58,7 @@ function expectValidValues(config: ConfigReader) {
expect(config.getConfig('nested').getNumber('one')).toBe(1);
expect(config.get('nested')).toEqual({
one: 1,
null: null,
string: 'string',
strings: ['string1', 'string2'],
});
@@ -144,14 +153,14 @@ describe('ConfigReader', () => {
it('should throw on invalid keys', () => {
const config = new ConfigReader({}, CTX);
expect(() => config.getString('.')).toThrow(/^Invalid config key/);
expect(() => config.getString('0')).toThrow(/^Invalid config key/);
expect(() => config.getString('(')).toThrow(/^Invalid config key/);
expect(() => config.has('.')).toThrow(/^Invalid config key/);
expect(() => config.get('0')).toThrow(/^Invalid config key/);
expect(() => config.getOptional('(')).toThrow(/^Invalid config key/);
expect(() => config.getString('z-_')).toThrow(/^Invalid config key/);
expect(() => config.getString('-')).toThrow(/^Invalid config key/);
expect(() => config.getString('.a')).toThrow(/^Invalid config key/);
expect(() => config.getString('0.a')).toThrow(/^Invalid config key/);
expect(() => config.getString('0a')).toThrow(/^Invalid config key/);
expect(() => config.getOptionalString('-')).toThrow(/^Invalid config key/);
expect(() => config.getNumber('.a')).toThrow(/^Invalid config key/);
expect(() => config.getConfig('0.a')).toThrow(/^Invalid config key/);
expect(() => config.getOptionalConfig('0a')).toThrow(/^Invalid config key/);
expect(() => config.getString('a.0a')).toThrow(/^Invalid config key/);
expect(() => config.getString('a..a')).toThrow(/^Invalid config key/);
expect(() => config.getString('a.')).toThrow(/^Invalid config key/);
@@ -308,6 +317,12 @@ describe('ConfigReader with fallback', () => {
const config = new ConfigReader(a, CTX, new ConfigReader(b, CTX));
expect(config.keys()).toEqual(['merged']);
expect(config.has('merged.x')).toBe(true);
expect(config.has('merged.y')).toBe(true);
expect(config.has('merged.w')).toBe(false);
expect(config.getConfig('merged').has('x')).toBe(true);
expect(config.getConfig('merged').has('y')).toBe(true);
expect(config.getConfig('merged').has('w')).toBe(false);
expect(config.getConfig('merged').keys()).toEqual([
'x',
'z',
+8
View File
@@ -71,6 +71,14 @@ export class ConfigReader implements Config {
private readonly prefix: string = '',
) {}
has(key: string): boolean {
const value = this.readValue(key);
if (value !== undefined) {
return true;
}
return this.fallback?.has(key) ?? false;
}
keys(): string[] {
const localKeys = this.data ? Object.keys(this.data) : [];
const fallbackKeys = this.fallback?.keys() ?? [];