// Copyright 2017-2026 @pezkuwi/dev-test authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { BaseFn, BaseObj, StubFn } from './types.js'; /** * Extends an existing object with the additional function if they * are not already existing. */ export function enhanceObj (obj: T, extra: X) { Object .entries(extra as Record) .forEach(([key, value]) => { (obj as Record)[key] ??= value; }); return obj as T & Omit; } /** * @internal * * A helper to create a stub object based wite the stub creator supplied */ function createStub (keys: N, creator: (key: string) => StubFn) { return keys.reduce>((obj, key) => { obj[key] ??= creator(key); return obj; }, {}) as unknown as { [K in N[number]]: StubFn }; } /** * Extends a given object with the named functions if they do not * already exist on the object. * * @type {StubObjFn} */ export function stubObj (objName: string, keys: N, alts?: Record) { return createStub(keys, (key) => () => { const alt = alts?.[key]; throw new Error(`${objName}.${key} has not been implemented${alt ? ` (Use ${alt} instead)` : ''}`); }); } /** * Extends a given object with the named functions if they do not * already exist on the object. * * @type {StubObjFn} */ export function warnObj (objName: string, keys: N) { return createStub(keys, (key) => () => { console.warn(`${objName}.${key} has been implemented as a noop`); }); }