diff --git a/src/interceptAnyObject.ts b/src/interceptAnyObject.ts index 7254cad..bbb75ef 100644 --- a/src/interceptAnyObject.ts +++ b/src/interceptAnyObject.ts @@ -1,34 +1,39 @@ import _ from 'lodash' +export type FunctionInterceptor = (name: string, original: Function) => Function +export type PropertyInterceptor = (name: string, original: any) => any + export interface AnyObject { [key: string]: any } -export type FunctionInterceptor = (name: string, original: Function) => Function -export type PropertyInterceptor = (name: string, original: any) => any +const isPlainObject = (value: any): value is AnyObject => _.isPlainObject(value) -export function interceptAnyObject( +export function interceptAnyObject( inner: T, onFunction?: FunctionInterceptor, onProperty?: PropertyInterceptor, includeNestedLevels?: number ): T { - const result = _.mapValues(inner, (original, key) => { - if (typeof original === 'function' && typeof onFunction === 'function') { - return onFunction(key, original) - } - if (includeNestedLevels && _.isObjectLike(original)) { - return interceptAnyObject( - original, - onFunction ? (name, func) => onFunction(`${key}.${name}`, func) : undefined, - onProperty ? (name, value) => onProperty(`${key}.${name}`, value) : undefined, - includeNestedLevels - 1 - ) - } - if (typeof onProperty === 'function') { - return onProperty(key, original) - } - return original - }) as T + if (isPlainObject(inner)) { + const result = _.mapValues(inner, (original, key) => { + if (typeof original === 'function' && typeof onFunction === 'function') { + return onFunction(key, original) + } + if (includeNestedLevels && _.isPlainObject(original)) { + return interceptAnyObject( + original, + onFunction ? (name, func) => onFunction(`${key}.${name}`, func) : undefined, + onProperty ? (name, value) => onProperty(`${key}.${name}`, value) : undefined, + includeNestedLevels - 1 + ) + } + if (typeof onProperty === 'function') { + return onProperty(key, original) + } + return original + }) as T - return result + return result + } + return inner } diff --git a/src/monitorAPI.ts b/src/monitorAPI.ts index 385e132..fcbb677 100644 --- a/src/monitorAPI.ts +++ b/src/monitorAPI.ts @@ -39,7 +39,7 @@ export function monitorAPI( : () => true return interceptAnyObject( - api as any, + api, (funcName, originalFunc) => { if (!shouldMonitor(funcName)) { console.log('DISABLED MONITORING>', funcName) diff --git a/test/interceptAnyObject.spec.ts b/test/interceptAnyObject.spec.ts index f6b092d..1b5720f 100644 --- a/test/interceptAnyObject.spec.ts +++ b/test/interceptAnyObject.spec.ts @@ -1,5 +1,5 @@ import _ from 'lodash' -import { interceptAnyObject, FunctionInterceptor, PropertyInterceptor } from '../src/interceptAnyObject' +import { FunctionInterceptor, interceptAnyObject, PropertyInterceptor } from '../src/interceptAnyObject' type LogSpy = jest.Mock @@ -184,4 +184,34 @@ describe('interceptAnyObject', () => { ]) expect(nestedFuncLevelTwoRetVal).toBe(12345) }) + + it('should not intercept non object-like values - functions', () => { + const spy: LogSpy = jest.fn() + function real() { + return 'real' + } + const intercepted = interceptAnyObject(real, createFuncInterceptor(spy), createPropInterceptor(spy)) + expect(intercepted()).toBe('real') + }) + + it('should not intercept non object-like values - primitives', () => { + const spy: LogSpy = jest.fn() + const real = 'real' + const intercepted = interceptAnyObject(real, createFuncInterceptor(spy), createPropInterceptor(spy)) + expect(intercepted).toBe('real') + }) + + it('should not intercept non object-like values - arrays', () => { + const spy: LogSpy = jest.fn() + const real = ['real'] + const intercepted = interceptAnyObject(real, createFuncInterceptor(spy), createPropInterceptor(spy)) + expect(intercepted).toEqual(['real']) + }) + + it('should not intercept non object-like values - maps', () => { + const spy: LogSpy = jest.fn() + const real = new Map([['key', 'value']]) + const intercepted = interceptAnyObject(real, createFuncInterceptor(spy), createPropInterceptor(spy)) + expect(intercepted.get('key')).toEqual('value') + }) })