<commit_before>

create_liar = (lies) ->  
  injectLies {}, lies

injectLies = (liar, lies) -> 
  for lie in lies
    liar[lie.function_name] = 
      generateHandler lie.function_name, lies
  liar

generateHandler = (function_name, lies) ->
  () ->

    matching_lies = filter lies, function_name, arguments
    lie = matching_lies[0]
    if not lie
      throw new Error(
        "funkyFunction called with unexpected arguments. " +
        "Actual: " + arguments_to_array(arguments).join(', '))
          
    run_callback lie, arguments

    r = lie.returns ? null
    if r and r.value
      if r.on_value
        injectLies r.value, r.on_value
      r.value


filter = (lies, function_name, arguments_obj) ->
  matching_lies = []
  for lie in lies
    if function_name is lie.function_name
      arguments_arr = arguments_to_array arguments_obj
      arguments_arr = remove_functions arguments_arr 
      if arrays_equal arguments_arr, lie.arguments ? []
        matching_lies.push lie
  matching_lies



run_callback = (lie, arguments_obj) ->
  callback_arguments = callback_arguments_array lie
  if callback_arguments
    callback = find_function arguments_obj
    if callback
      callback.apply this, callback_arguments

find_function = (arguments_obj) ->
  for arg in arguments_obj
    return arg if is_function(arg)
  null

callback_arguments_array = (lie) ->
  args = []
  highest_index = 0
  # TODO perhaps use the funky coffescript Comprehensions here
  # to create a matches array
  for property of lie
    match = /callback_argument_(\d+)/.exec property
    if match?
      index = parseInt(match[1])-1
      args[index] = lie[property].value
      highest_index = index if index > highest_index

  # fill em up
  for i in [0..highest_index]
    args[i] = null if not args[i]?

  return  if args.length > 0 then args else null

arrays_equal = (a, b) ->
  for item, i in a
    return false if item isnt b[i]
  true

remove_functions = (arr) ->
  # Returns a copy of the array, with functions removed.
  item for item in arr when not is_function(item)

is_function = (obj) ->
  return false if not obj?
  (({}).toString).call(obj) is '[object Function]'

arguments_to_array = (arguments_obj) ->
  # Convert that pesky function arguments object
  # to a normal array.
  arg for arg in arguments_obj



module.exports = create_liar<commit_msg>slight tweaks<commit_after>

create_liar = (lies) ->  
  injectLies {}, lies

injectLies = (liar, lies) -> 
  for lie in lies
    liar[lie.function_name] = 
      generateHandler lie.function_name, lies
  liar

generateHandler = (function_name, lies) ->
  () ->

    matching_lies = filter lies, function_name, arguments
    lie = matching_lies[0]
    if not lie
      throw new Error(
        "funkyFunction called with unexpected arguments. " +
        "Actual: " + arguments_to_array(arguments).join(', '))
          
    run_callback lie, arguments

    r = lie.returns ? null
    if r and r.value
      if r.on_value
        injectLies r.value, r.on_value
      r.value


filter = (lies, function_name, arguments_obj) ->
  matching_lies = []
  for lie in lies
    if function_name is lie.function_name
      arguments_arr = arguments_to_array arguments_obj
      arguments_arr = remove_functions arguments_arr 
      if arrays_equal arguments_arr, lie.arguments ? []
        matching_lies.push lie
  matching_lies



run_callback = (lie, arguments_obj) ->
  callback_arguments = callback_arguments_array lie
  if callback_arguments
    callback = find_function arguments_obj
    if callback
      callback.apply this, callback_arguments

find_function = (arguments_obj) ->
  for arg in arguments_obj
    return arg if is_function(arg)
  null

callback_arguments_array = (lie) ->
  args = []
  highest_index = 0
  # TODO perhaps use the funky coffescript Comprehensions here
  # to create a matches array

  for property_name of lie
    match = /callback_argument_(\d+)/.exec property_name
    if match?
      index = parseInt(match[1])-1
      args[index] = lie[property_name].value
      highest_index = index if index > highest_index

  # fill em up
  for i in [0..highest_index]
    args[i] = null if not args[i]?

  if args.length > 0 then args else null

arrays_equal = (a, b) ->
  for item, i in a
    return false if item isnt b[i]
  true

remove_functions = (arr) ->
  # Returns a copy of the array, with functions removed.
  item for item in arr when not is_function(item)

is_function = (obj) ->
  return false if not obj?
  (({}).toString).call(obj) is '[object Function]'

arguments_to_array = (arguments_obj) ->
  # Convert that pesky function arguments object
  # to a normal array.
  arg for arg in arguments_obj



module.exports = create_liar
{'fraction_non_alphanumeric': 0.08432203389830509, 'fraction_numerical': 0.002542372881355932, 'mean_word_length': 3.066322136089578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, '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': '4467456', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1441, 'n_words': 457}