@crpt/react-utils
Version:
A useful kit for daily using
57 lines (40 loc) • 1.94 kB
JavaScript
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; };
import warning from 'warning';
import { assign, isEmpty } from 'lodash';
import { GET_PROPS } from '../../constants';
import mergeProps from './mergeProps';
import { reduceCollector } from './collectors';
import { defaultProcessor, processAccessor, processBlacklist } from './processors';
export var defaults = {
accessor: GET_PROPS,
accumulator: mergeProps,
collection: [processAccessor, processBlacklist],
collector: reduceCollector,
processor: defaultProcessor,
initial: null
};
/**
* @param {any} collector
*/
export var checkAccumulator = function checkAccumulator(collector) {
var isValid = typeof collector === 'function';
var message = ' (utils/lib/propsUtils) collectProps: Expected "collector" of type \'' + (typeof collector === 'undefined' ? 'undefined' : _typeof(collector)) + '\' to be a function. ';
return isValid ? true : warning(isValid, message.replace(/ +/g, ' '));
};
/**
* @param {object} target
* @param {Object} options
* @param {array} ...props
*/
var collectProps = function collectProps(target) {
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var currArgs = isEmpty(args) ? [target] : args;
var collectOptions = assign({}, defaults, options);
var collection = collectOptions.collection,
collector = collectOptions.collector;
return checkAccumulator(collector) ? collector.apply(undefined, [collection, target, collectOptions].concat(currArgs)) : undefined;
};
export default collectProps;