<commit_before>
var ResultType = require('result-type')
var ResultCore = require('result-core')
var listen = ResultCore.addListener
var inherit = require('inherit')

/**
 * expose `Result`
 */

module.exports = exports = Result

/**
 * expose helpers
 */

exports.wrap = exports.done = wrap
exports.transfer = transfer
exports.coerce = coerce
exports.failed = failed
exports.unbox = unbox
exports.when = when
exports.read = read

/**
 * the Result class
 */

function Result(){}

/**
 * inherit from ResultCore
 */

inherit(Result, ResultCore)

/**
 * Create a Result for a transformation of the value
 * of `this` Result
 *
 * @param  {Function} onValue
 * @param  {Function} onError
 * @return {Result}
 */

Result.prototype.then = function(onValue, onError) {
  switch (this.state) {
    case 'done':
      return typeof onValue == 'function'
        ? run(onValue, this.value, this)
        : wrap(this.value)
    case 'fail':
      return typeof onError == 'function'
        ? run(onError, this.value, this)
        : failed(this.value)
    default:
      var x = new Result
      this.listen(
        handle(x, onValue, 'write', this),
        handle(x, onError, 'error', this))
      return x
  }
}

/**
 * use the same `fn` for both `onValue` and `onError`
 *
 * @param  {Function} fn
 * @return {Result}
 */

Result.prototype.always = function(fn){
  return this.then(fn, fn)
}

/**
 * read using a node style function
 *
 *   result.node(function(err, value){})
 *
 * @param  {Function} callback(error, value)
 * @return {this}
 */

Result.prototype.node = function(fn){
  return this.read(function(v){ fn(null, v) }, fn)
}

/**
 * Create a child Result destined to fulfill with `value`
 *
 *   return result.then(function(value){
 *     // some side effect
 *   }).yeild(e)
 *
 * @param  {x} value
 * @return {Result}
 */

Result.prototype.yeild = function(value){
  return this.then(function(){ return value })
}

/**
 * return a Result for `this[attr]`
 *
 * @param {String} attr
 * @return {Result}
 */

Result.prototype.get = function(attr){
  return this.then(function(obj){
    return obj[attr]
  })
}

/**
 * run `value` through `handler` and ensure the result
 * is wrapped in a trusted Result
 *
 * @param {Function} handler
 * @param {x} value
 * @param {Any} ctx
 * @api private
 */

function run(handler, value, ctx){
  try { return coerce(handler.call(ctx, value)) }
  catch (e) { return failed(e) }
}

/**
 * wrap `reason` in a "failed" result
 *
 * @param {x} reason
 * @return {Result}
 * @api public
 */

function failed(reason){
  var res = new Result
  res.value = reason
  res.state = 'fail'
  return res
}

/**
 * wrap `value` in a "done" Result
 *
 * @param {x} value
 * @return {Result}
 * @api public
 */

function wrap(value){
  var res = new Result
  res.value = value
  res.state = 'done'
  return res
}

/**
 * coerce `value` to a Result
 *
 * @param {x} value
 * @return {Result}
 * @api public
 */

function coerce(value){
  if (value instanceof ResultType) {
    if (value instanceof Result) return value
    var result = new Result
    value.read(
      function(v){ result.write(v) },
      function(e){ result.error(e) })
    return result
  }
  return wrap(value)
}

/**
 * create a function which will propagate a value/error
 * onto `result` when called. If `fn` is present it<commit_msg>style<commit_after>
var ResultType = require('result-type')
var ResultCore = require('result-core')
var listen = ResultCore.addListener
var inherit = require('inherit')

/**
 * expose `Result`
 */

module.exports = exports = Result

/**
 * expose helpers
 */

exports.wrap = exports.done = wrap
exports.transfer = transfer
exports.coerce = coerce
exports.failed = failed
exports.unbox = unbox
exports.when = when
exports.read = read

/**
 * the Result class
 */

function Result(){}

/**
 * inherit from ResultCore
 */

inherit(Result, ResultCore)

/**
 * Create a Result for a transformation of the value
 * of `this` Result
 *
 * @param  {Function} onValue
 * @param  {Function} onError
 * @return {Result}
 */

Result.prototype.then = function(onValue, onError) {
  switch (this.state) {
    case 'done':
      return typeof onValue == 'function'
        ? run(onValue, this.value, this)
        : wrap(this.value)
    case 'fail':
      return typeof onError == 'function'
        ? run(onError, this.value, this)
        : failed(this.value)
    default:
      var x = new Result
      this.listen(
        handle(x, onValue, 'write', this),
        handle(x, onError, 'error', this))
      return x
  }
}

/**
 * use the same `fn` for both `onValue` and `onError`
 *
 * @param  {Function} fn
 * @return {Result}
 */

Result.prototype.always = function(fn){
  return this.then(fn, fn)
}

/**
 * read using a node style function
 *
 *   result.node(function(err, value){})
 *
 * @param  {Function} callback(error, value)
 * @return {this}
 */

Result.prototype.node = function(fn){
  return this.read(function(v){ fn(null, v) }, fn)
}

/**
 * Create a child Result destined to fulfill with `value`
 *
 *   return result.then(function(value){
 *     // some side effect
 *   }).yeild(e)
 *
 * @param  {x} value
 * @return {Result}
 */

Result.prototype.yeild = function(value){
  return this.then(function(){ return value })
}

/**
 * return a Result for `this[attr]`
 *
 * @param {String} attr
 * @return {Result}
 */

Result.prototype.get = function(attr){
  return this.then(function(obj){
    return obj[attr]
  })
}

/**
 * run `value` through `handler` and ensure the result
 * is wrapped in a trusted Result
 *
 * @param {Function} handler
 * @param {x} value
 * @param {Any} ctx
 * @api private
 */

function run(handler, value, ctx){
  try { return coerce(handler.call(ctx, value)) }
  catch (e) { return failed(e) }
}

/**
 * wrap `reason` in a "failed" result
 *
 * @param {x} reason
 * @return {Result}
 * @api public
 */

function failed(reason){
  var res = new Result
  res.value = reason
  res.state = 'fail'
  return res
}

/**
 * wrap `value` in a "done" Result
 *
 * @param {x} value
 * @return {Result}
 * @api public
 */

function wrap(value){
  var res = new Result
  res.value = value
  res.state = 'done'
  return res
}

/**
 * coerce `value` to a Result
 *
 * @param {x} value
 * @return {Result}
 * @api public
 */

function coerce(value){
  if (!(value instanceof ResultType)) return wrap(value)
  if (value instanceof Result) return value
  var result = new Result
  value.read(
    function(v){ result.write(v) },
    function(e){ result.error(e) })
  return result
}

/**
 * create a function which will propagate a value/error
 * onto `result` when called. If `fn` is present it
{'fraction_non_alphanumeric': 0.14874417205594825, 'fraction_numerical': 0.0, 'mean_word_length': 3.1227526348419095, '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': '150729', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2176, 'n_words': 657}