<commit_before>module = angular.module('impac.components.widgets-settings.accounts-list', [])

# There is no template associated to this setting, and though it won't appear in the 'settings' panel
# However, as its metadata has to be initialized from, and saved to Impac!, we build ListAccounts as a setting
module.controller('SettingAccountsListCtrl', ($scope, ImpacWidgetsSvc) ->

  # ---------------------------------------------------------
  # ### Populate the widget
  # ---------------------------------------------------------

  w = $scope.parentWidget

  # Used by the 'delete' button in the accounts list and by the comboBox
  w.moveAccountToAnotherList = (account, src, dst, triggerUpdate=true) ->
    return if _.isEmpty(src) || _.isEmpty(account)
    dst ||= []
    _.remove src, (acc) -> account.uid == acc.uid
    dst.push(account)
    ImpacWidgetsSvc.updateWidgetSettings(w, false) if triggerUpdate
    return null

  # collection wrapper for w.moveAccountToAnotherList
  w.clearAccounts = (src, dst, triggerUpdate=false) ->
    srcCopy = angular.copy(src)
    _.forEach(srcCopy, (account) -> w.moveAccountToAnotherList(account, src, dst, triggerUpdate))
    return null

  # keeps local of src accounts for switching between grouped and ungrouped.
  stashedAccounts = []
  # groups accounts by a string matcher, from a src collection, into a destination collection.
  # note: src collection can be the dst collection. Src will be cleared.
  w.groupAccounts = (src, dst, groupByKey, regExp) ->
    grouped = []
    counter = 0
    rgx = new RegExp(regExp || /[^a-z0-9]/g)
    stashedAccounts = angular.copy(src)
    normalise = (str) -> str.toLowerCase().replace(rgx, "")
    collectBalance = (arr) -> _.reduce(arr, (total, n) -> total + n )
    sort = ->
      baseAccount = src.shift()
      matcher = normalise(baseAccount[groupByKey])
      group = { name: baseAccount.account_name, uid: counter++ }
      group.accounts = _.select src, (acc, index) ->
        src.splice(index, 1)[0] if !!acc && normalise(acc[groupByKey]) == matcher
      if group.accounts.length > 0
        group.accounts.unshift(baseAccount)
        # TODO::multi-currency: support for conversions across multi-currency, multi-company accounts.
        group.current_balance = collectBalance(_.map(group.accounts, (acc) -> acc.current_balance))
        group.currency = _.map(group.accounts, (acc) -> acc.currency).join('/')
        grouped.push(group)
      unless src.length
        _.forEach(grouped, (acc) -> dst.push(acc) )
      else
        sort()<commit_msg>minor update for impac api minor refactor<commit_after>module = angular.module('impac.components.widgets-settings.accounts-list', [])

# There is no template associated to this setting, and though it won't appear in the 'settings' panel
# However, as its metadata has to be initialized from, and saved to Impac!, we build ListAccounts as a setting
module.controller('SettingAccountsListCtrl', ($scope, ImpacWidgetsSvc) ->

  # ---------------------------------------------------------
  # ### Populate the widget
  # ---------------------------------------------------------

  w = $scope.parentWidget

  # Used by the 'delete' button in the accounts list and by the comboBox
  w.moveAccountToAnotherList = (account, src, dst, triggerUpdate=true) ->
    return if _.isEmpty(src) || _.isEmpty(account)
    dst ||= []
    _.remove src, (acc) -> account.uid == acc.uid
    dst.push(account)
    ImpacWidgetsSvc.updateWidgetSettings(w, false) if triggerUpdate
    return null

  # collection wrapper for w.moveAccountToAnotherList
  w.clearAccounts = (src, dst, triggerUpdate=false) ->
    srcCopy = angular.copy(src)
    _.forEach(srcCopy, (account) -> w.moveAccountToAnotherList(account, src, dst, triggerUpdate))
    return null

  # keeps local of src accounts for switching between grouped and ungrouped.
  stashedAccounts = []
  # groups accounts by a string matcher, from a src collection, into a destination collection.
  # note: src collection can be the dst collection. Src will be cleared.
  w.groupAccounts = (src, dst, groupByKey, regExp) ->
    grouped = []
    counter = 0
    rgx = new RegExp(regExp || /[^a-z0-9]/g)
    stashedAccounts = angular.copy(src)
    normalise = (str) -> str.toLowerCase().replace(rgx, "")
    collectBalance = (arr) -> _.reduce(arr, (total, n) -> total + n )
    sort = ->
      baseAccount = src.shift()
      matcher = normalise(baseAccount[groupByKey])
      group = { name: baseAccount.account_name || baseAccount.name, uid: counter++ }
      group.accounts = _.select src, (acc, index) ->
        src.splice(index, 1)[0] if !!acc && normalise(acc[groupByKey]) == matcher
      if group.accounts.length > 0
        group.accounts.unshift(baseAccount)
        # TODO::multi-currency: support for conversions across multi-currency, multi-company accounts.
        group.current_balance = collectBalance(_.map(group.accounts, (acc) -> acc.current_balance))
        group.currency = _.map(group.accounts, (acc) -> acc.currency).join('/')
        grouped.push(group)
      unless src.length
        _.forEach(grouped, (acc) -> dst.push(acc) )
      else
        sort()
{'fraction_non_alphanumeric': 0.16124661246612465, 'fraction_numerical': 0.0023228803716608595, 'mean_word_length': 2.8559701492537313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, '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': '7252420', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1477, 'n_words': 426}