<commit_before>'use strict'
var PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-find'))
var assign = require('lodash/assign')
var defaults = require('lodash/defaults')
var unique = require('lodash/uniq')
var toArray = require('lodash/toArray')
var url = require('url')
var path = require('path')
var designDocRegex = new RegExp('^_design/')
var couchUrlify = function (str) {
  return str.replace(/[^/a-z0-9_$()+-]/gi, '')
}

/**
 * @constructor Pouchy
 * @param {object} opts {
 *     name: {string} kname of db. calculated from derived url string if `conn` or `url` provided. otherwise, required
 *     conn: {object=} creates `url` using the awesome and simple [url.format](https://www.npmjs.com/package/url#url-format-urlobj)
 *     couchdbSafe {boolean=} [default: true] asserts that `name` provided or `url` provided will work with couchdb.  tests by asserting str conforms to [couch specs](https://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing), minus the `/`.  This _may complain that some valid urls are invalid_.  Please be aware and disable if necessary.
 *     url: {string=} url to remote CouchDB
 *     path: {string=} path to store pouch on filesystem, if using on filesystem!  defaults to _PouchDB_ default of cwd
 *     pouchConfig: {object=} PouchDB constructor [options](http://pouchdb.com/api.html#create_database)
 *     replicate: {string=} [default: undefined] 'out/in/sync/both', where sync and both are ===
 *     replicateLive: {boolean=} [default: true] activates only if `replicate` is set
 * }
 */
function Pouchy (opts) {
  var replicate = opts.replicate
  var couchdbSafe = opts.couchdbSafe === undefined ? true : opts.couchdbSafe
  var pathParts
  var _url // temp url used during validation cycle
  var live  // eslint-disable-line

  if (!opts) {
    throw new ReferenceError('db options required')
  }
  if (!opts.name && !opts.url && !opts.conn) {
    throw new ReferenceError('db name, url, or conn required to create or access pouchdb')
  }
  if (!this) { throw new ReferenceError('no `this` context.  did you forget `new`?') }

  if (opts.url) {
    _url = opts.url
  } else if (opts.conn) {
    _url = url.format(opts.conn)
  }

  if (_url) {
    pathParts = url.parse(_url).pathname.split('/')
    // check opts.name, as name may have / in it
    this.name = opts.name || pathParts[pathParts.length - 1]
    if (couchdbSafe && this.name !== couchUrlify(this.name.toLowerCase())) {
      throw new Error([
        'provided `url` or `conn` "',
        ((opts.conn && JSON.stringify(opts.conn)) || _url),
        '" may not be couchdb safe'
      ].join(' '))
    }
  } else {
    this.name = couchUrlify(opts.name).toLowerCase()
    if (couchdbSafe && this.name !== opts.name) {
      throw new Error([
        'provided name', '"' + opts.name + '"',
        'may not be couchdb safe. couchdb safe url:',
        this.name
      ].join(' '))
    }
  }

  if (_url) {
    this.url = _url
  }
  this.path = path.resolve(opts.path || '', this.name)

  this.db = new PouchDB(
    opts.name ? this.path : this.url,
    opts.pouchConfig
  )
  if (replicate) {
    if (!this.url) {
      throw new ReferenceError('url or conn object required to replicate')
    }
    replicate = replicate === 'both' ? 'sync' : replicate
    live = (opts.replicateLive === undefined) ? true : opts.replicateLive
    switch (replicate) {
      case 'out':
        PouchDB.replicate(this.path, this.url, { live: true })
        break
      case 'in':
        PouchDB.replicate(this.url, this.path, { live: true })
        break
      case 'sync':
        PouchDB.replicate(this.path, this.url, { live: true })
        PouchDB.replicate(this.url, this.path, { live: true })
        break
      default:
        throw new Error('in/out replication direction ' +
          'must be specified')
    }
  }
}

