src/parsetree.js
function makeRhsExpansionPromise (config) {
  var pt = this
  var rhs = config.rhs || this.sampleParseTree (config.parsedRhsText || parseRhs (config.rhsText), config)
  var resolve = config.sync ? syncPromiseResolve : Promise.resolve.bind(Promise)
  var maxLength = config.maxLength || pt.maxLength
  var maxNodes = config.maxNodes || pt.maxNodes
  var reduce = config.reduce || textReducer
  var makeExpansionPromise = config.makeExpansionPromise || pt.makeExpansionPromise
  var init = extend ({ text: '',
                       vars: config.vars,
                       tree: [],
                       nodes: 0 },
                     config.init)
  return rhs.reduce (function (promise, child) {
    return promise.then (function (expansion) {
      if ((expansion.text && expansion.text.length >= maxLength)
          || (expansion.nodes && expansion.nodes >= maxNodes)) {
        return expansion
      }
      return makeExpansionPromise.call (pt,
                                        extend ({},
                                                config,
                                                { node: child,
                                                  vars: expansion.vars }))
        .then (function (childExpansion) {
          return reduce.call (pt, expansion, childExpansion, config)
        })
    })
  }, resolve (init))
}
function makeRhsExpansionPromise (config) {
  var pt = this
  var rhs = config.rhs || this.sampleParseTree (config.parsedRhsText || parseRhs (config.rhsText), config)
  var resolve = config.sync ? syncPromiseResolve : Promise.resolve.bind(Promise)
  var maxLength = config.maxLength || pt.maxLength
  var maxNodes = config.maxNodes || pt.maxNodes
  var reduce = config.reduce || textReducer
  var makeExpansionPromise = config.makeExpansionPromise || pt.makeExpansionPromise
  var init = extend ({ text: '',
                       vars: config.vars,
                       tree: [],
                       nodes: 0 },
                     config.init)
  return rhs.reduce (function (promise, child) {
    return promise.then (function (expansion) {
      if ((expansion.text && expansion.text.length >= maxLength)
          || (expansion.nodes && expansion.nodes >= maxNodes)) {
        return expansion
      }
      return makeExpansionPromise.call (pt,
                                        extend ({},
                                                config,
                                                { node: child,
                                                  vars: expansion.vars }))
        .then (function (childExpansion) {
          return reduce.call (pt, expansion, childExpansion, config)
        })
    })
  }, resolve (init))
}
function makeRhsExpansionPromise ( config ) { var pt = this var rhs = config . rhs || this . sampleParseTree ( config . parsedRhsText || parseRhs ( config . rhsText ) , config ) var resolve = config . sync ? syncPromiseResolve : Promise . resolve . bind ( Promise ) var maxLength = config . maxLength || pt . maxLength var maxNodes = config . maxNodes || pt . maxNodes var reduce = config . reduce || textReducer var makeExpansionPromise = config . makeExpansionPromise || pt . makeExpansionPromise var init = extend ( { text : '' , vars : config . vars , tree : [ ] , nodes : 0 } , config . init ) return rhs . reduce ( function ( promise , child ) { return promise . then ( function ( expansion ) { if ( ( expansion . text && expansion . text . length >= maxLength ) || ( expansion . nodes && expansion . nodes >= maxNodes ) ) { return expansion } return makeExpansionPromise . call ( pt , extend ( { } , config , { node : child , vars : expansion . vars } ) ) . then ( function ( childExpansion ) { return reduce . call ( pt , expansion , childExpansion , config ) } ) } ) } , resolve ( init ) ) }
makeRhsExpansionPromise is the main method for asynchronously expanding a template that may already have been partially expanded using sampleParseTree.
makeRhsExpansionPromise is the main method for asynchronously expanding a template that may already have been partially expanded using sampleParseTree .