<commit_before>'use strict';

const rxFetch = require('rx-fetch');
const url = require('url');

//------------------------------------------------------------------------------

var server = module.exports = function (baseUrl) {

  const parsedUrl = url.parse(baseUrl ? baseUrl : 'http://localhost:5984');
  const pathName = parsedUrl.pathname;
  if (pathName && pathName !== '/') {
    throw new Error("CouchDB server must not contain a path or query string");
  }

  this._baseUrl = url.format(parsedUrl);

};

//------------------------------------------------------------------------------
/**
 * Return an Observable which will fire once with a list of all databases
 * available on the server.
 */

server.prototype.allDatabases = function () {

  return rxFetch(url.resolve(this._baseUrl, '_all_dbs')).json();

};

//------------------------------------------------------------------------------
/**
 * Create a new database on the server. Return an Observable which sends only
 * an onCompleted event when the database has been created.
 */

server.prototype.createDatabase = function (dbName) {

  if (typeof (dbName) != 'string' || dbName.length === 0) {
    throw new Error("rxCouch.createDatabase: dbName must be non-empty string");
  }

  return rxFetch(url.resolve(this._baseUrl, dbName), { method: 'put' })
    .map((response) => {
      if ([201, 412].indexOf(response.status) === -1) {
        throw new Error("Unexpected response from server " +
                        response.status + " " + response.statusText);
      }
      return response;
    })
    .filter(() => false);

};

//------------------------------------------------------------------------------
/**
 * Create a new database on the server. Return an Observable which sends only
 * an onCompleted event when the database has been created.
 */

server.prototype.deleteDatabase = function (dbName) {

  if (typeof (dbName) != 'string' || dbName.length === 0) {
    throw new Error("rxCouch.deleteDatabase: dbName must be non-empty string");
  }

  return rxFetch(url.resolve(this._baseUrl, dbName), { method: 'delete' })
    .map((response) => {
      if ([200, 404].indexOf(response.status) === -1) {
        throw new Error("Unexpected response from server " +
                        response.status + " " + response.statusText);
      }
      return response;
    })
    .filter(() => false);

};<commit_msg>Simplify error handling using rx-fetch's new .failIfStatusNotIn method.<commit_after>'use strict';

const rxFetch = require('rx-fetch');
const url = require('url');

//------------------------------------------------------------------------------

var server = module.exports = function (baseUrl) {

  const parsedUrl = url.parse(baseUrl ? baseUrl : 'http://localhost:5984');
  const pathName = parsedUrl.pathname;
  if (pathName && pathName !== '/') {
    throw new Error("CouchDB server must not contain a path or query string");
  }

  this._baseUrl = url.format(parsedUrl);

};

//------------------------------------------------------------------------------
/**
 * Return an Observable which will fire once with a list of all databases
 * available on the server.
 */

server.prototype.allDatabases = function () {

  return rxFetch(url.resolve(this._baseUrl, '_all_dbs')).json();

};

//------------------------------------------------------------------------------
/**
 * Create a new database on the server. Return an Observable which sends only
 * an onCompleted event when the database has been created.
 */

server.prototype.createDatabase = function (dbName) {

  if (typeof (dbName) != 'string' || dbName.length === 0) {
    throw new Error("rxCouch.createDatabase: dbName must be non-empty string");
  }

  return rxFetch(url.resolve(this._baseUrl, dbName), { method: 'put' })
    .failIfStatusNotIn([201, 412])
    .filter(() => false);

};

//------------------------------------------------------------------------------
/**
 * Create a new database on the server. Return an Observable which sends only
 * an onCompleted event when the database has been created.
 */

server.prototype.deleteDatabase = function (dbName) {

  if (typeof (dbName) != 'string' || dbName.length === 0) {
    throw new Error("rxCouch.deleteDatabase: dbName must be non-empty string");
  }

  return rxFetch(url.resolve(this._baseUrl, dbName), { method: 'delete' })
    .failIfStatusNotIn([200, 404])
    .filter(() => false);

};
{'fraction_non_alphanumeric': 0.2641338760741746, 'fraction_numerical': 0.008593396653098146, 'mean_word_length': 1.9624916275954454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3338762', 'n_tokens_mistral': 1185, 'n_tokens_neox': 1137, 'n_words': 345}