assign(Pouchy.prototype, {
  all: function (opts, cb) {
    if (typeof opts === 'function') {
      cb = opts
      opts = {}
    }
    opts = defaults(opts || {}, {
      include_docs: true // jshint ignore:line
    })
    var p = this.db.allDocs(opts).then(function getDocs (docs) {
      return docs.rows.reduce(function (r, v) {
        if (opts.includeDesignDocs || !v.doc._id.match(designDocRegex)) {
          r.push(v.doc)<commit_msg>fix replication issue which would not honor requested backend<commit_after>'use strict'
var PouchDB = require('pouchdb')
PouchDB.plugin(require('pouchdb-find'))
var assign = require('lodash/assign')
var defaults = require('lodash/defaults')
var unique = require('lodash/uniq')
var toArray = require('lodash/toArray')
var url = require('url')
var path = require('path')
var designDocRegex = new RegExp('^_design/')
var couchUrlify = function (str) {
  return str.replace(/[^/a-z0-9_$()+-]/gi, '')
}

/**
 * @constructor Pouchy
 * @param {object} opts {
 *     name: {string} kname of db. calculated from derived url string if `conn` or `url` provided. otherwise, required
 *     conn: {object=} creates `url` using the awesome and simple [url.format](https://www.npmjs.com/package/url#url-format-urlobj)
 *     couchdbSafe {boolean=} [default: true] asserts that `name` provided or `url` provided will work with couchdb.  tests by asserting str conforms to [couch specs](https://wiki.apache.org/couchdb/HTTP_database_API#Naming_and_Addressing), minus the `/`.  This _may complain that some valid urls are invalid_.  Please be aware and disable if necessary.
 *     url: {string=} url to remote CouchDB
 *     path: {string=} path to store pouch on filesystem, if using on filesystem!  defaults to _PouchDB_ default of cwd
 *     pouchConfig: {object=} PouchDB constructor [options](http://pouchdb.com/api.html#create_database)
 *     replicate: {string=} [default: undefined] 'out/in/sync/both', where sync and both are ===
 *     replicateLive: {boolean=} [default: true] activates only if `replicate` is set
 * }
 */
function Pouchy (opts) {
  var replicate = opts.replicate
  var couchdbSafe = opts.couchdbSafe === undefined ? true : opts.couchdbSafe
  var pathParts
  var _url // temp url used during validation cycle
  var live  // eslint-disable-line

  if (!opts) {
    throw new ReferenceError('db options required')
  }
  if (!opts.name && !opts.url && !opts.conn) {
    throw new ReferenceError('db name, url, or conn required to create or access pouchdb')
  }
  if (!this) { throw new ReferenceError('no `this` context.  did you forget `new`?') }

  if (opts.url) {
    _url = opts.url
  } else if (opts.conn) {
    _url = url.format(opts.conn)
  }

  if (_url) {
    pathParts = url.parse(_url).pathname.split('/')
    // check opts.name, as name may have / in it
    this.name = opts.name || pathParts[pathParts.length - 1]
    if (couchdbSafe && this.name !== couchUrlify(this.name.toLowerCase())) {
      throw new Error([
        'provided `url` or `conn` "',
        ((opts.conn && JSON.stringify(opts.conn)) || _url),
        '" may not be couchdb safe'
      ].join(' '))
    }
  } else {
    this.name = couchUrlify(opts.name).toLowerCase()
    if (couchdbSafe && this.name !== opts.name) {
      throw new Error([
        'provided name', '"' + opts.name + '"',
        'may not be couchdb safe. couchdb safe url:',
        this.name
      ].join(' '))
    }
  }

  if (_url) {
    this.url = _url
  }
  this.path = path.resolve(opts.path || '', this.name)

  this.db = new PouchDB(
    opts.name ? this.path : this.url,
    opts.pouchConfig
  )
  if (replicate) {
    if (!this.url) {
      throw new ReferenceError('url or conn object required to replicate')
    }
    replicate = replicate === 'both' ? 'sync' : replicate
    live = (opts.replicateLive === undefined) ? true : opts.replicateLive
    switch (replicate) {
      case 'out':
        this.db.replicate.to(this.url, { live: true })
        break
      case 'in':
        this.db.replicate.from(this.url, { live: true })
        break
      case 'sync':
        this.db.replicate.to(this.url, { live: true })
        this.db.replicate.from(this.url, { live: true })
        break
      default:
        throw new Error('in/out replication direction ' +
          'must be specified')
    }
  }
}

assign(Pouchy.prototype, {
  all: function (opts, cb) {
    if (typeof opts === 'function') {
      cb = opts
      opts = {}
    }
    opts = defaults(opts || {}, {
      include_docs: true // jshint ignore:line
    })
    var p = this.db.allDocs(opts).then(function getDocs (docs) {
      return docs.rows.reduce(function (r, v) {
        if (opts.includeDesignDocs || !v.doc._id.match(designDocRegex)) {
          r.push(v.doc)
{'fraction_non_alphanumeric': 0.1439200186003255, 'fraction_numerical': 0.0006975122064636131, 'mean_word_length': 2.9590427979751497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '4459118', 'n_tokens_mistral': 2812, 'n_tokens_neox': 2759, 'n_words': 779}