Skip to content

Commit

Permalink
chore(compartment-mapper): strongly type a function for lulz
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull committed Jun 19, 2024
1 parent bbda7af commit 45c147e
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/compartment-mapper/src/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ export const ATTENUATORS_COMPARTMENT = '<ATTENUATORS>';

/**
* Copies properties (optionally limited to a specific list) from one object to another.
*
* @param {object} from
* @param {object} to
* @param {Array<string | symbol>} [list]
* @returns {object}
* @template {Record<PropertyKey, any>} T
* @template {Record<PropertyKey, any>} U
* @template {Array<Partial<keyof T>>} [K=Array<keyof T>]
* @param {T} from
* @param {U} to
* @param {K} [list]
* @returns {Omit<U, K[number]> & Pick<T, K[number]>}
*/
const selectiveCopy = (from, to, list) => {
/** @type {Array<Partial<keyof T>>} */
let props;
if (!list) {
list = keys(from);
props = /** @type {Array<keyof T>} */ (keys(from));
} else {
props = list;
}
for (let index = 0; index < list.length; index += 1) {
const key = list[index];
for (let index = 0; index < props.length; index += 1) {
const key = props[index];
// If an endowment is missing, global value is undefined.
// This is an expected behavior if globals are used for platform feature detection
to[key] = from[key];
Expand Down

0 comments on commit 45c147e

Please sign in to comment.