From 8b7972eb1966c9b6d7b61b1a38994dda85b2f83e Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Mon, 29 May 2023 23:09:58 -0400 Subject: [PATCH] feat(types): `IsObjectPlain`, `IfObjectPlain` Signed-off-by: Lexus Drumgold --- src/types/__tests__/if-object-plain.spec-d.ts | 25 ++++++++++++++ src/types/__tests__/is-object-plain.spec-d.ts | 33 +++++++++++++++++++ src/types/if-object-plain.ts | 21 ++++++++++++ src/types/index.ts | 2 ++ src/types/is-object-plain.ts | 22 +++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 src/types/__tests__/if-object-plain.spec-d.ts create mode 100644 src/types/__tests__/is-object-plain.spec-d.ts create mode 100644 src/types/if-object-plain.ts create mode 100644 src/types/is-object-plain.ts diff --git a/src/types/__tests__/if-object-plain.spec-d.ts b/src/types/__tests__/if-object-plain.spec-d.ts new file mode 100644 index 00000000..3db501c6 --- /dev/null +++ b/src/types/__tests__/if-object-plain.spec-d.ts @@ -0,0 +1,25 @@ +/** + * @file Type Tests - IfObjectPlain + * @module tutils/types/tests/unit-d/IfObjectPlain + */ + +import type Book from '#fixtures/book.interface' +import type TestSubject from '../if-object-plain' +import type Simplify from '../simplify' + +describe('unit-d:types/IfObjectPlain', () => { + type False = false + type True = true + + it('should equal False if IsObjectPlain extends false', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal True if IsObjectPlain extends true', () => { + // Arrange + type T = Simplify + + // Expect + expectTypeOf>().toEqualTypeOf() + }) +}) diff --git a/src/types/__tests__/is-object-plain.spec-d.ts b/src/types/__tests__/is-object-plain.spec-d.ts new file mode 100644 index 00000000..b6189c62 --- /dev/null +++ b/src/types/__tests__/is-object-plain.spec-d.ts @@ -0,0 +1,33 @@ +/** + * @file Type Tests - IsObjectPlain + * @module tutils/types/tests/unit-d/IsObjectPlain + */ + +import type Book from '#fixtures/book.interface' +import type Fn from '../fn' +import type TestSubject from '../is-object-plain' +import type ObjectPlain from '../object-plain' +import type Primitive from '../primitive' +import type Simplify from '../simplify' + +describe('unit-d:types/IsObjectPlain', () => { + it('should equal false if [T] does not extend [ObjectPlain]', () => { + expectTypeOf>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal false if T is any', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal false if T is never', () => { + expectTypeOf>().toEqualTypeOf() + }) + + it('should equal true if [T] extends [ObjectPlain]', () => { + expectTypeOf>().toEqualTypeOf() + expectTypeOf>>().toEqualTypeOf() + }) +}) diff --git a/src/types/if-object-plain.ts b/src/types/if-object-plain.ts new file mode 100644 index 00000000..2db83b8d --- /dev/null +++ b/src/types/if-object-plain.ts @@ -0,0 +1,21 @@ +/** + * @file Type Definitions - IfObjectPlain + * @module tutils/types/IfObjectPlain + */ + +import type IsObjectPlain from './is-object-plain' + +/** + * Returns a type that indicates if `T` is a plain object. + * + * @see {@linkcode IsObjectPlain} + * + * @template T - Type to evaluate + * @template True - Type if `T` is plain object + * @template False - Type if `T` is not plain object + */ +type IfObjectPlain = IsObjectPlain extends true + ? True + : False + +export type { IfObjectPlain as default } diff --git a/src/types/index.ts b/src/types/index.ts index 4a8a0e14..acb8b5a5 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -46,6 +46,7 @@ export type { default as IfNumber } from './if-number' export type { default as IfNumeric } from './if-numeric' export type { default as IfObject } from './if-object' export type { default as IfObjectCurly } from './if-object-curly' +export type { default as IfObjectPlain } from './if-object-plain' export type { default as IfPrimitive } from './if-primitive' export type { default as IfString } from './if-string' export type { default as IfSymbol } from './if-symbol' @@ -72,6 +73,7 @@ export type { default as IsNumber } from './is-number' export type { default as IsNumeric } from './is-numeric' export type { default as IsObject } from './is-object' export type { default as IsObjectCurly } from './is-object-curly' +export type { default as IsObjectPlain } from './is-object-plain' export type { default as IsPrimitive } from './is-primitive' export type { default as IsString } from './is-string' export type { default as IsSymbol } from './is-symbol' diff --git a/src/types/is-object-plain.ts b/src/types/is-object-plain.ts new file mode 100644 index 00000000..507641fa --- /dev/null +++ b/src/types/is-object-plain.ts @@ -0,0 +1,22 @@ +/** + * @file Type Definitions - IsObjectPlain + * @module tutils/types/IsObjectPlain + */ + +import type IfAnyOrNever from './if-any-or-never' +import type ObjectPlain from './object-plain' + +/** + * Returns a boolean indicating if `T` is a plain object. + * + * @see {@linkcode ObjectPlain} + * + * @template T - Type to evaluate + */ +type IsObjectPlain = IfAnyOrNever< + T, + false, + [T] extends [ObjectPlain] ? true : false +> + +export type { IsObjectPlain as default }