Skip to content

Commit 1c6ab59

Browse files
author
Ovidiu Barabula
committed
feat(core): add config manager getter support for multiple keys
1 parent 3b65762 commit 1c6ab59

File tree

2 files changed

+61
-8
lines changed

2 files changed

+61
-8
lines changed

src/config-manager/index.spec.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import * as path from 'path';
44
import { stdout } from 'test-console';
55
import Logger from '../util/logger';
66
import ConfigManager, { ERRORS, IConfigManager } from './index';
7-
import { Config, ConfigReader } from './package-json-config-reader';
7+
import { Config } from './index';
8+
import { ConfigReader } from './package-json-config-reader';
89

910
describe('ConfigManager', () => {
1011
let configManager: IConfigManager;
@@ -62,6 +63,39 @@ describe('ConfigManager', () => {
6263
});
6364

6465

66+
it('gets the keys from the passed array', async () => {
67+
await configManager.set({
68+
'plugin:optionA': 'valueA',
69+
'plugin:optionB': 'valueB',
70+
'plugin:optionC': 'valueC',
71+
});
72+
73+
const validKeys = ['plugin:optionA', 'plugin:optionB', 'plugin:optionC'];
74+
expect(await configManager.get(validKeys))
75+
.to.be.an('object')
76+
.to.contain.keys(...validKeys);
77+
78+
await configManager.remove(...validKeys);
79+
});
80+
81+
82+
it('gets only the existing keys from passed array', async () => {
83+
await configManager.set({
84+
'plugin:optionA': 'valueA',
85+
'plugin:optionB': 'valueB',
86+
'plugin:optionC': 'valueC',
87+
});
88+
89+
const validKeys = ['plugin:optionA', 'plugin:optionB', 'plugin:optionC'];
90+
const mixedKeys = [...validKeys, 'plugin:optionD', 'plugin:optionE'];
91+
expect(await configManager.get(mixedKeys))
92+
.to.be.an('object')
93+
.to.contain.keys(...validKeys);
94+
95+
await configManager.remove(...validKeys);
96+
});
97+
98+
6599
it('sets an option', async () => {
66100
configManager.set('key', 'value');
67101
expect(await configManager.get('key')).to.equal('value');
@@ -78,7 +112,7 @@ describe('ConfigManager', () => {
78112
});
79113

80114

81-
it('returns false if not called properly', async () => {
115+
it('returns false if .set() is not called properly', async () => {
82116
expect(await configManager.set({})).to.be.false;
83117
});
84118

src/config-manager/index.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface Config {
1818

1919
export interface IConfigManager {
2020
has(key: string): Promise<boolean>;
21-
get(key?: string): Promise<Config | any>;
21+
get(key?: string | string[]): Promise<Config | any>;
2222
set(option: Config | string, value?: any): Promise<boolean|Error>;
2323
remove(...options: string[]): Promise<boolean|Error>;
2424
errorHandler?(error?: Error): Error;
@@ -92,13 +92,33 @@ async function ConfigManager(
9292
* Retrieve value from configuration
9393
* @param key Configuration option key
9494
*/
95-
async function get(key?: string): Promise<Config | any> {
95+
async function get(key?: string | string[]): Promise<Config | any> {
9696
config = await configReader.fetch();
9797

98+
// If no key is specified, return the entire config object
9899
if (typeof key === 'undefined') {
99100
return config;
100101
}
101102

103+
// If an array is passed, return an object
104+
// with the all the existing config keys from the array
105+
if (Array.isArray(key)) {
106+
// Assigning array to new variable so the naming makes sense
107+
const keys = key;
108+
// Create a new object with all available keys in the configuration
109+
return keys.reduce((accumConfig, currKey) => {
110+
// Check if configuration has provided key
111+
if (typeof config[currKey] !== 'undefined') {
112+
// Add the key value pair to the returning object
113+
accumConfig = {...accumConfig,
114+
...{ [currKey]: config[currKey] },
115+
};
116+
}
117+
return accumConfig;
118+
}, {});
119+
}
120+
121+
// Otherwise, return just the specified key
102122
return config[key];
103123
}
104124

@@ -120,8 +140,7 @@ async function ConfigManager(
120140
}
121141

122142
try {
123-
const saved: boolean | Error = await configReader.update(config);
124-
return saved;
143+
return await configReader.update(config);
125144
} catch (error) {
126145
return Promise.reject(errorHandler(error));
127146
}
@@ -135,13 +154,13 @@ async function ConfigManager(
135154
async function remove(...options: string[]): Promise<boolean|Error> {
136155
options.forEach(item => delete config[item]);
137156
try {
138-
const saved: boolean | Error = await configReader.update(config);
139-
return saved;
157+
return await configReader.update(config);
140158
} catch (error) {
141159
return Promise.reject(errorHandler(error));
142160
}
143161
}
144162

163+
145164
// Creating the public API object
146165
let publicApi: IConfigManager = {
147166
get,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy