/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Assign } from "utility-types";6import { Restrict, Optionals } from "./types";78/**9* Given an object: T with some optional parameters10* and defaults: a subset of optional parameters from T.11*12* Explicitly setting a default to `undefined` is not recommended13*14* @return T except provided defaults are guaranteed15*16* @example17* props: {foo: string; bar?: string},18* defaults: {bar: "good stuff"}19* => {foo: string; bar: string} // <- Note bar is defined20*21*22* props: {foo: string; bar?: string; baz?: number},23* defaults: {bar: "good stuff"}24* => {foo: string; bar: string; baz?: number} // <- Note baz is optional25* .26**/27export function fill<T extends object, U extends Optionals<T>>(28props: T,29defaults: Restrict<U, Optionals<T>, "Defaults cannot contain required values">30): Assign<T, U> {31const ret: U = {} as any;32for (const key in defaults) {33if (!props.hasOwnProperty(key) || props[key] == undefined) {34ret[key] = defaults[key];35}36}37return Object.assign({}, props, ret);38}394041