Nuxt/packages/utils/src/serialize.js
2019-02-01 17:01:16 +03:30

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