-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(*): Rename __factory identifier to __ident and apply it to mock…
…ed methods as well
- Loading branch information
1 parent
ef93ab5
commit aba964b
Showing
3 changed files
with
43 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// eslint-disable-next-line | ||
type Function<K> = (...args: any[]) => K; | ||
type IdentityFlavored<K> = K & { __ident?: string }; | ||
|
||
export function applyIdentityProperty<K extends object, T extends Function<K>>(target: T, identity: string): T { | ||
return new Proxy( | ||
target, | ||
{ | ||
apply(func: T, _this: unknown, args: Parameters<T>): IdentityFlavored<K> | undefined { | ||
const t: IdentityFlavored<K> = func(...args); | ||
|
||
if (typeof t === 'undefined') { | ||
return; | ||
} | ||
|
||
if (!(t instanceof Object)) { | ||
return t; | ||
} | ||
|
||
if (typeof t.__ident !== 'undefined') { | ||
return t; | ||
} | ||
|
||
Object.defineProperty(t, '__ident', { | ||
enumerable: false, | ||
writable: false, | ||
value: identity, | ||
}); | ||
|
||
return t; | ||
}, | ||
}, | ||
); | ||
} |