-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(types):
IsObjectPlain
, IfObjectPlain
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
cfdaac9
commit 8b7972e
Showing
5 changed files
with
103 additions
and
0 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,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>() | ||
}) | ||
}) |
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,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>() | ||
}) | ||
}) |
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,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 } |
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,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 } |