<commit_before>module.exports = function (fn, opts) {
  const concurrency = Object.assign({ concurrency: 1 }, opts).concurrency
  if (typeof concurrency !== 'number' || isNaN(concurrency) || concurrency <= 0) {
    throw new TypeError(`Invalid concurrency value: ${concurrency}`)
  }

  let count = 0
  const queue = []

  return async function (...args) {
    try {
      count += 1
      if (count > concurrency) {
        await new Promise((resolve) => queue.push({ resolve }))
      }

      return await fn(...args)
    } finally {
      count -= 1
      if (queue.length > 0) queue.shift().resolve()
    }
  }
}<commit_msg>move concurrency validation to function<commit_after>module.exports = function (fn, { concurrency = 1 } = {}) {
  validateConcurrencyValue(concurrency)

  let count = 0
  const queue = []

  return async function (...args) {
    count += 1

    try {
      if (count > concurrency) {
        await new Promise((resolve) => queue.push({ resolve }))
      }

      return await fn(...args)
    } finally {
      count -= 1
      if (queue.length > 0) queue.shift().resolve()
    }
  }
}

function validateConcurrencyValue (value) {
  const isValid = typeof value === 'number' && value > 0 && (value === Infinity || value % 1 === 0)
  if (!isValid) throw new TypeError(`Invalid concurrency value: ${value}`)
}
{'fraction_non_alphanumeric': 0.149063670411985, 'fraction_numerical': 0.010486891385767791, 'mean_word_length': 2.7633802816901407, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 10, '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': '6995249', 'n_tokens_mistral': 413, 'n_tokens_neox': 394, 'n_words': 116}