Skip to content

Instantly share code, notes, and snippets.

@naramdash
Last active July 13, 2022 06:32
Show Gist options
  • Save naramdash/28d01c9286fa5eccff91ba118d6a9f69 to your computer and use it in GitHub Desktop.
Save naramdash/28d01c9286fa5eccff91ba118d6a9f69 to your computer and use it in GitHub Desktop.
Function proxies for Logging
function getParams(func: Function) {
// String representaation of the function code
const funcS = func.toString()
// Remove comments of the form /* ... */
// Removing comments of the form //
// Remove body of the function { ... }
// removing '=>' if func is arrow function
const str = funcS
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/(.)*/g, '')
.replace(/{[\s\S]*}/, '')
.replace(/=>/g, '')
.trim()
// Start parameter names after first '('
const start = str.indexOf('(') + 1
// End parameter names is just before last ')'
const end = str.length - 1
const result = str.substring(start, end).split(', ')
const params = result
.map((element) => {
// Removing any default value
element = element.replace(/=[\s\S]*/g, '').trim()
return element.length > 0 ? element : undefined
})
.filter((element) => element)
return params
}
function makeFsProxy(fsObject: any) {
let fsProxied: any = {}
for (const method in fsObject) {
// @ts-ignore
fsProxied[method] = new Proxy(fsObject[method], {
apply: function (target: Function, thisArg, argumentsList) {
console.groupCollapsed(
`%cEXPOSED PROXY => ${target.name
} | ${new Date().toLocaleTimeString()}`,
'font-weight: bold; color: aqua;'
)
{
const params = getParams(target)
params.forEach((paramName, index) => {
console.log(
`%c ${index} %c ${paramName}\n`,
'color: red; font-weight: bold;',
'color: orange; font-weight: bold',
argumentsList[index]
)
})
}
console.groupEnd()
target(...argumentsList)
},
})
}
return fsProxied
}
export { makeFsProxy }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment