You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
2.1 KiB
52 lines
2.1 KiB
4 years ago
|
import identity from './identity';
|
||
|
import { isArray, isObject } from './inspect';
|
||
|
import { clone, hasOwnProperty, keys } from './object';
|
||
|
import { lowerFirst, upperFirst } from './string'; // Prefix a property
|
||
|
|
||
|
export var prefixPropName = function prefixPropName(prefix, value) {
|
||
|
return prefix + upperFirst(value);
|
||
|
}; // Remove a prefix from a property
|
||
|
|
||
|
export var unprefixPropName = function unprefixPropName(prefix, value) {
|
||
|
return lowerFirst(value.replace(prefix, ''));
|
||
|
}; // Suffix can be a falsey value so nothing is appended to string
|
||
|
// (helps when looping over props & some shouldn't change)
|
||
|
// Use data last parameters to allow for currying
|
||
|
|
||
|
export var suffixPropName = function suffixPropName(suffix, str) {
|
||
|
return str + (suffix ? upperFirst(suffix) : '');
|
||
|
}; // Copies props from one array/object to a new array/object
|
||
|
// Prop values are also cloned as new references to prevent possible
|
||
|
// mutation of original prop object values
|
||
|
// Optionally accepts a function to transform the prop name
|
||
|
|
||
|
export var copyProps = function copyProps(props) {
|
||
|
var transformFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : identity;
|
||
|
|
||
|
if (isArray(props)) {
|
||
|
return props.map(transformFn);
|
||
|
}
|
||
|
|
||
|
var copied = {};
|
||
|
|
||
|
for (var prop in props) {
|
||
|
/* istanbul ignore else */
|
||
|
if (hasOwnProperty(props, prop)) {
|
||
|
// If the prop value is an object, do a shallow clone
|
||
|
// to prevent potential mutations to the original object
|
||
|
copied[transformFn(prop)] = isObject(props[prop]) ? clone(props[prop]) : props[prop];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return copied;
|
||
|
}; // Given an array of properties or an object of property keys,
|
||
|
// plucks all the values off the target object, returning a new object
|
||
|
// that has props that reference the original prop values
|
||
|
|
||
|
export var pluckProps = function pluckProps(keysToPluck, objToPluck) {
|
||
|
var transformFn = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : identity;
|
||
|
return (isArray(keysToPluck) ? keysToPluck.slice() : keys(keysToPluck)).reduce(function (memo, prop) {
|
||
|
memo[transformFn(prop)] = objToPluck[prop];
|
||
|
return memo;
|
||
|
}, {});
|
||
|
};
|