@@ -286,6 +286,26 @@ _.assign(Property, /** @lends Property */ {
286
286
return Substitutor . box ( variables , Substitutor . DEFAULT_VARS ) . parse ( str ) . toString ( ) ;
287
287
} ,
288
288
289
+ /**
290
+ * Similar to `replaceSubstitutions` but runs asynchronously
291
+ * and supports async value functions
292
+ *
293
+ * @param {String } str -
294
+ * @param {VariableList|Object|Array.<VariableList|Object> } variables -
295
+ * @returns {String }
296
+ */
297
+ // @todo : improve algorithm via variable replacement caching
298
+ replaceSubstitutionsLazy : async function ( str , variables ) {
299
+ // if there is nothing to replace, we move on
300
+ if ( ! ( str && _ . isString ( str ) ) ) { return str ; }
301
+
302
+ // if variables object is not an instance of substitutor then ensure that it is an array so that it becomes
303
+ // compatible with the constructor arguments for a substitutor
304
+ ! Substitutor . isInstance ( variables ) && ! _ . isArray ( variables ) && ( variables = _ . tail ( arguments ) ) ;
305
+
306
+ return ( await Substitutor . box ( variables , Substitutor . DEFAULT_VARS ) . parseLazy ( str ) ) . toString ( ) ;
307
+ } ,
308
+
289
309
/**
290
310
* This function accepts an object followed by a number of variable sources as arguments. One or more variable
291
311
* sources can be provided and it will use the one that has the value in left-to-right order.
@@ -319,6 +339,49 @@ _.assign(Property, /** @lends Property */ {
319
339
return _ . mergeWith ( { } , obj , customizer ) ;
320
340
} ,
321
341
342
+ /**
343
+ * Similar to `replaceSubstitutionsIn` but runs asynchronously
344
+ * and supports async value functions
345
+ *
346
+ * @param {Object } obj -
347
+ * @param {Array.<VariableList|Object> } variables -
348
+ * @returns {Object }
349
+ */
350
+ replaceSubstitutionsInLazy : async function ( obj , variables ) {
351
+ // if there is nothing to replace, we move on
352
+ if ( ! ( obj && _ . isObject ( obj ) ) ) {
353
+ return obj ;
354
+ }
355
+
356
+ // convert the variables to a substitutor object (will not reconvert if already substitutor)
357
+ variables = Substitutor . box ( variables , Substitutor . DEFAULT_VARS ) ;
358
+
359
+ const promises = [ ] ;
360
+ var customizer = function ( objectValue , sourceValue , key ) {
361
+ objectValue = objectValue || { } ;
362
+ if ( ! _ . isString ( sourceValue ) ) {
363
+ _ . forOwn ( sourceValue , function ( value , key ) {
364
+ sourceValue [ key ] = customizer ( objectValue [ key ] , value ) ;
365
+ } ) ;
366
+
367
+ return sourceValue ;
368
+ }
369
+
370
+ const result = this . replaceSubstitutionsLazy ( sourceValue , variables ) ;
371
+
372
+ promises . push ( { key : key , promise : result } ) ;
373
+
374
+ return result ;
375
+ } . bind ( this ) ,
376
+ res = _ . mergeWith ( { } , obj , customizer ) ;
377
+
378
+ await Promise . all ( promises . map ( async ( { key, promise } ) => {
379
+ res [ key ] = await promise ;
380
+ } ) ) ;
381
+
382
+ return res ;
383
+ } ,
384
+
322
385
/**
323
386
* This function recursively traverses a variable and detects all instances of variable replacements
324
387
* within the string of the object
0 commit comments