<commit_before>'use strict';

const type = require('./type');

/**
 * Test a JSON object
 *
 * @param {String | Number | Boolean | Object | Array} value The value that will be tested.
 * @param {*} expected A pattern to test against the value provided.
 * @return {Boolean} The boolean result of the test.
 * @api public
 */
function match(value, expected) {
  if (value === null) return expected === null;
  if (expected === null) return value === null;
  switch (type(expected)) {
    case 'string':
    case 'number':
    case 'boolean':
    case 'undefined':
      return value === expected;
    case 'regexp':
      return expected.test(value);
    case 'function':
      return evaluateFunction(value, expected); // eslint-disable-line no-use-before-define
    case 'object':
      return compareObjects(value, expected); // eslint-disable-line no-use-before-define
    case 'array':
      return compareArrays(value, expected); // eslint-disable-line no-use-before-define
    default:
      return false;
  }
}

/**
 * Make a function which test a JSON object
 *
 * @param {*} expected A pattern to test against the value provided.
 * @returns {Function} A function with the expected patter predefined.
 * @api public
 */
match.bake = expected => value => match(value, expected);

/**
 * Compares two arrays
 *
 * @param {Array} array Array to be tested.
 * @param {Object} expected Pattern to test the array.
 * @return {Boolean} Result of the test.
 * @api private
 */
function compareArrays(array, expected) {
  if (array.length !== expected.length) return false;
  return array.reduce((previous, current, index) => {
    return match(array[index], expected[index]) && previous;
  }, true);
}

/**
 * Compares two objects
 *
 * @param {Objet} object Object to be tested.
 * @param {Object} expected Pattern to test the object.
 * @return {Boolean} Result of the test.
 * @api private
 */
function compareObjects(object, expected) {
  if (!compareArrays(Object.keys(object), Object.keys(expected))) return false;
  return Object.keys(expected).reduce((previous, current) => {
    return match(object[current], expected[current]) && previous;
  }, true);
}

/**
 * Evaluate a function and throw an error if the result is not boolean
 *
 * @param {*} value A value that will be sended as a parameter to the function.
 * @param {Function} func A function to call.
 * @return {Boolean} The result of the function.
 * @api private
 */
function evaluateFunction(value, func) {
  let result = func.call(null, value);
  if (typeof result !== 'boolean') {
    throw new Error('Function provided should return a boolean');
  }
  return result;
}

module.exports = match;
<commit_msg>Use custom type function for consistency<commit_after>'use strict';

const type = require('./type');

/**
 * Test a JSON object
 *
 * @param {String | Number | Boolean | Object | Array} value The value that will be tested.
 * @param {*} expected A pattern to test against the value provided.
 * @return {Boolean} The boolean result of the test.
 * @api public
 */
function match(value, expected) {
  if (value === null) return expected === null;
  if (expected === null) return value === null;
  switch (type(expected)) {
    case 'string':
    case 'number':
    case 'boolean':
    case 'undefined':
      return value === expected;
    case 'regexp':
      return expected.test(value);
    case 'function':
      return evaluateFunction(value, expected); // eslint-disable-line no-use-before-define
    case 'object':
      return compareObjects(value, expected); // eslint-disable-line no-use-before-define
    case 'array':
      return compareArrays(value, expected); // eslint-disable-line no-use-before-define
    default:
      return false;
  }
}

/**
 * Make a function which test a JSON object
 *
 * @param {*} expected A pattern to test against the value provided.
 * @returns {Function} A function with the expected patter predefined.
 * @api public
 */
match.bake = expected => value => match(value, expected);

/**
 * Compares two arrays
 *
 * @param {Array} array Array to be tested.
 * @param {Object} expected Pattern to test the array.
 * @return {Boolean} Result of the test.
 * @api private
 */
function compareArrays(array, expected) {
  if (array.length !== expected.length) return false;
  return array.reduce((previous, current, index) => {
    return match(array[index], expected[index]) && previous;
  }, true);
}

/**
 * Compares two objects
 *
 * @param {Objet} object Object to be tested.
 * @param {Object} expected Pattern to test the object.
 * @return {Boolean} Result of the test.
 * @api private
 */
function compareObjects(object, expected) {
  if (!compareArrays(Object.keys(object), Object.keys(expected))) return false;
  return Object.keys(expected).reduce((previous, current) => {
    return match(object[current], expected[current]) && previous;
  }, true);
}

/**
 * Evaluate a function and throw an error if the result is not boolean
 *
 * @param {*} value A value that will be sended as a parameter to the function.
 * @param {Function} func A function to call.
 * @return {Boolean} The result of the function.
 * @api private
 */
function evaluateFunction(value, func) {
  let result = func.call(null, value);
  if (type(result) !== 'boolean') {
    throw new Error('Function provided should return a boolean');
  }
  return result;
}

module.exports = match;

{'fraction_non_alphanumeric': 0.13024118738404453, 'fraction_numerical': 0.0, 'mean_word_length': 3.6594641313742438, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6833005', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1505, 'n_words': 570}