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 506d990
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/compartment-mapper/src/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,25 @@ 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>]
* @overload
* @param {T} from
* @param {U} to
* @param {K} [list]
* @returns {Omit<U, K[number]> & Pick<T, K[number]>}
*/
const selectiveCopy = (from, to, list) => {
/** @type {Partial<keyof T>[]} */
let props;
if (!list) {
list = keys(from);
props = /** @type {(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 506d990

Please sign in to comment.