<commit_before>var utils = require('./utils.js')

module.exports = function (controller, model, allModules) {
  var initialState = {}

  var registerSignals = function (moduleName, signals) {
    var scopedSignals = Object.keys(signals).reduce(function (scopedSignals, key) {
      scopedSignals[moduleName + '.' + key] = signals[key]
      return scopedSignals
    }, {})

    return controller.signals(scopedSignals, {
      modulePath: moduleName.split('.')
    })
  }

  var registerSignalsSync = function (moduleName, signals) {
    var scopedSignals = Object.keys(signals).reduce(function (scopedSignals, key) {
      scopedSignals[moduleName + '.' + key] = signals[key]
      return scopedSignals
    }, {})
    return controller.signalsSync(scopedSignals, {
      modulePath: moduleName.split('.')
    })
  }

  var registerServices = function (moduleName, services) {
    var scopedServices = Object.keys(services).reduce(function (scopedServices, key) {
      scopedServices[moduleName + '.' + key] = services[key]
      return scopedServices
    }, {})
    controller.services(scopedServices)
  }

  var registerInitialState = function (moduleName, state) {
    utils.setDeep(initialState, moduleName, state)
    model.mutators.set(moduleName.split('.'), state)
  }

  controller.on('reset', function () {
    model.mutators.merge([], initialState)
  })

  return function registerModules (parentModuleName, modules) {
    if (arguments.length === 1) {
      modules = parentModuleName
      parentModuleName = null
    }
    Object.keys(modules).forEach(function (moduleName) {
      var moduleConstructor = modules[moduleName]
      var actualName = moduleName
      if (parentModuleName) {
        moduleName = parentModuleName + '.' + moduleName
      }
      var moduleExport = {
        name: actualName,
        path: moduleName.split('.')
      }
      var module = {
        name: moduleName,
        alias: function (alias) {
          allModules[alias] = moduleExport
        },
        signals: registerSignals.bind(null, moduleName),
        signalsSync: registerSignalsSync.bind(null, moduleName),
        services: registerServices.bind(null, moduleName),
        state: registerInitialState.bind(null, moduleName),
        getSignals: function () {
          var signals = controller.getSignals()
          var path = moduleName.split('.')
          return path.reduce(function (signals, key) {
            return signals[key]
          }, signals)
        },
        modules: registerModules.bind(null, moduleName)
      }
      var constructedModule = moduleConstructor(module, controller)

      moduleExport.meta = constructedModule
      module.meta = constructedModule
      allModules[moduleName] = moduleExport

      return moduleExport
    })
    return allModules
  }
}
<commit_msg>refactor(registerModules): flatten function definitions<commit_after>var utils = require('./utils.js')

module.exports = function (controller, model, allModules) {
  var initialState = {}

  var registerSignals = function (moduleName, signals) {
    var scopedSignals = Object.keys(signals).reduce(function (scopedSignals, key) {
      scopedSignals[moduleName + '.' + key] = signals[key]
      return scopedSignals
    }, {})

    return controller.signals(scopedSignals, {
      modulePath: moduleName.split('.')
    })
  }

  var registerSignalsSync = function (moduleName, signals) {
    var scopedSignals = Object.keys(signals).reduce(function (scopedSignals, key) {
      scopedSignals[moduleName + '.' + key] = signals[key]
      return scopedSignals
    }, {})
    return controller.signalsSync(scopedSignals, {
      modulePath: moduleName.split('.')
    })
  }

  var registerServices = function (moduleName, services) {
    var scopedServices = Object.keys(services).reduce(function (scopedServices, key) {
      scopedServices[moduleName + '.' + key] = services[key]
      return scopedServices
    }, {})
    controller.services(scopedServices)
  }

  var registerInitialState = function (moduleName, state) {
    utils.setDeep(initialState, moduleName, state)
    model.mutators.set(moduleName.split('.'), state)
  }

  var registerModules = function (parentModuleName, modules) {
    if (arguments.length === 1) {
      modules = parentModuleName
      parentModuleName = null
    }

    Object.keys(modules).forEach(function (moduleName) {
      registerModule(moduleName, parentModuleName, modules)
    })

    return allModules
  }

  var registerModule = function (moduleName, parentModuleName, modules) {
    var moduleConstructor = modules[moduleName]
    var actualName = moduleName
    if (parentModuleName) {
      moduleName = parentModuleName + '.' + moduleName
    }
    var moduleExport = {
      name: actualName,
      path: moduleName.split('.')
    }
    var module = {
      name: moduleName,
      alias: function (alias) {
        allModules[alias] = moduleExport
      },
      signals: registerSignals.bind(null, moduleName),
      signalsSync: registerSignalsSync.bind(null, moduleName),
      services: registerServices.bind(null, moduleName),
      state: registerInitialState.bind(null, moduleName),
      getSignals: function () {
        var signals = controller.getSignals()
        var path = moduleName.split('.')
        return path.reduce(function (signals, key) {
          return signals[key]
        }, signals)
      },
      modules: registerModules.bind(null, moduleName)
    }
    var constructedModule = moduleConstructor(module, controller)

    moduleExport.meta = constructedModule
    module.meta = constructedModule
    allModules[moduleName] = moduleExport

    return moduleExport
  }

  controller.on('reset', function () {
    model.mutators.merge([], initialState)
  })

  return registerModules
}

{'fraction_non_alphanumeric': 0.11186030428769018, 'fraction_numerical': 0.00034578146611341634, 'mean_word_length': 3.1648668106551474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3717490', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1628, 'n_words': 338}