Skip to content

Commit 0266796

Browse files
author
Ovidiu Barabula
committed
feat(config): add remove method and support for setting multiple keys at once
1 parent 10a077d commit 0266796

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

src/config-manager/index.spec.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ describe('ConfigManager', () => {
99
const configFile = 'package.json';
1010
let configManager: IConfigManager;
1111

12+
1213
beforeEach(async () => configManager = await ConfigManagerFactory('frontvue'));
1314

1415

1516
it('instantiates', async () => {
16-
expect(configManager).to.be.an('object').to.have.all.keys('has', 'set', 'get');
17+
expect(configManager).to.be.an('object').to.have.all.keys('get', 'has', 'remove', 'set');
1718
});
1819

1920

@@ -32,26 +33,55 @@ describe('ConfigManager', () => {
3233
};
3334

3435
const customConfigManager = await ConfigManagerFactory('frontvue', customReader);
35-
expect(customConfigManager).to.be.an('object').to.have.all.keys('has', 'set', 'get');
36+
expect(customConfigManager).to.be.an('object').to.have.all.keys('get', 'has', 'remove', 'set');
3637
});
3738

3839

3940
it('gets all options', async () => {
40-
expect(configManager.get()).to.be.an('object');
41+
expect(await configManager.get()).to.be.an('object');
4142
});
4243

4344

4445
it('sets an option', async () => {
4546
configManager.set('key', 'value');
46-
expect(configManager.get('key')).to.equal('value');
47+
expect(await configManager.get('key')).to.equal('value');
4748
});
4849

49-
it('gets one option', async () => {
50-
expect(configManager.get('key')).to.equal('value');
50+
51+
it('sets an object of options', async () => {
52+
const saved = await configManager.set({
53+
key2: 'value2',
54+
key3: 'value3',
55+
});
56+
expect(await configManager.has('key2')).to.be.true;
57+
expect(await configManager.has('key3')).to.be.true;
58+
});
59+
60+
61+
it('returns false if not called properly', async () => {
62+
expect(await configManager.set({})).to.be.false;
5163
});
5264

5365

5466
it('checks if option exists', async () => {
55-
expect(configManager.has('key')).to.be.true;
67+
expect(await configManager.has('key')).to.be.true;
68+
});
69+
70+
71+
it('gets one option', async () => {
72+
expect(await configManager.get('key')).to.equal('value');
73+
});
74+
75+
76+
it('removes one option', async () => {
77+
const removed = await configManager.remove('key');
78+
const config = await configManager.get();
79+
expect(Object.keys(config).includes('key')).to.be.false;
80+
});
81+
82+
83+
it('removes multiple options', async () => {
84+
const removed = await configManager.remove('key2', 'key3');
85+
expect(await configManager.get()).to.eql({});
5686
});
5787
});

src/config-manager/index.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import PackageJsonConfigReader, {
1313

1414

1515
export interface IConfigManager {
16-
has(key: string): boolean;
17-
get(key?: string): any;
18-
set(key: string, value: any): Promise<boolean>;
16+
has(key: string): Promise<boolean>;
17+
get(key?: string): Promise<Config | any>;
18+
set(option: Config | string, value?: any): Promise<boolean>;
19+
remove(...options: string[]): Promise<boolean>;
1920
}
2021

2122
/**
@@ -39,14 +40,15 @@ async function ConfigManagerFactory(
3940
}
4041

4142
// Get the configuration contents
42-
const config: Config = await configReader.fetch();
43+
let config: Config = await configReader.fetch();
4344

4445

4546
/**
4647
* Check if key exists in config
4748
* @param key Configuration option key
4849
*/
49-
function has(key: string): boolean {
50+
async function has(key: string): Promise<boolean> {
51+
config = await configReader.fetch();
5052
return config.hasOwnProperty(key);
5153
}
5254

@@ -55,7 +57,9 @@ async function ConfigManagerFactory(
5557
* Retrieve value from configuration
5658
* @param key Configuration option key
5759
*/
58-
function get(key?: string): Config | any {
60+
async function get(key?: string): Promise<Config | any> {
61+
config = await configReader.fetch();
62+
5963
if (typeof key === 'undefined') {
6064
return config;
6165
}
@@ -65,20 +69,42 @@ async function ConfigManagerFactory(
6569

6670

6771
/**
68-
* Set new value for configuration option
69-
* @param key Configuration option key
70-
* @param value New value to be set
72+
* Set new value(s) in configuration
73+
* @param option Configuration object or object key
74+
* @param value New value to be set if option is an object key ("string")
7175
*/
72-
async function set(key: string, value: any): Promise<boolean> {
73-
config[key] = value;
74-
const saved = await configReader.update(config);
76+
async function set(option: Config | string, value?: any): Promise<boolean> {
77+
// If we're passing in a "key" and a "value"
78+
if (typeof option === 'string' && typeof value !== 'undefined') {
79+
config[option] = value;
80+
// If we're passing in an object of key/value pairs
81+
} else if (typeof option === 'object' && Object.keys(option).length > 0) {
82+
config = { ...config, ...option };
83+
} else {
84+
return false;
85+
}
86+
87+
const saved: boolean = await configReader.update(config);
7588
return saved;
7689
}
7790

91+
92+
/**
93+
* Removes one or more options from config
94+
* @param option Option name or array of option names
95+
*/
96+
async function remove(...options: string[]): Promise<boolean> {
97+
options.forEach(item => delete config[item]);
98+
const saved: boolean = await configReader.update(config);
99+
return saved;
100+
}
101+
102+
78103
// Returning config manager public API
79104
return Object.freeze({
80105
get,
81106
has,
107+
remove,
82108
set,
83109
});
84110
}

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