-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ui,ui-react-core): Add type guard utils (#3291)
- Loading branch information
1 parent
8c62114
commit 9ce2d01
Showing
9 changed files
with
111 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@aws-amplify/ui-react-core': patch | ||
'@aws-amplify/ui': patch | ||
--- | ||
|
||
chore(ui,ui-react-core): Add type guard utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-core/src/InAppMessaging/utils/handleMessageAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { isObject, isString, isUndefined } from '..'; | ||
|
||
describe('isObject', () => { | ||
it('should return `true` for objects', () => { | ||
expect(isObject(new Number(0))).toStrictEqual(true); | ||
expect(isObject(new String(''))).toStrictEqual(true); | ||
expect(isObject(new Date())).toStrictEqual(true); | ||
expect(isObject(new Error())).toStrictEqual(true); | ||
expect(isObject({})).toStrictEqual(true); | ||
expect(isObject({ test: 1 })).toStrictEqual(true); | ||
expect(isObject(Object(false))).toStrictEqual(true); | ||
expect(isObject(Object(0))).toStrictEqual(true); | ||
expect(isObject(Object('test'))).toStrictEqual(true); | ||
}); | ||
|
||
it('should return `false` for non-objects', () => { | ||
expect(isObject(null)).toStrictEqual(false); | ||
expect(isObject(undefined)).toStrictEqual(false); | ||
expect(isObject(true)).toStrictEqual(false); | ||
expect(isObject(0)).toStrictEqual(false); | ||
expect(isObject('test')).toStrictEqual(false); | ||
expect(isObject([1, 2, 3])).toStrictEqual(false); | ||
expect(isObject(() => {})).toStrictEqual(false); | ||
}); | ||
}); | ||
|
||
describe('isString', () => { | ||
it('should return `true` for strings', () => { | ||
expect(isString('')).toStrictEqual(true); | ||
expect(isString('test')).toStrictEqual(true); | ||
expect(isString(new String('test'))).toStrictEqual(true); | ||
}); | ||
|
||
it('should return `false` for non-strings', () => { | ||
expect(isString(null)).toStrictEqual(false); | ||
expect(isString(undefined)).toStrictEqual(false); | ||
expect(isString(true)).toStrictEqual(false); | ||
expect(isString(0)).toStrictEqual(false); | ||
expect(isString([1, 2, 3])).toStrictEqual(false); | ||
}); | ||
}); | ||
|
||
describe('isUndefined', () => { | ||
it('should return `true` for undefined values', function () { | ||
expect(isUndefined(undefined)).toStrictEqual(true); | ||
expect(isUndefined(void 0)).toStrictEqual(true); | ||
}); | ||
|
||
it('should return `false` for non-undefined values', function () { | ||
expect(isUndefined(null)).toStrictEqual(false); | ||
expect(isUndefined(true)).toStrictEqual(false); | ||
expect(isUndefined('')).toStrictEqual(false); | ||
expect(isUndefined(0)).toStrictEqual(false); | ||
expect(isUndefined([1, 2, 3])).toStrictEqual(false); | ||
expect(isUndefined(new Number(0))).toStrictEqual(false); | ||
expect(isUndefined(new String(''))).toStrictEqual(false); | ||
expect(isUndefined(new Date())).toStrictEqual(false); | ||
expect(isUndefined(new Error())).toStrictEqual(false); | ||
expect(isUndefined({})).toStrictEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Checks if `value` is an Object (non-primitive, non-array, non-function) | ||
* Will return false for Arrays and functions | ||
* | ||
* | ||
* @param {unknown} value The value to check | ||
* @returns {boolean} Returns `true` if `value` is an object, `false` otherwise | ||
*/ | ||
export function isObject(value: unknown): value is object { | ||
return value != null && !Array.isArray(value) && typeof value === 'object'; | ||
} | ||
|
||
/** | ||
* Checks if `value` is a string primitive or object | ||
* | ||
* @param {unknown} value The value to check | ||
* @returns {boolean} Returns `true` if `value` is a string, `false` otherwise | ||
*/ | ||
export function isString(value: unknown): value is string { | ||
return ( | ||
typeof value === 'string' || | ||
(typeof value === 'object' && | ||
Object.prototype.toString.call(value) === '[object String]') | ||
); | ||
} | ||
|
||
/** | ||
* Checks if `value` is undefined | ||
* | ||
* @param {unknown} value The value to check | ||
* @returns {boolean} Returns `true` if `value` is undefined, `false` otherwise | ||
*/ | ||
export function isUndefined(value: unknown): value is undefined { | ||
return value === undefined; | ||
} |