function serializeFunction(name, func) {
  if (!func) {
    return '';
  }
  let expression = func.toString();
  if (
    func.name &&
    func.name !== 'function' &&
    expression.match(new RegExp('^' + func.name.replace('$', '\\$') + '\\b'))
  ) {
    // assume function came from an object property using method syntax
    expression = 'function ' + expression;
  }
  return `var ${name} = ${expression};`;
}
This is intended to work with named functions, anonymous functions, arrow functions, and object methods.
Due to how the Evalscript is executed, these are serialized as function expressions using `var`.

@param {string} name The name of the function.
@param {Function|undefined} func The function to serialize.
@return {string} The serialized function.