Nuxt/packages/utils/src/serialize.js

23 lines
665 B
JavaScript
Raw Normal View History

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) => {
return `${spaces}:function(`
})
.replace(serializeFunction.internalFunctionRE, (_, spaces, name, args) => {
if (open) {
return `${spaces}${name}:function(${args}) {`
} else {
open = true
return _
}
})
.replace(`${func.name}(`, 'function(')
}
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm