// adopted from: fast-apply <"https://github.com/JayceTDE/fast-apply>
// copyright: Jayce Pulsipher <jaycemp@gmail.com> (MIT)

// altered switch statement to plain object.
// add type check for `function` to prevent errors by using identity

function fastApply (fn, context, args) {
  var something = {
    0: () => {
      return context
        ? fn.call(context)
        : fn()
    },
    1: () => {
      return context
        ? fn.call(context, args[0])
        : fn(args[0])
    },
    2: () => {
      return context
        ? fn.call(context, args[0], args[1])
        : fn(args[0], args[1])
    },
    3: () => {
      return context
        ? fn.call(context, args[0], args[1], args[2])
        : fn(args[0], args[1], args[2])
    },
    4: () => {
      return context
        ? fn.call(context, args[0], args[1], args[2], args[3])
        : fn(args[0], args[1], args[2], args[3])
    },
    5: () => {
      return context
        ? fn.call(context, args[0], args[1], args[2], args[3], args[4])
        : fn(args[0], args[1], args[2], args[3], args[4])
    },
    6: () => {
      return context
        ? fn.call(context, args[0], args[1], args[2], args[3], args[4], args[5])
        : fn(args[0], args[1], args[2], args[3], args[4], args[5])
    },
    defaults: () => {
      return fn.apply(context, args)
    }
  }
  var things = args ? args.length : 0
  // console.log(things, (context ? 'true' : 'false'))

  return (typeof something[things] !== 'function')
    ? something.defaults()
    : something[things]()
}

// concept adopted from: https://github.com/wilmoore/apply-or.js
// reasoning: Function.prototype.apply is normally sufficient; however, there are situations where it is useful to treat a value as a Function (invoke it) only if it is indeed a Function; otherwise, return it as-is. Calling .apply on a value that is not a function would cause an error.
function fastApplyNoThrow (fn, context, args) {
  fn = (typeof fn !== 'function')
    ? identity
    : fn

  return fastApply(fn, context, args)
}

function identity (value) {
  return value
}

module.exports = fastApply
module.exports.Or = fastApplyNoThrow

@@ -282,40 +282,53 @@
 %7B%0A  
-var something = %7B%0A    0: () =%3E %7B
+switch (args ? args.length : 0) %7B%0A    case 0:
 %0A   
@@ -395,25 +395,15 @@
     
-%7D,%0A    1: () =%3E %7B
+case 1:
 %0A   
@@ -486,25 +486,15 @@
     
-%7D,%0A    2: () =%3E %7B
+case 2:
 %0A   
@@ -595,25 +595,15 @@
     
-%7D,%0A    3: () =%3E %7B
+case 3:
 %0A   
@@ -722,25 +722,15 @@
     
-%7D,%0A    4: () =%3E %7B
+case 4:
 %0A   
@@ -867,25 +867,15 @@
     
-%7D,%0A    5: () =%3E %7B
+case 5:
 %0A   
@@ -1030,25 +1030,15 @@
     
-%7D,%0A    6: () =%3E %7B
+case 6:
 %0A   
@@ -1207,15 +1207,8 @@
 5%5D)%0A
-    %7D,%0A
     
@@ -1220,22 +1220,8 @@
 lts:
- () =%3E %7B%0A     
  ret
@@ -1254,213 +1254,9 @@
 )%0A  
-  %7D%0A  %7D%0A  var things = args ? args.length : 0%0A  // console.log(things, (context ? 'true' : 'false'))%0A%0A  return (typeof something%5Bthings%5D !== 'function')%0A    ? something.defaults()%0A    : something%5Bthings%5D()
+%7D
 %0A%7D%0A%0A

update function/apply — revert back to switch