react-component-decorators
Version:
React decorators, magic.
64 lines (50 loc) • 2.2 kB
JavaScript
;
exports.__esModule = true;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
function injectProps(propertyNames, target, name, descriptor) {
var originalFunction = descriptor.value;
if (typeof originalFunction !== 'function') {
throw new SyntaxError('@injectProps can only be used on functions, not: ' + originalFunction);
}
return _extends({}, descriptor, {
value: function propsInjectorWrapper() {
var toInject = [];
for (var i = 0; i < propertyNames.length; i++) {
toInject.push(this[propertyNames[i]]);
}
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return originalFunction.apply(this, toInject.concat(args));
}
});
}
/**
* @params: ''
*/
function applyInjectProps() {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var first = args[0];
// if it is an object, apply as arguments
if ((typeof first === 'undefined' ? 'undefined' : _typeof(first)) === 'object' && !Array.isArray(first)) {
return injectProps.apply(undefined, [['props']].concat(args));
}
// then bind to null
// then bind args (an array, or string) as the first argument
// so we can use it as a decorator with options
//
// if it is an array, use that
else if (Array.isArray(first)) {
return injectProps.bind(null, first);
}
// otherwise args is an array so it goes in
else {
return injectProps.bind(null, args);
}
}
exports.default = applyInjectProps;
exports.injectProps = injectProps;
exports.applyInjectProps = applyInjectProps;