Add "pick" and "omit" utils
Helpful when dealing with props in react components
This commit is contained in:
parent
54ce9e8b3c
commit
ce64147aa8
1 changed files with 46 additions and 2 deletions
|
@ -95,6 +95,50 @@ const scrollIntoViewIfNeeded = (element, container, opts = {}) => {
|
||||||
|
|
||||||
const stopPropagation = ev => ev.stopPropagation();
|
const stopPropagation = ev => ev.stopPropagation();
|
||||||
|
|
||||||
export {
|
// pick and omit from https://github.com/zotero/web-common/blob/master/utils/immutable.js
|
||||||
nextHTMLID, noop, getDragTargetOrient, createDragHandler, scrollIntoViewIfNeeded, stopPropagation
|
const omit = (object, deleteKeys) => {
|
||||||
|
if (typeof (deleteKeys) !== 'function') {
|
||||||
|
if (!Array.isArray(deleteKeys)) {
|
||||||
|
deleteKeys = [deleteKeys];
|
||||||
|
}
|
||||||
|
deleteKeys = deleteKeys.map(dk => (typeof (dk) !== 'string' ? dk.toString() : dk));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.entries(object)
|
||||||
|
.reduce((aggr, [key, value]) => {
|
||||||
|
if (typeof (deleteKeys) === 'function') {
|
||||||
|
if (!deleteKeys(key, value)) { aggr[key] = value; }
|
||||||
|
} else if (!deleteKeys.includes(key)) {
|
||||||
|
aggr[key] = value;
|
||||||
|
}
|
||||||
|
return aggr;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
const pick = (object, pickKeys) => {
|
||||||
|
if (typeof (pickKeys) === 'function') {
|
||||||
|
return Object.entries(object)
|
||||||
|
.reduce((aggr, [key, value]) => {
|
||||||
|
if (pickKeys(key)) {
|
||||||
|
aggr[key] = value;
|
||||||
|
}
|
||||||
|
return aggr;
|
||||||
|
}, {});
|
||||||
|
}
|
||||||
|
if (!Array.isArray(pickKeys)) {
|
||||||
|
pickKeys = [pickKeys];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.entries(object)
|
||||||
|
.reduce((aggr, [key, value]) => {
|
||||||
|
if (pickKeys.includes(key)) {
|
||||||
|
aggr[key] = value;
|
||||||
|
}
|
||||||
|
return aggr;
|
||||||
|
}, {});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export {
|
||||||
|
nextHTMLID, noop, getDragTargetOrient, createDragHandler, scrollIntoViewIfNeeded, stopPropagation, pick, omit
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue