<commit_before>import R from 'ramda'

/**
 * Returns an array of rule violations in the passed-in string.
 *
 * Ignores and rules are separated at the beginning of the call, then the ranges
 * to ignore are computed. When the a check results intersect with or is
 * contained in a range it will be simply ignored.
 *
 * If there's no results, the function returns `undefined`.
 *
 * @param  {Array} [ruleset=[]] the array with all the rules and ignores to use
 *                              when checking the passed-in string
 * @param  {string} [string] the string to check
 * @return {Array|undefined} an array of rule violation results or `undefined`
 *                           when there is no violations.<br>Each result
 *                           object will have the following properties:
 * @property {string} name the name of the broken rule
 * @property {Array} range the range at which the violation can be found
 *                         in the string
 * @example
 * import {check} from 'typography-fixer'
 * import rules from 'typography-fixer/lib/rules/en-UK'
 *
 * const results = check(rules, 'Some string "to check".')
 *
 * // The check function support currying
 * const checkString = check(rules)
 *
 * const results = checkSring('Some string "to check".')
 */
export function check (ruleset = [], string) {
  const {ignores, rules} = splitRules(ruleset)

  if (rules.length === 0) { return string ? undefined : function () {} }

  const getRanges = R.compose(R.unnest, R.ap(R.map(rangesFor, ignores)), R.of)

  const doCheck = (string) => {
    const anyIntersection = R.anyPass(R.map(rangesIntersects, getRanges(string)))
    const noIntersection = R.compose(R.not, R.propSatisfies(anyIntersection, 'range'))
    const getResults = R.compose(R.flatten, R.map(checkString(string)))
    const results = R.filter(noIntersection, getResults(rules))

    return results.length > 0 ? results : undefined
  }

  return string ? doCheck(string) : doCheck
}

/**
 * Returns the passed-in string modified by the specified ruleset.
 *
 * Ignores and rules are separated at the beginning of the call, then the ranges
 * to ignore are computed. The string is split using the ranges to have in one
 * array all the parts that can be modified and in another array all the ignored
 * parts. Once all the fixes were applied, the string from the two arrays are
 * joined together into a new string and returned.<commit_msg>:memo: Add link to rule and ignore documentation<commit_after>import R from 'ramda'

/**
 * Returns an array of rule violations in the passed-in string.
 *
 * Ignores and rules are separated at the beginning of the call, then the ranges
 * to ignore are computed. When the a check results intersect with or is
 * contained in a range it will be simply ignored.
 *
 * If there's no results, the function returns `undefined`.
 *
 * See the {@link rule} and {@link ignore} documentation for details about how
 * rules and ignore rules are created.
 *
 * @param  {Array} [ruleset=[]] the array with all the rules and ignores to use
 *                              when checking the passed-in string
 * @param  {string} [string] the string to check
 * @return {Array|undefined} an array of rule violation results or `undefined`
 *                           when there is no violations.<br>Each result
 *                           object will have the following properties:
 * @property {string} name the name of the broken rule
 * @property {Array} range the range at which the violation can be found
 *                         in the string
 * @example
 * import {check} from 'typography-fixer'
 * import rules from 'typography-fixer/lib/rules/en-UK'
 *
 * const results = check(rules, 'Some string "to check".')
 *
 * // The check function support currying
 * const checkString = check(rules)
 *
 * const results = checkSring('Some string "to check".')
 */
export function check (ruleset = [], string) {
  const {ignores, rules} = splitRules(ruleset)

  if (rules.length === 0) { return string ? undefined : function () {} }

  const getRanges = R.compose(R.unnest, R.ap(R.map(rangesFor, ignores)), R.of)

  const doCheck = (string) => {
    const anyIntersection = R.anyPass(R.map(rangesIntersects, getRanges(string)))
    const noIntersection = R.compose(R.not, R.propSatisfies(anyIntersection, 'range'))
    const getResults = R.compose(R.flatten, R.map(checkString(string)))
    const results = R.filter(noIntersection, getResults(rules))

    return results.length > 0 ? results : undefined
  }

  return string ? doCheck(string) : doCheck
}

/**
 * Returns the passed-in string modified by the specified ruleset.
 *
 * Ignores and rules are separated at the beginning of the call, then the ranges
 * to ignore are computed. The string is split using the ranges to have in one
 * array all the parts that can be modified and in another array all the ignored
 * parts. Once all the fixes were applied, the string from the two arrays are
 * joined together into a new string and returned.
 *
 * See the {@link rule} and {@link ignore} documentation for details about how
 * rules and ignore rules are created.
{'fraction_non_alphanumeric': 0.0925889904687804, 'fraction_numerical': 0.0007780587434351293, 'mean_word_length': 3.270764119601329, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6476558', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1378, 'n_words': 612}