uncouple
Version:
Uncouple constructors and classes methods into functions.
136 lines (122 loc) • 4.08 kB
JavaScript
/*!
* uncouple v0.6.0
* (c) Vitor Luiz Cavalcanti <vitorluizc@outlook.com> (https://vitorluizc.github.io)
* Released under the MIT License.
*/
;
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Get property keys from object, `T`.
* @param object
*/
var getKeys = Object.getOwnPropertyNames;
/**
* Curry to check if name is a method name from object, `T`.
* @param object
*/
var isMethodOf = function (object) { return function (key) { return key !== 'constructor' && typeof object[key] === 'function'; }; };
/**
* 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.
*/
var uncoupleMethods = function (constructor) {
var prototype = constructor.prototype || constructor;
var methods = Object.create(null);
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) {
// @ts-ignore
methods[name] = Function.call.bind(prototype[name]);
});
return methods;
};
/**
* 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.
*/
var uncoupleMethodsAsCurries = function (constructor) {
var prototype = constructor.prototype || constructor;
var methods = Object.create(null);
getKeys(prototype).filter(isMethodOf(prototype)).forEach(function (name) {
// @ts-ignore
methods[name] = function () {
var arguments$1 = arguments;
// @ts-ignore
return function (instance) { return prototype[name].apply(instance, arguments$1); };
};
});
return methods;
};
/**
* Append prefix to a word and capitalize it.
* @param prefix
* @param name
*/
var prefix = function (prefix, name) { return prefix + (name ? name[0].toUpperCase() + name.substr(1) : ''); };
/**
* 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
*/
var uncoupleGetters = function (constructor) {
var prototype = constructor.prototype || constructor;
var getters = Object.create(null);
getKeys(prototype).forEach(function (name) {
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {};
if (typeof descriptor.get === 'function') { getters[prefix('get', name)] = Function.call.bind(descriptor.get); }
});
return getters;
};
/**
* 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
*/
var uncoupleSetters = function (constructor) {
var prototype = constructor.prototype || constructor;
var setters = Object.create(null);
getKeys(prototype).forEach(function (name) {
var descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {};
if (typeof descriptor.set === 'function') // @ts-ignore
{ setters[prefix('set', name)] = Function.call.bind(descriptor.set); }
});
return setters;
};
exports.uncoupleGetters = uncoupleGetters;
exports.uncoupleMethods = uncoupleMethods;
exports.uncoupleMethodsAsCurries = uncoupleMethodsAsCurries;
exports.uncoupleSetters = uncoupleSetters;