<commit_before>var httpify = require('httpify')
var jsend = require('jsend')
var qs = require('querystring')

function req (url, body, xhrOptions, callback, deconstruct) {
  var options = {
    method: 'POST',
    url: url,
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(body)
  }

  // extend the options object if necessary
  for (var option in xhrOptions) {
    options[option] = xhrOptions[option]
  }

  httpify(options, function (err, res) {
    if (err) return callback(err)
    if (res.statusCode !== 200) return callback(new Error('Error ' + res.statusCode))

    var body = res.body
    if (!jsend.isValid(body)) return callback(new Error('Invalid JSend response'))
    if (body.status === 'fail' || body.status === 'error') {
      return callback(new Error(body.message || body.data))
    }

    return callback(undefined, deconstruct ? body.data[0] : body.data)
  })
}

function Node (base, defaultParams) {
  var params = defaultParams ? ('?' + qs(defaultParams)) : ''
  var self = this

  this.addresses = {
    summary: function (addresses, callback) {
      req(base + '/addresses/summary' + params, {
        addresses: [].concat(addresses)
      }, self.xhrOptions, callback, !Array.isArray(addresses))
    },

    transactions: function (addresses, blockHeight, callback) {
      // optional blockHeight
      if ('function' === typeof blockHeight) {
        callback = blockHeight
        blockHeight = 0
      }

      req(base + '/addresses/transactions' + params, {
        addresses: [].concat(addresses),
        blockHeight: blockHeight
      }, self.xhrOptions, callback)
    },

    unspents: function (addresses, callback) {
      req(base + '/addresses/unspents' + params, {
        addresses: [].concat(addresses)
      }, self.xhrOptions, callback)
    }
  }

  this.blocks = {
    get: function (blockIds, callback) {
      req(base + '/blocks/get' + params, {
        blockIds: [].concat(blockIds)
      }, self.xhrOptions, callback, !Array.isArray(blockIds))
    },

    latest: function (callback) {
      req(base + '/blocks/latest' + params, {}, self.xhrOptions, callback)
    },

    propagate: function (blockHex, callback) {
      req(base + '/blocks/propagate' + params, {
        blockHex: blockHex
      }, self.xhrOptions, callback)
    },

    summary: function (blockIds, callback) {
      req(base + '/blocks/summary' + params, {
        blockIds: [].concat(blockIds)
      }, self.xhrOptions, callback, !Array.isArray(blockIds))
    }
  }

  this.transactions = {
    get: function (txIds, callback) {
      req(base + '/transactions/get' + params, {
        txIds: [].concat(txIds)
      }, self.xhrOptions, callback, !Array.isArray(txIds))
    },

    latest: function (callback) {
      req(base + '/transactions/latest' + params, {}, self.xhrOptions, callback)
    },

    propagate: function (txHex, callback) {
      req(base + '/transactions/propagate' + params, {
        txHexs: [].concat(txHex)
      }, self.xhrOptions, callback)
    },

    summary: function (txIds, callback) {
      req(base + '/transactions/summary' + params, {
        txIds: [].concat(txIds)
      }, self.xhrOptions, callback, !Array.isArray(txIds))
    }
  }

  this.xhrOptions = {}
}

module.exports = Node<commit_msg>index: simpler use of closures for less duplication<commit_after>var httpify = require('httpify')
var jsend = require('jsend')
var qs = require('querystring')

function Node (base, queryParams) {
  var queryString = queryParams ? ('?' + qs(queryParams)) : ''
  var self = this

  function req (url, body, callback, deconstruct) {
    var options = {
      method: 'POST',
      url: base + url + queryString,
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body)
    }

    // extend the options object if necessary
    for (var option in self.xhrOptions) {
      options[option] = self.xhrOptions[option]
    }

    httpify(options, function (err, res) {
      if (err) return callback(err)
      if (res.statusCode !== 200) return callback(new Error('Error ' + res.statusCode))

      var body = res.body
      if (!jsend.isValid(body)) return callback(new Error('Invalid JSend response'))
      if (body.status === 'fail' || body.status === 'error') {
        return callback(new Error(body.message || body.data))
      }

      return callback(undefined, deconstruct ? body.data[0] : body.data)
    })
  }

  this.addresses = {
    summary: function (addresses, callback) {
      req('/addresses/summary', { addresses: [].concat(addresses) }, callback, !Array.isArray(addresses))
    },

    transactions: function (addresses, blockHeight, callback) {
      // optional blockHeight
      if ('function' === typeof blockHeight) {
        callback = blockHeight
        blockHeight = 0
      }

      req('/addresses/transactions', { addresses: [].concat(addresses), blockHeight: blockHeight }, callback)
    },

    unspents: function (addresses, callback) {
      req('/addresses/unspents', { addresses: [].concat(addresses) }, callback)
    }
  }

  this.blocks = {
    get: function (blockIds, callback) {
      req('/blocks/get', { blockIds: [].concat(blockIds) }, callback, !Array.isArray(blockIds))
    },

    latest: function (callback) {
      req('/blocks/latest', {}, callback)
    },

    propagate: function (blockHex, callback) {
      req('/blocks/propagate', { blockHex: blockHex }, callback)
    },

    summary: function (blockIds, callback) {
      req('/blocks/summary', { blockIds: [].concat(blockIds) }, callback, !Array.isArray(blockIds))
    }
  }

  this.transactions = {
    get: function (txIds, callback) {
      req('/transactions/get', { txIds: [].concat(txIds) }, callback, !Array.isArray(txIds))
    },

    latest: function (callback) {
      req('/transactions/latest', {}, callback)
    },

    propagate: function (txHex, callback) {
      req('/transactions/propagate', { txHexs: [].concat(txHex) }, callback)
    },

    summary: function (txIds, callback) {
      req('/transactions/summary', { txIds: [].concat(txIds) }, callback, !Array.isArray(txIds))
    }
  }

  this.xhrOptions = {}
}

module.exports = Node
{'fraction_non_alphanumeric': 0.15190283400809718, 'fraction_numerical': 0.0016194331983805667, 'mean_word_length': 3.0025923525599483, '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': '2662539', 'n_tokens_mistral': 1919, 'n_tokens_neox': 1884, 'n_words': 432}