Skip to content

Commit a680ae5

Browse files
authored
Merge pull request #2 from codedeviate/2.3.1
Added getAsString
2 parents 5157972 + 05645b0 commit a680ae5

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ If the parameter throwOnDefault is set to true then an exception will be thrown
162162
If the key is omitted (or set to undefined) the config object wil be returned.
163163

164164

165+
### getAsString(key, defaultValue = "", throwOnDefault = false): value
166+
The same as *get* but returns a string.
167+
168+
NULL will be returned as an empty string.
169+
170+
165171
### getConfig(key, defaultValue = null): [value, keyFound]
166172
Will try to find the key in the config.
167173

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
v2.3.1
22
* Updated README to reflect changes in the priority of config paths
33
* Fixed a bug in fuseAll to correctly add all subkeys
4+
* Added getAsString
45

56
v2.3.0 - Released 2022-11-25
67
This is the first official release of this package so everything is new.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare class DynamicConfig {
77
hasEnv(key: string): boolean;
88
hasConfig(key: string): boolean;
99
get(key: string = undefined, defaultValue: any = null, throwOnDefault: boolean = false): any;
10+
getAsString(key: string = undefined, defaultValue: any = null, throwOnDefault: boolean = false): string;
1011
getEnv(key: string, defaultValue: any = null): [any, boolean];
1112
getConfig(key: string, defaultValue: any = null): [any, boolean];
1213
addFuse(key: string): void;

index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ class DynamicConfig {
242242
return defaultValue;
243243
}
244244

245+
getAsString(key, defaultValue = null, throwOnDefault = false) {
246+
const value = this.get(key, defaultValue, throwOnDefault);
247+
if (value === null) {
248+
return "";
249+
}
250+
return value.toString();
251+
}
252+
245253
set(key, value) {
246254
const keys = key.split(this.configSplit || this.config['__configSplit'] || '.');
247255
if (this.fuseList[keys.join('.')] === true) {

0 commit comments

Comments
 (0)