From d80dc7c18b6296e00c519ce4cdaf76f68df2e611 Mon Sep 17 00:00:00 2001 From: Ioana Brooks Date: Thu, 26 Jan 2023 11:51:56 -0800 Subject: [PATCH] Address feddback --- packages/ui/src/helpers/index.ts | 1 - packages/ui/src/helpers/typeGuards.ts | 23 ------------ packages/ui/src/index.ts | 1 + packages/ui/src/theme/utils.ts | 2 +- .../__tests__/index.test.ts} | 6 ++-- packages/ui/src/utils/index.ts | 35 +++++++++++++++++++ 6 files changed, 41 insertions(+), 27 deletions(-) delete mode 100644 packages/ui/src/helpers/typeGuards.ts rename packages/ui/src/{helpers/__tests__/typeGuards.test.ts => utils/__tests__/index.test.ts} (90%) create mode 100644 packages/ui/src/utils/index.ts diff --git a/packages/ui/src/helpers/index.ts b/packages/ui/src/helpers/index.ts index 87a7d3e9f1e..f2a5446ea5f 100644 --- a/packages/ui/src/helpers/index.ts +++ b/packages/ui/src/helpers/index.ts @@ -1,5 +1,4 @@ export * from './authenticator'; export * from './accountSettings'; export * from './storage'; -export * from './typeGuards'; export { getLogger } from './utils'; diff --git a/packages/ui/src/helpers/typeGuards.ts b/packages/ui/src/helpers/typeGuards.ts deleted file mode 100644 index be7dcb7d821..00000000000 --- a/packages/ui/src/helpers/typeGuards.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Checks if `value` is an Object (excludes arrays and functions) - * - * @param value - */ -export const isObject = (value: unknown): value is object => - value != null && !Array.isArray(value) && typeof value === 'object'; - -/** - * Checks if `value` is a string (excludes primitive object wrappers) - * - * @param value - */ -export const isString = (value: unknown): value is string => - typeof value === 'string'; - -/** - * Checks if `value` is undefined - * - * @param value - */ -export const isUndefined = (value: unknown): value is undefined => - value === undefined; diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index a5ea76833a9..35c1fb42662 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -3,3 +3,4 @@ export * from './i18n'; export * from './machines'; export * from './theme'; export * from './types'; +export * from './utils'; diff --git a/packages/ui/src/theme/utils.ts b/packages/ui/src/theme/utils.ts index d2a438cab58..9e01af5c3aa 100644 --- a/packages/ui/src/theme/utils.ts +++ b/packages/ui/src/theme/utils.ts @@ -4,7 +4,7 @@ import kebabCase from 'lodash/kebabCase'; // internal style dictionary function import usesReference from 'style-dictionary/lib/utils/references/usesReference'; -import { isObject, isString } from '../helpers/typeGuards'; +import { isObject, isString } from '../utils'; import { DesignToken, ShadowValue, diff --git a/packages/ui/src/helpers/__tests__/typeGuards.test.ts b/packages/ui/src/utils/__tests__/index.test.ts similarity index 90% rename from packages/ui/src/helpers/__tests__/typeGuards.test.ts rename to packages/ui/src/utils/__tests__/index.test.ts index a908a6222ad..a97072ef134 100644 --- a/packages/ui/src/helpers/__tests__/typeGuards.test.ts +++ b/packages/ui/src/utils/__tests__/index.test.ts @@ -1,4 +1,4 @@ -import { isObject, isString, isUndefined } from '../typeGuards'; +import { isObject, isString, isUndefined } from '..'; describe('isObject', () => { it('should return `true` for objects', () => { @@ -28,6 +28,7 @@ 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', () => { @@ -35,13 +36,14 @@ describe('isString', () => { expect(isString(undefined)).toStrictEqual(false); expect(isString(true)).toStrictEqual(false); expect(isString(0)).toStrictEqual(false); - expect(isString(new String(''))).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 () { diff --git a/packages/ui/src/utils/index.ts b/packages/ui/src/utils/index.ts new file mode 100644 index 00000000000..0f2393d14c6 --- /dev/null +++ b/packages/ui/src/utils/index.ts @@ -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; +}