From 6e2887e3e5e7e049f8823aa0e5d9292e9a2ed93d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Tue, 1 Aug 2023 10:17:27 +0200 Subject: [PATCH] Refacto omit to accept multiple keys --- site/source/design-system/buttons/Button.tsx | 12 ++++++---- site/source/utils/index.ts | 23 +++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/site/source/design-system/buttons/Button.tsx b/site/source/design-system/buttons/Button.tsx index de5121351..6847510c0 100644 --- a/site/source/design-system/buttons/Button.tsx +++ b/site/source/design-system/buttons/Button.tsx @@ -5,7 +5,7 @@ import { GenericButtonOrNavLinkProps, useButtonOrLink, } from '@/design-system/typography/link' -import { wrapperDebounceEvents } from '@/utils' +import { omit, wrapperDebounceEvents } from '@/utils' import { FocusStyle } from '../global-style' @@ -41,13 +41,17 @@ export const Button = forwardRef(function Button( forwardedRef ) - // @ts-ignore - delete buttonOrLinkProps.isDisabled // Remove isDisabled from props casue it's not a valid HTML attribute + // Omit isDisabled and openInSameWindow from props casue it's not a valid HTML attribute + const props = omit( + buttonOrLinkProps as Record, + 'isDisabled', + 'openInSameWindow' + ) return ( (obj: T, key: K): Omit { - const { [key]: _ignore, ...returnObject } = obj +/** + * Omits the given keys from the object + * @param obj + * @param keys + * @example omit({ a: 1, b: 2, c: 3 }, 'a', 'c') // { b: 2 } + */ +export function omit( + obj: T, + ...keys: K[] +): Omit { + const ret = keys.reduce( + (acc, key) => { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { [key]: _, ...rest } = acc as T - return returnObject + return rest + }, + obj as Omit + ) + + return ret } /**