UNPKG

@xylabs/logger

Version:

XYLabs Logger Library

24 lines (22 loc) 697 B
import { handleError } from '@xylabs/error' export const getFunctionName = (depth = 2) => { try { throw new Error('Getting function name') } catch (ex) { return handleError(ex, (error) => { let newIndex: number | undefined const stackParts = error.stack?.split('\n')[depth]?.split(' ') const funcName = stackParts?.find((item, index) => { if (item.length > 0 && item !== 'at') { // check if constructor if (item === 'new') { newIndex = index } return item } }) ?? '<unknown>' return newIndex ? `${funcName} ${stackParts?.[newIndex + 1]}` : funcName }) } }