2018-12-22 21:05:13 +00:00
|
|
|
import serialize from 'serialize-javascript'
|
|
|
|
|
|
|
|
export function serializeFunction(func) {
|
|
|
|
let open = false
|
|
|
|
return serialize(func)
|
|
|
|
.replace(serializeFunction.assignmentRE, (_, spaces) => {
|
2019-01-06 07:56:59 +00:00
|
|
|
return `${spaces}: function (`
|
2018-12-22 21:05:13 +00:00
|
|
|
})
|
|
|
|
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
|
|
|
|
if (open) {
|
2019-01-06 07:56:59 +00:00
|
|
|
return `${spaces}${name}: function (${args}) {`
|
2018-12-22 21:05:13 +00:00
|
|
|
} else {
|
|
|
|
open = true
|
|
|
|
return _
|
|
|
|
}
|
|
|
|
})
|
2019-02-01 13:31:16 +00:00
|
|
|
.replace(`${func.name || 'function'}(`, 'function (')
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
|
|
|
|
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm
|