Skip to content

Commit

Permalink
feat(types): IsObjectPlain, IfObjectPlain
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 30, 2023
1 parent cfdaac9 commit 8b7972e
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/types/__tests__/if-object-plain.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<T> extends false', () => {
expectTypeOf<TestSubject<Book, True, False>>().toEqualTypeOf<False>()
})

it('should equal True if IsObjectPlain<T> extends true', () => {
// Arrange
type T = Simplify<Book>

// Expect
expectTypeOf<TestSubject<T, True, False>>().toEqualTypeOf<True>()
})
})
33 changes: 33 additions & 0 deletions src/types/__tests__/is-object-plain.spec-d.ts
Original file line number Diff line number Diff line change
@@ -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<TestSubject<Book>>().toEqualTypeOf<false>()
expectTypeOf<TestSubject<Fn>>().toEqualTypeOf<false>()
expectTypeOf<TestSubject<Primitive | RegExp>>().toEqualTypeOf<false>()
expectTypeOf<TestSubject<readonly Primitive[]>>().toEqualTypeOf<false>()
})

it('should equal false if T is any', () => {
expectTypeOf<TestSubject<any>>().toEqualTypeOf<false>()
})

it('should equal false if T is never', () => {
expectTypeOf<TestSubject<never>>().toEqualTypeOf<false>()
})

it('should equal true if [T] extends [ObjectPlain]', () => {
expectTypeOf<TestSubject<ObjectPlain>>().toEqualTypeOf<true>()
expectTypeOf<TestSubject<Simplify<Book>>>().toEqualTypeOf<true>()
})
})
21 changes: 21 additions & 0 deletions src/types/if-object-plain.ts
Original file line number Diff line number Diff line change
@@ -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<T, True, False> = IsObjectPlain<T> extends true
? True
: False

export type { IfObjectPlain as default }
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
22 changes: 22 additions & 0 deletions src/types/is-object-plain.ts
Original file line number Diff line number Diff line change
@@ -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<T> = IfAnyOrNever<
T,
false,
[T] extends [ObjectPlain] ? true : false
>

export type { IsObjectPlain as default }

0 comments on commit 8b7972e

Please sign in to comment.