<commit_before>"use strict";

var stateIndexes = [undefined, 0, 1];
var resolved = 1;
var rejected = 2;

/* istanbul ignore next */
var nodeProcess = typeof process !== 'undefined' ? process : {};

/* istanbul ignore next */
var nextTick = nodeProcess.nextTick || setImmediate || setTimeout;

function welsh(onResolved, onRejected) {
  var state, head, tail, stateIndex, pendingResult;

  if ( onResolved || onRejected ) {
    appendThen(onResolved, onRejected);
  }

  var welshInterface = {
    then: appendThen,
    catch: function (onRejected) { return appendThen(undefined, onRejected); },
    resolve: function (result) { return setState(resolved, result); },
    reject: function (error) { return setState(rejected, error); }
  };

  return welshInterface;

  function setState(newState, result) {
    if ( state ) {
      throw new Error("You can't welsh on this promise!");
    }
    state = newState;
    stateIndex = stateIndexes[state];
    queueResult(result);
    return welshInterface;
  }

  function queueResult(result) {
    if ( head ) {
      proceed(result);
    }
    else {
      pendingResult = result;
    }
  }

  function appendThen(onResolved, onRejected) {
    if ( typeof onResolved !== 'function' ) {
      onResolved = undefined;
    }
    if ( typeof onRejected !== 'function' ) {
      onRejected = undefined;
    }

    var item = { value: [onResolved, onRejected] };
  	if ( !tail ) {
  		head = tail = item;
  	}
    else {
      tail.next = item;
      tail = item;
    }

    if ( state ) {
      proceed(pendingResult);
      pendingResult = undefined;
    }
    return welshInterface;
  }

  function proceed(result) {
    if ( isPromise(result) ) {
      result.then(thenLinker, thenLinker);
      return;
    }

    do {
      var callback = head.value[stateIndex];
      head = head.next || (tail = null);
      if ( callback ) {
        result = callback(result);
        if ( isPromise(result) ) {
          result.then(thenLinker, thenLinker);
          return;
        }
      }
    }
    while ( head );
  }

  function thenLinker(result) {
    nextTick(function () {
      queueResult(result);
    });
    return result;
  }

  function isPromise(value) {
    return typeof value === 'object' && value !== null &&
           typeof value.then === 'function';
  }
}

module.exports = welsh;
<commit_msg>header<commit_after>/*
 * Welsh (Promises, but not really)
 * Licensed under the MIT License
 * see LICENSE.md
 *
 * @author <NAME> (kode4food.it) 
 */
 
"use strict";

var stateIndexes = [undefined, 0, 1];
var resolved = 1;
var rejected = 2;

/* istanbul ignore next */
var nodeProcess = typeof process !== 'undefined' ? process : {};

/* istanbul ignore next */
var nextTick = nodeProcess.nextTick || setImmediate || setTimeout;

function welsh(onResolved, onRejected) {
  var state, head, tail, stateIndex, pendingResult;

  if ( onResolved || onRejected ) {
    appendThen(onResolved, onRejected);
  }

  var welshInterface = {
    then: appendThen,
    catch: function (onRejected) { return appendThen(undefined, onRejected); },
    resolve: function (result) { return setState(resolved, result); },
    reject: function (error) { return setState(rejected, error); }
  };

  return welshInterface;

  function setState(newState, result) {
    if ( state ) {
      throw new Error("You can't welsh on this promise!");
    }
    state = newState;
    stateIndex = stateIndexes[state];
    queueResult(result);
    return welshInterface;
  }

  function queueResult(result) {
    if ( head ) {
      proceed(result);
    }
    else {
      pendingResult = result;
    }
  }

  function appendThen(onResolved, onRejected) {
    if ( typeof onResolved !== 'function' ) {
      onResolved = undefined;
    }
    if ( typeof onRejected !== 'function' ) {
      onRejected = undefined;
    }

    var item = { value: [onResolved, onRejected] };
  	if ( !tail ) {
  		head = tail = item;
  	}
    else {
      tail = tail.next = item;
    }

    if ( state ) {
      proceed(pendingResult);
      pendingResult = undefined;
    }
    return welshInterface;
  }

  function proceed(result) {
    if ( isPromise(result) ) {
      result.then(thenLinker, thenLinker);
      return;
    }

    do {
      var callback = head.value[stateIndex];
      head = head.next || (tail = null);
      if ( callback ) {
        result = callback(result);
        if ( isPromise(result) ) {
          result.then(thenLinker, thenLinker);
          return;
        }
      }
    }
    while ( head );
  }

  function thenLinker(result) {
    nextTick(function () {
      queueResult(result);
    });
    return result;
  }

  function isPromise(value) {
    return typeof value === 'object' && value !== null &&
           typeof value.then === 'function';
  }
}

module.exports = welsh;

{'fraction_non_alphanumeric': 0.1257522307532683, 'fraction_numerical': 0.001867607387424777, 'mean_word_length': 2.594332587621178, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '5943729', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1489, 'n_words': 371}