mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
22 lines
683 B
JavaScript
22 lines
683 B
JavaScript
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'}(`, 'function (')
|
|
}
|
|
|
|
serializeFunction.internalFunctionRE = /^(\s*)(?!(?:if)|(?:for)|(?:while)|(?:switch))(\w+)\s*\((.*?)\)\s*\{/gm
|
|
serializeFunction.assignmentRE = /^(\s*):(\w+)\(/gm
|