@@ -107,6 +107,7 @@ export class ParsedValue {
107
107
return parseValue ( raw , new LiteralSource ( raw ) , extensions ) ;
108
108
}
109
109
110
+ /** Deep merge two ParsedValue objects */
110
111
static merge ( a : ParsedValue , b : ParsedValue ) {
111
112
const meta = merge ( a . meta , b . meta ) ;
112
113
@@ -161,6 +162,7 @@ export class ParsedValue {
161
162
return new ParsedValue ( [ ...a . sources , ...b . sources ] , newRawValue , newValue ) . assignMeta ( meta ) ;
162
163
}
163
164
165
+ /** Returns the first ConfigSource that is of some instance type */
164
166
getSource < CS extends ConfigSource > ( clazz : new ( ...args : any [ ] ) => CS ) : CS | undefined {
165
167
for ( const source of this . sources ) {
166
168
if ( source instanceof clazz ) {
@@ -169,6 +171,7 @@ export class ParsedValue {
169
171
}
170
172
}
171
173
174
+ /** Returns the first ConfigSource that is of some instance type */
172
175
assertSource < CS extends ConfigSource > ( clazz : new ( ...args : any [ ] ) => CS ) : CS {
173
176
const source = this . getSource ( clazz ) ;
174
177
@@ -179,6 +182,7 @@ export class ParsedValue {
179
182
throw new AppConfigError ( `Failed to find ConfigSource ${ clazz . name } ` ) ;
180
183
}
181
184
185
+ /** Returns all ConfigSource objects that contributed to this value (including nested) */
182
186
allSources ( ) : Set < ConfigSource > {
183
187
const sources = new Set ( this . sources ) ;
184
188
@@ -201,11 +205,13 @@ export class ParsedValue {
201
205
return sources ;
202
206
}
203
207
208
+ /** Adds metadata to the ParsedValue */
204
209
assignMeta ( metadata : ParsedValueMetadata ) {
205
210
Object . assign ( this . meta , metadata ) ;
206
211
return this ;
207
212
}
208
213
214
+ /** Removes metadata by key */
209
215
removeMeta ( key : string ) {
210
216
delete this . meta [ key ] ;
211
217
return this ;
@@ -226,38 +232,46 @@ export class ParsedValue {
226
232
return this . value [ key ] ?. property ( rest ) ;
227
233
}
228
234
235
+ /** Returns JSON object if the value is one */
229
236
asObject ( ) : { [ key : string ] : ParsedValue } | undefined {
230
237
if ( typeof this . value === 'object' && this . value !== null && ! Array . isArray ( this . value ) ) {
231
238
return this . value ;
232
239
}
233
240
}
234
241
242
+ /** Returns JSON array if the value is one */
235
243
asArray ( ) : ParsedValue [ ] | undefined {
236
244
if ( Array . isArray ( this . value ) ) return this . value ;
237
245
}
238
246
247
+ /** Returns JSON primitive value if the value is one */
239
248
asPrimitive ( ) : JsonPrimitive | undefined {
240
249
if ( ( typeof this . value !== 'object' || this . value === null ) && ! Array . isArray ( this . value ) ) {
241
250
return this . value ;
242
251
}
243
252
}
244
253
254
+ /** Returns if the underlying value is an object */
245
255
isObject ( ) : boolean {
246
256
return this . asObject ( ) !== undefined ;
247
257
}
248
258
259
+ /** Returns if the underlying value is an array */
249
260
isArray ( ) : boolean {
250
261
return this . asArray ( ) !== undefined ;
251
262
}
252
263
264
+ /** Returns if the underlying value is a primitive */
253
265
isPrimitive ( ) : boolean {
254
266
return this . asPrimitive ( ) !== undefined ;
255
267
}
256
268
269
+ /** Deep clones underlying value */
257
270
clone ( ) : ParsedValue {
258
271
return this . cloneWhere ( ( ) => true ) ;
259
272
}
260
273
274
+ /** Deep clones underlying value, depending on a predicate function */
261
275
cloneWhere ( filter : ( value : ParsedValue ) => boolean ) : ParsedValue {
262
276
if ( Array . isArray ( this . value ) ) {
263
277
const filtered = this . value . filter ( filter ) ;
@@ -293,6 +307,7 @@ export class ParsedValue {
293
307
return new ParsedValue ( this . sources , this . raw , this . value ) ;
294
308
}
295
309
310
+ /** Calls the function, with every nested ParsedValue */
296
311
visitAll ( callback : ( value : ParsedValue ) => void ) {
297
312
callback ( this ) ;
298
313
@@ -307,6 +322,7 @@ export class ParsedValue {
307
322
}
308
323
}
309
324
325
+ /** Extracts underlying JSON value from the wrapper */
310
326
toJSON ( ) : Json {
311
327
if ( Array . isArray ( this . value ) ) {
312
328
return this . value . map ( ( v ) => v . toJSON ( ) ) ;
0 commit comments