Skip to content

Commit 9ce2d01

Browse files
authored
chore(ui,ui-react-core): Add type guard utils (#3291)
1 parent 8c62114 commit 9ce2d01

File tree

9 files changed

+111
-8
lines changed

9 files changed

+111
-8
lines changed

.changeset/sixty-buttons-study.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/ui-react-core': patch
3+
'@aws-amplify/ui': patch
4+
---
5+
6+
chore(ui,ui-react-core): Add type guard utils

packages/react-core/src/Authenticator/hooks/useAuthenticator/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import {
77
getSortedFormFields,
88
UnverifiedContactMethods,
99
getActorContext,
10+
isString,
1011
} from '@aws-amplify/ui';
11-
import isString from 'lodash/isString';
1212

1313
import { areEmptyArrays, areEmptyObjects } from '../../../utils';
1414
import { AuthenticatorLegacyField, AuthenticatorLegacyFields } from '../types';

packages/react-core/src/InAppMessaging/utils/handleMessageAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ConsoleLogger as Logger } from '@aws-amplify/core';
2-
import isString from 'lodash/isString';
2+
import { isString } from '@aws-amplify/ui';
33

44
import { MessageAction } from '../types';
55

packages/react-core/src/hooks/useHasValueUpdated.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { isUndefined } from '@aws-amplify/ui';
2+
13
import usePreviousValue from './usePreviousValue';
2-
import isUndefined from 'lodash/isUndefined';
34

45
export default function useHasValueUpdated<Value>(
56
value: Value,

packages/react-core/src/utils/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import isEmpty from 'lodash/isEmpty';
2-
import isObject from 'lodash/isObject';
3-
import isString from 'lodash/isString';
2+
3+
import { isObject, isString } from '@aws-amplify/ui';
44

55
function isEmptyArray<T>(value: T): boolean {
66
return Array.isArray(value) && isEmpty(value);
@@ -11,7 +11,7 @@ export function areEmptyArrays<T>(...values: T[]): boolean {
1111
}
1212

1313
function isEmptyObject<T>(value: T): boolean {
14-
return isObject(value) && !Array.isArray(value) && isEmpty(value);
14+
return isObject(value) && isEmpty(value);
1515
}
1616

1717
export function areEmptyObjects<T>(...values: T[]): boolean {

packages/ui/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './i18n';
33
export * from './machines';
44
export * from './theme';
55
export * from './types';
6+
export * from './utils';

packages/ui/src/theme/utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import has from 'lodash/has';
2-
import isObject from 'lodash/isObject';
3-
import isString from 'lodash/isString';
42
import kebabCase from 'lodash/kebabCase';
53

64
// internal style dictionary function
75
import usesReference from 'style-dictionary/lib/utils/references/usesReference';
86

7+
import { isObject, isString } from '../utils';
98
import {
109
DesignToken,
1110
ShadowValue,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { isObject, isString, isUndefined } from '..';
2+
3+
describe('isObject', () => {
4+
it('should return `true` for objects', () => {
5+
expect(isObject(new Number(0))).toStrictEqual(true);
6+
expect(isObject(new String(''))).toStrictEqual(true);
7+
expect(isObject(new Date())).toStrictEqual(true);
8+
expect(isObject(new Error())).toStrictEqual(true);
9+
expect(isObject({})).toStrictEqual(true);
10+
expect(isObject({ test: 1 })).toStrictEqual(true);
11+
expect(isObject(Object(false))).toStrictEqual(true);
12+
expect(isObject(Object(0))).toStrictEqual(true);
13+
expect(isObject(Object('test'))).toStrictEqual(true);
14+
});
15+
16+
it('should return `false` for non-objects', () => {
17+
expect(isObject(null)).toStrictEqual(false);
18+
expect(isObject(undefined)).toStrictEqual(false);
19+
expect(isObject(true)).toStrictEqual(false);
20+
expect(isObject(0)).toStrictEqual(false);
21+
expect(isObject('test')).toStrictEqual(false);
22+
expect(isObject([1, 2, 3])).toStrictEqual(false);
23+
expect(isObject(() => {})).toStrictEqual(false);
24+
});
25+
});
26+
27+
describe('isString', () => {
28+
it('should return `true` for strings', () => {
29+
expect(isString('')).toStrictEqual(true);
30+
expect(isString('test')).toStrictEqual(true);
31+
expect(isString(new String('test'))).toStrictEqual(true);
32+
});
33+
34+
it('should return `false` for non-strings', () => {
35+
expect(isString(null)).toStrictEqual(false);
36+
expect(isString(undefined)).toStrictEqual(false);
37+
expect(isString(true)).toStrictEqual(false);
38+
expect(isString(0)).toStrictEqual(false);
39+
expect(isString([1, 2, 3])).toStrictEqual(false);
40+
});
41+
});
42+
43+
describe('isUndefined', () => {
44+
it('should return `true` for undefined values', function () {
45+
expect(isUndefined(undefined)).toStrictEqual(true);
46+
expect(isUndefined(void 0)).toStrictEqual(true);
47+
});
48+
49+
it('should return `false` for non-undefined values', function () {
50+
expect(isUndefined(null)).toStrictEqual(false);
51+
expect(isUndefined(true)).toStrictEqual(false);
52+
expect(isUndefined('')).toStrictEqual(false);
53+
expect(isUndefined(0)).toStrictEqual(false);
54+
expect(isUndefined([1, 2, 3])).toStrictEqual(false);
55+
expect(isUndefined(new Number(0))).toStrictEqual(false);
56+
expect(isUndefined(new String(''))).toStrictEqual(false);
57+
expect(isUndefined(new Date())).toStrictEqual(false);
58+
expect(isUndefined(new Error())).toStrictEqual(false);
59+
expect(isUndefined({})).toStrictEqual(false);
60+
});
61+
});

packages/ui/src/utils/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Checks if `value` is an Object (non-primitive, non-array, non-function)
3+
* Will return false for Arrays and functions
4+
*
5+
*
6+
* @param {unknown} value The value to check
7+
* @returns {boolean} Returns `true` if `value` is an object, `false` otherwise
8+
*/
9+
export function isObject(value: unknown): value is object {
10+
return value != null && !Array.isArray(value) && typeof value === 'object';
11+
}
12+
13+
/**
14+
* Checks if `value` is a string primitive or object
15+
*
16+
* @param {unknown} value The value to check
17+
* @returns {boolean} Returns `true` if `value` is a string, `false` otherwise
18+
*/
19+
export function isString(value: unknown): value is string {
20+
return (
21+
typeof value === 'string' ||
22+
(typeof value === 'object' &&
23+
Object.prototype.toString.call(value) === '[object String]')
24+
);
25+
}
26+
27+
/**
28+
* Checks if `value` is undefined
29+
*
30+
* @param {unknown} value The value to check
31+
* @returns {boolean} Returns `true` if `value` is undefined, `false` otherwise
32+
*/
33+
export function isUndefined(value: unknown): value is undefined {
34+
return value === undefined;
35+
}

0 commit comments

Comments
 (0)