From 9ba521839e54fe9267b2982a16c0d5ab7f0423f0 Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 25 Nov 2022 22:05:30 -0500 Subject: [PATCH] fix(striker-ui): remove isEmpty() --- striker-ui/components/GeneralInitForm.tsx | 23 +++++---------- striker-ui/lib/isEmpty.ts | 35 ----------------------- 2 files changed, 7 insertions(+), 51 deletions(-) delete mode 100644 striker-ui/lib/isEmpty.ts diff --git a/striker-ui/components/GeneralInitForm.tsx b/striker-ui/components/GeneralInitForm.tsx index d166007a..d1739239 100644 --- a/striker-ui/components/GeneralInitForm.tsx +++ b/striker-ui/components/GeneralInitForm.tsx @@ -14,7 +14,6 @@ import { REP_DOMAIN } from '../lib/consts/REG_EXP_PATTERNS'; import FlexBox from './FlexBox'; import InputWithRef, { InputForwardedRefContent } from './InputWithRef'; -import isEmpty from '../lib/isEmpty'; import MessageBox, { Message } from './MessageBox'; import MessageGroup, { MessageGroupForwardedRefContent } from './MessageGroup'; import OutlinedInputWithLabel, { @@ -87,7 +86,7 @@ const buildHostName = ({ hostNumber?: number; domainName?: string; }) => - isEmpty([organizationPrefix, hostNumber, domainName], { not: true }) + [organizationPrefix, hostNumber, domainName].every((value) => Boolean(value)) ? `${organizationPrefix}-striker${pad(hostNumber)}.${domainName}` : ''; @@ -422,24 +421,16 @@ const GeneralInitForm = forwardRef< [testInputToToggleSubmitDisabled], ); const isOrganizationPrefixPrereqFilled = useCallback( - () => - isEmpty([organizationNameInputRef.current.getValue?.call(null)], { - not: true, - }), + () => Boolean(organizationNameInputRef.current.getValue?.call(null)), [], ); const isHostNamePrereqFilled = useCallback( () => - isEmpty( - [ - organizationPrefixInputRef.current.getValue?.call(null), - hostNumberInputRef.current.getValue?.call(null), - domainNameInputRef.current.getValue?.call(null), - ], - { - not: true, - }, - ), + [ + organizationPrefixInputRef.current.getValue?.call(null), + hostNumberInputRef.current.getValue?.call(null), + domainNameInputRef.current.getValue?.call(null), + ].every((value) => Boolean(value)), [], ); const populateOrganizationPrefixInputOnBlur: OutlinedInputWithLabelOnBlur = diff --git a/striker-ui/lib/isEmpty.ts b/striker-ui/lib/isEmpty.ts deleted file mode 100644 index 23be50ce..00000000 --- a/striker-ui/lib/isEmpty.ts +++ /dev/null @@ -1,35 +0,0 @@ -type IsEmptyTypeMap = Pick< - MapToType, - 'number' | 'object' | 'string' | 'undefined' ->; - -type MapToValueIsEmptyFunction = { - [TypeName in keyof IsEmptyTypeMap]: ( - value: IsEmptyTypeMap[TypeName], - ) => boolean; -}; - -const MAP_TO_VALUE_IS_EMPTY_FUNCTION: MapToValueIsEmptyFunction = { - number: (value = 0) => value === 0, - object: (value) => Object.keys(value).length === 0, - string: (value = '') => value.trim().length === 0, - undefined: () => true, -}; - -const isEmpty = ( - values: Array, - { not, fn = 'every' }: { not?: boolean; fn?: 'every' | 'some' } = {}, -): boolean => - values[fn]((value) => { - const type = typeof value as TypeName; - - let result = MAP_TO_VALUE_IS_EMPTY_FUNCTION[type](value); - - if (not) { - result = !result; - } - - return result; - }); - -export default isEmpty;