<commit_before>
const R        = require('ramda');
const Bluebird = require('bluebird');
const uuid     = require('uuid');
const Err      = require('../error.js');
//const QueryRouter = require('../query-router.js')


const throwInvalidBucket = (bucket) => {
  throw new Error(`Invalid Bucket: ${bucket}`);
};


const throwItemNotFound  = (bucket, id) => {
  throw new Error(`Item ID#${id} Not Found in bucket: ${bucket}`);
};


const missingBucket = R.curryN(2, R.compose(R.not, R.flip(R.has)));


const assertBucket = R.curry((store, bucket) => 
    R.when(missingBucket(store), throwInvalidBucket)(bucket)
);


module.exports = function MemoryStore(data_set) {

  const store = R.defaultTo({}, data_set);

  const getBucket = (bucket) => {
    assertBucket(store, bucket);

    return R.prop(bucket, store);
  };


  const getById = R.curry((bucket, id) => {
    assertBucket(store, bucket);

    return Bluebird.resolve(store[bucket][id]);
  });


  const insert = R.curry((bucket, data) => {
    data.id = uuid.v4();

    if (!R.has(bucket, store)) store[bucket] = {};

    store[bucket][data.id] = data;

    return Bluebird.resolve(data.id);
  });


  const update = R.curry((bucket, id, data) =>

      getById(bucket, id)

    .tap(R.when(R.isNil, () => throwItemNotFound(bucket, id)))

    .then(() => { store[bucket][id] = data })

    .return(id)

  );


  const findWhere = R.curry((bucket, predicates) => R.composeP(
    R.unless(R.isNil, R.compose(
      R.find(R.whereEq(predicates))
      , R.values
    ))
    , R.compose(Bluebird.resolve, R.prop(R.__, store))
  )(bucket));


  const upsert = R.curry((bucket, keys, data) =>

      findWhere(bucket, R.pick(keys,  data))

    .then((found) => found ?
      update(bucket, found.id, R.merge(found, data)) :
      insert(bucket, data)
    )

  );


  const getAll = R.compose(Bluebird.resolve, R.values, getBucket);


  /** @TODO Query Routing
   * const query = QueryRouter(config.query_route_file)(store)
   */


  const projectAll = R.curry((bucket, projection) =>
      getAll(bucket).map(R.pick(projection)));


  const findBy = R.curry((bucket, projection, prop, value) =>
      projectAll(bucket, projection).filter(R.propEq(prop, value)));


  const findOneBy = R.curry((bucket, projection, prop, value) =>
      findBy(bucket, projection, prop, value)

    .tap((results) => {
      if (results.length > 1)
        throw new Err.TooManyRecords(
          bucket, prop, value, results.length
        );
    })

    .then(R.head));


  const findById = R.curry((bucket, projection, id) => {
    
    return getById(bucket, id)
      .then(R.pick(projection))

  });


  return {
    insert
    , upsert
    , getById
    , getAll
    , projectAll
    , findBy
    , findOneBy
    , findById
    , findWhere
  };

}

<commit_msg>fix linter errors<commit_after>
const R        = require('ramda')
const Bluebird = require('bluebird')
const uuid     = require('uuid')
const Err      = require('../error.js')
//const QueryRouter = require('../query-router.js')

const ONE = 1
const TWO = 2


const throwInvalidBucket = (bucket) => {
  throw new Error(`Invalid Bucket: ${bucket}`)
}


const throwItemNotFound  = (bucket, id) => {
  throw new Error(`Item ID#${id} Not Found in bucket: ${bucket}`)
}


const missingBucket = R.curryN(TWO, R.compose(R.not, R.flip(R.has)))


const assertBucket = R.curry((store, bucket) =>
    R.when(missingBucket(store), throwInvalidBucket)(bucket)
)


module.exports = function MemoryStore(data_set) {

  const store = R.defaultTo({}, data_set)

  const getBucket = (bucket) => {
    assertBucket(store, bucket)

    return R.prop(bucket, store)
  }


  const getById = R.curry((bucket, id) => {
    assertBucket(store, bucket)

    return Bluebird.resolve(store[bucket][id])
  })


  const insert = R.curry((bucket, data) => {
    data.id = uuid.v4()

    if (!R.has(bucket, store)) store[bucket] = {}

    store[bucket][data.id] = data

    return Bluebird.resolve(data.id)
  })


  const update = R.curry((bucket, id, data) =>

      getById(bucket, id)

    .tap(R.when(R.isNil, () => throwItemNotFound(bucket, id)))

    .then(() => { store[bucket][id] = data })

    .return(id)

  )


  const findWhere = R.curry((bucket, predicates) => R.composeP(
    R.unless(R.isNil, R.compose(
      R.find(R.whereEq(predicates))
      , R.values
    ))
    , R.compose(Bluebird.resolve, R.prop(R.__, store))
  )(bucket))


  const upsert = R.curry((bucket, keys, data) =>

      findWhere(bucket, R.pick(keys,  data))

    .then((found) => found ?
      update(bucket, found.id, R.merge(found, data)) :
      insert(bucket, data)
    )

  )


  const getAll = R.compose(Bluebird.resolve, R.values, getBucket)


  /** @TODO Query Routing
   * const query = QueryRouter(config.query_route_file)(store)
   */


  const projectAll = R.curry((bucket, projection) =>
      getAll(bucket).map(R.pick(projection)))


  const findBy = R.curry((bucket, projection, prop, value) =>
      projectAll(bucket, projection).filter(R.propEq(prop, value)))


  const findOneBy = R.curry((bucket, projection, prop, value) =>
      findBy(bucket, projection, prop, value)

    .tap((results) => {
      if (ONE < results.length)
        throw new Err.TooManyRecords(
          bucket, prop, value, results.length
        )
    })

    .then(R.head))


  const findById = R.curry((bucket, projection, id) =>
      getById(bucket, id).then(R.pick(projection))
  )


  return {
    insert
    , upsert
    , getById
    , getAll
    , projectAll
    , findBy
    , findOneBy
    , findById
    , findWhere
  }

}


{'fraction_non_alphanumeric': 0.1650259531054233, 'fraction_numerical': 0.0010739216037229282, 'mean_word_length': 2.9914285714285715, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 38, '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': '5858259', 'n_tokens_mistral': 1986, 'n_tokens_neox': 1882, 'n_words': 394}