uncouple
Version:
Uncouple constructors and classes methods into functions.
97 lines (96 loc) • 2.92 kB
TypeScript
import { KeyOf } from './key';
/**
* A generic function type.
*/
declare type Method = (...args: any[]) => any;
/**
* Uncoupled methods from `T`.
*/
declare type UncoupledMethodsOf<T> = {
[K in KeyOf<T>]: T[K] extends Method ? (instance: T, ...args: Parameters<T[K]>) => ReturnType<T[K]> : never;
};
/**
* A function constructor (class) with prototype of `T` or an object of `T`.
*/
declare type Constructor<T> = {
prototype: T;
} | (T & {
prototype: undefined;
});
/**
* Uncouple methods from function constructor, a class or an object into functions.
* @example ```js
* const { filter } = uncoupleMethods(Array);
* filter([ 1, 2, 3, 4 ], (value) => value % 2 === 0);
* //=> [ 2, 4 ]
* ```
* @param constructor - A function constructor, a class or an object to be uncoupled into functions.
*/
export declare const uncoupleMethods: <T>(constructor: Constructor<T>) => UncoupledMethodsOf<T>;
/**
* Uncoupled methods from `T`.
*/
declare type UncoupledMethodsAsCurriesOf<T> = {
[K in KeyOf<T>]: T[K] extends Method ? (...args: Parameters<T[K]>) => (instance: T) => ReturnType<T[K]> : never;
};
/**
* Uncouple methods from function constructor, a class or an object into functions.
* @example ```js
* const { filter: createFilter } = uncoupleMethodsAsCurries(Array);
* const filter((value) => value % 2 === 0);
* filter([ 1, 2, 3, 4 ]);
* //=> [ 2, 4 ]
* ```
* @param constructor - A function constructor, a class or an object.
*/
export declare const uncoupleMethodsAsCurries: <T>(constructor: Constructor<T>) => UncoupledMethodsAsCurriesOf<T>;
/**
* Uncoupled getters from `T`.
*/
declare type UncoupledGettersOf<T> = {
[name: string]: (instance: T) => any;
};
/**
* Uncouple getters from function constructor, a class or an object into functions.
* @example ```js
* const { getName } = uncoupleGetters({
* _name: 'Vitor',
* get name () {
* return this._name;
* }
* });
* getName({ _name: 'Lucas' })
* //=> 'Lucas'
* ```
* @param constructor - A function constructor, a class or an object
*/
export declare const uncoupleGetters: <T>(constructor: Constructor<T>) => UncoupledGettersOf<T>;
/**
* Uncoupled setters from `T`.
*/
declare type UncoupledSettersOf<T> = {
[name: string]: (instance: T, value: any) => void;
};
/**
* Uncouple setters from function constructor, a class or an object into functions.
* @example ```js
* const { setName } = uncoupleGetters({
* _name: 'Vitor',
* set name (name) {
* this._name = name;
* }
* });
*
* const user = {
* _name: 'Vitor'
* };
*
* setName(user, 'Lucas');
*
* user._name;
* //=> 'Lucas'
* ```
* @param constructor - A function constructor, a class or an object
*/
export declare const uncoupleSetters: <T>(constructor: Constructor<T>) => UncoupledSettersOf<T>;
export {};