Skip to content

Commit

Permalink
chore(utils): prefer isFunction (#5849)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpollman authored Sep 30, 2024
1 parent 545aa6a commit 22e285f
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 39 deletions.
9 changes: 9 additions & 0 deletions .changeset/cyan-baboons-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@aws-amplify/ui-react-core-auth": patch
"@aws-amplify/ui-react-core": patch
"@aws-amplify/ui-react-storage": patch
"@aws-amplify/ui-react": patch
"@aws-amplify/ui": patch
---

chore(utils): prefer isFunction
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { isString, isTypedFunction } from '@aws-amplify/ui';
import { isString, isFunction } from '@aws-amplify/ui';
import { useTimeout } from '@aws-amplify/ui-react-core';

interface UseHandleCopyParams {
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function useHandleCopy({
(v?: string) => {
// prefer `v` passed to callback over `target`
const copyValue = v ?? target;
if (isTypedFunction(copyHandler) && isString(copyValue)) {
if (isFunction(copyHandler) && isString(copyValue)) {
copyHandler(copyValue);
setValue(copyValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
UseControlledField,
UseControlledFieldParams,
} from './types';
import { isTypedFunction } from '@aws-amplify/ui';
import { isFunction } from '@aws-amplify/ui';

export const DEFAULT_ERROR_MESSAGE =
'`useControlledField` must be used within a `FormProvider`';
Expand Down Expand Up @@ -44,15 +44,15 @@ export default function useControlledField<OnBlur extends FocusHandler>({
const hasError = !!errorMessage;

const handleBlur = (event: Parameters<OnBlur>[0]) => {
if (isTypedFunction(_onBlur)) {
if (isFunction(_onBlur)) {
_onBlur(event);
}
// `useController.onBlur` does not receive params
onBlur();
};

const handleChangeText = (event: string) => {
if (isTypedFunction(_onChangeText)) {
if (isFunction(_onChangeText)) {
_onChangeText(event);
}
onChangeText(event);
Expand Down
4 changes: 2 additions & 2 deletions packages/react-core/src/hooks/useTimeout.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { isTypedFunction } from '@aws-amplify/ui';
import { isFunction } from '@aws-amplify/ui';

export default function useTimeout({
callback,
Expand All @@ -15,7 +15,7 @@ export default function useTimeout({
}, [callback]);

React.useEffect(() => {
if (!isTypedFunction(storedCallback.current) || !delay) {
if (!isFunction(storedCallback.current) || !delay) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fetchAuthSession } from 'aws-amplify/auth';
import { StorageAccessLevel } from '@aws-amplify/core';
import { UploadDataWithPathInput, UploadDataInput } from 'aws-amplify/storage';

import { isString, isTypedFunction } from '@aws-amplify/ui';
import { isString, isFunction } from '@aws-amplify/ui';

import { ProcessFile } from '../types';
import { resolveFile } from './resolveFile';
Expand All @@ -28,7 +28,7 @@ export const getInput = ({
useAccelerateEndpoint,
}: GetInputParams) => {
return async (): Promise<PathInput | UploadDataInput> => {
const hasCallbackPath = isTypedFunction<PathCallback>(path);
const hasCallbackPath = isFunction(path);
const hasStringPath = isString(path);

const hasKeyInput = !!accessLevel && !hasCallbackPath;
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/primitives/Accordion/AccordionTrigger.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { classNames } from '@aws-amplify/ui';

import { ComponentClassName, isTypedFunction } from '@aws-amplify/ui';
import { ComponentClassName, isFunction } from '@aws-amplify/ui';
import { primitiveWithForwardRef } from '../utils/primitiveWithForwardRef';
import { ElementType, PrimitiveProps, BaseViewProps } from '../types/view';
import { ForwardRefPrimitive, Primitive } from '../types/view';
Expand All @@ -20,7 +20,7 @@ const AccordionTriggerPrimitive: Primitive<AccordionTriggerProps, 'summary'> = (
const context = React.useContext(AccordionContext);
const value = React.useContext(AccordionItemContext);
const handleOnClick = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
if (isTypedFunction(rest.onClick)) {
if (isFunction(rest.onClick)) {
rest.onClick(e);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/primitives/Tabs/TabsItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { classNames } from '@aws-amplify/ui';
import {
ComponentClassName,
classNameModifierByFlag,
isTypedFunction,
isFunction,
} from '@aws-amplify/ui';

import { ForwardRefPrimitive, Primitive } from '../types';
Expand All @@ -25,7 +25,7 @@ const TabsItemPrimitive: Primitive<TabsItemProps, 'button'> = (
}
const isActive = activeTab === value;
const handleOnClick = (e: React.MouseEvent<HTMLButtonElement>) => {
if (isTypedFunction(onClick)) {
if (isFunction(onClick)) {
onClick?.(e);
}
setActiveTab(value);
Expand Down
14 changes: 0 additions & 14 deletions packages/ui/src/utils/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
isObject,
isSet,
isString,
isTypedFunction,
isUndefined,
sanitizeNamespaceImport,
templateJoin,
Expand Down Expand Up @@ -317,19 +316,6 @@ describe('classNameModifierByFlag', () => {
});
});

describe('isTypedFunction', () => {
it('returns `true` when the value param is a function', () => {
expect(isTypedFunction(() => null)).toBe(true);
});

it.each(['string', null, undefined, true, false])(
'returns `false` when the value param is %s',
(value) => {
expect(isTypedFunction(value)).toBe(false);
}
);
});

describe('templateJoin', () => {
it('returns the expected value', () => {
const output = templateJoin(['one', 'two'], (value) => `^${value}^`);
Expand Down
12 changes: 0 additions & 12 deletions packages/ui/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,6 @@ export const classNameModifierByFlag = (
return flag ? `${base}--${modifier}` : '';
};

/**
* `isFunction` but types the param with its function signature
*
* @param {unknown} value param to check
* @returns {boolean} whether `value` is a function
*/
export function isTypedFunction<T extends (...args: any[]) => any>(
value: unknown
): value is T {
return isFunction(value);
}

/**
* Similar to `Array.join`, with an optional callback/template param
* for formatting returned string values
Expand Down

0 comments on commit 22e285f

Please sign in to comment.