react-elegant-ui
Version:
Elegant UI components, made by BEM best practices for react
79 lines • 2.39 kB
JavaScript
import { getDisplayName } from '../getDisplayName';
/**
* Check object props to match of pattern
*/
export var isMatchProps = function (props, pattern) {
return Object.keys(pattern).every(function (key) {
return props[key] === pattern[key];
});
};
/**
* Check HOC requirements to match of current props
*/
export var isMatchHOCProps = function (props, requirements) {
return requirements === undefined ? true : isMatchProps(props, requirements);
};
/**
* Check object structure to match with `ConfigurableHOC`
*/
export var isHOCObject = function (object) {
return '__hocOptions' in object;
};
/**
* Get array of props from `HOCOptions`
*/
export var getPropsFromHOCOptions = function (options, onlyPrivate) {
if (onlyPrivate === void 0) {
onlyPrivate = false;
}
var propsDict = {};
// Add `matchProps` keys
var shouldCollectMatchProps = !onlyPrivate || options.privateMatchProps;
if (shouldCollectMatchProps && options.matchProps !== undefined) {
Object.keys(options.matchProps).forEach(function (name) {
propsDict[name] = 0;
});
}
// Add `privateProps`
if (options.privateProps !== undefined) {
options.privateProps.forEach(function (name) {
propsDict[name] = 0;
});
}
// Add `matchOnlyProps`
if (options.matchOnlyProps !== undefined) {
options.matchOnlyProps.forEach(function (name) {
propsDict[name] = 0;
});
}
return Object.keys(propsDict);
};
/**
* Collect a private props from compose units
*/
export var getPrivatePropsFromComposeUnits = function (wrappers) {
// NOTE: #performance use spread + convert to object for merge instead iterate arrays
var privatePropsDict = {};
// Collect a private props
wrappers.forEach(function (wrapper) {
// Skip simply HOCs
if (!isHOCObject(wrapper)) {
return;
}
var options = wrapper.__hocOptions;
getPropsFromHOCOptions(options, true).forEach(function (name) {
privatePropsDict[name] = 0;
});
});
return privatePropsDict;
};
/**
* Convert object to string who look as `(key1:value1),(key2:value2)`
*/
export var getObjectHash = function (obj) {
return obj === undefined ? '' : Object.keys(obj).reduce(function (acc, key) {
var value = typeof obj[key] === 'function' ? getDisplayName(obj[key]) : obj[key];
acc.push("".concat(key, ":").concat(value));
return acc;
}, []).join(',');
};