-
Notifications
You must be signed in to change notification settings - Fork 0
/
isMacroabled.ts
32 lines (31 loc) · 1.44 KB
/
isMacroabled.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Macroabled } from '../types';
import isMacroedWith from './isMacroedWith';
/**
* Determines whether the given value is "macroabled".
*
* The term "macroabled" is used to identify objects or
* functions that have been extended using the `macroable()` function.
* All "macroabled" values are both `macroable` and `macroed` values.
*
* A value is considered "macroabled" if:
* - The value implements a `hasMacro` method, where the "target" is the value itself and that behaves exactly like the `isMacroedWith()` function.
* - The value implements a `macro` method, where the "target" is the value itself and that behaves exactly like the `macro()` function.
* - The value implements a `mixin` method, where the "target" is the value itself and that behaves exactly like the `mixin()` function.
*
* @example
* ```js
* import { isMacroabled } from '@vicgutt/macrojs';
*
* isMacroabled({ hasMacro() {}, macro() {}, mixin() {} }); // false
* isMacroabled({ hasMacro: isMacroedWith, macro: macro, mixin: mixin }); // false
* isMacroabled(macroable({})); // true
* ```
*/
export default function isMacroabled<T>(value: unknown): value is Macroabled<T> {
return (
isMacroedWith(value, ['hasMacro', 'macro', 'mixin']) &&
typeof (value as Macroabled<unknown>).hasMacro === 'function' &&
typeof (value as Macroabled<unknown>).macro === 'function' &&
typeof (value as Macroabled<unknown>).mixin === 'function'
);
}