-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix: interceptAnyObject used in monitorAPI (#259)
* Bug fix: interceptAnyObject used in monitorAPI * remove ts-ignore
- Loading branch information
1 parent
f75f355
commit 8b95cf2
Showing
3 changed files
with
58 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T extends AnyObject>( | ||
export function interceptAnyObject<T>( | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters