2022-07-22 22:39:48 +02:00
|
|
|
/**
|
|
|
|
* Return a "masked" copy of the given object.
|
|
|
|
*
|
|
|
|
* The returned object includes only the properties present in the mask. The
|
|
|
|
* mask is an object that mirrors the structure of the given object, except
|
|
|
|
* the only values are `true` or a sub-mask. `true` implies the property
|
|
|
|
* should be included, and a sub-mask implies the property should be further
|
|
|
|
* masked according to that sub-mask.
|
|
|
|
*
|
2023-08-17 01:41:17 +02:00
|
|
|
* If a property is not found in the last, its type is included instead.
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} object - The object to mask
|
2022-07-26 20:10:51 +02:00
|
|
|
* @param {Object<object | boolean>} mask - The mask to apply to the object
|
2022-07-22 22:39:48 +02:00
|
|
|
*/
|
|
|
|
export function maskObject(object, mask) {
|
|
|
|
return Object.keys(object).reduce((state, key) => {
|
|
|
|
if (mask[key] === true) {
|
|
|
|
state[key] = object[key];
|
|
|
|
} else if (mask[key]) {
|
|
|
|
state[key] = maskObject(object[key], mask[key]);
|
2023-08-17 01:41:17 +02:00
|
|
|
} else {
|
|
|
|
state[key] = typeof object[key];
|
2022-07-22 22:39:48 +02:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
}, {});
|
|
|
|
}
|