From 1ae8e04f5774efbfc09d7f902838c584068c4f80 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 11 Mar 2023 19:24:24 +0800 Subject: [PATCH] Update counting Reduce complexity in code by un-nesting ternary statements. --- src/index.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5926deb..03ce272 100644 --- a/src/index.ts +++ b/src/index.ts @@ -827,7 +827,7 @@ const normalizeValues = (values: unknown): Record = */ export const setFormValues = ( form: HTMLFormElement, - values: ConstructorParameters[0] | Record, + values: unknown, ) => { assertIsFormElement(form, 'getFormValues'); @@ -846,18 +846,23 @@ export const setFormValues = ( const count = fieldElements .filter(([, el]) => el.name in objectValues) .reduce( - (currentCount, [, el]) => ({ - ...currentCount, - [el.name]: ( - el.tagName === TAG_NAME_INPUT && el.type === INPUT_TYPE_RADIO - ? 1 - : ( - typeof currentCount[el.name] === 'number' - ? currentCount[el.name] + 1 - : 1 - ) - ), - }), + (currentCount, [, el]) => { + if (el.tagName === TAG_NAME_INPUT && el.type === INPUT_TYPE_RADIO) { + return { + ...currentCount, + [el.name]: 1, + }; + } + + return { + ...currentCount, + [el.name]: ( + typeof currentCount[el.name] === 'number' + ? currentCount[el.name] + 1 + : 1 + ), + }; + }, {} as Record, );