Skip to content

Commit

Permalink
refactor: ObjectAny -> ObjectCurly
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 29, 2023
1 parent dd2d709 commit c0e8981
Show file tree
Hide file tree
Showing 20 changed files with 125 additions and 98 deletions.
4 changes: 2 additions & 2 deletions src/types/__tests__/assign.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ describe('unit-d:types/Assign', () => {
expectTypeOf<TestSubject<Author>>().toEqualTypeOf<Author>()
})

it('should merge U into T if U extends ObjectAny', () => {
it('should merge U into T if U extends ObjectCurly', () => {
// Arrange
type U = { email: Lowercase<string> }

// Expect
expectTypeOf<TestSubject<Author, U>>().toEqualTypeOf<Merge<Author, U>>()
})

it('should merge U into T if U extends readonly ObjectAny[]', () => {
it('should merge U into T if U extends readonly ObjectCurly[]', () => {
// Arrange
type U1 = [{ display_name: string }, { email: Lowercase<string> }]
type U2 = { display_name: string; email: Lowercase<string> }[]
Expand Down
2 changes: 1 addition & 1 deletion src/types/__tests__/keys.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('unit-d:types/Keys', () => {
expectTypeOf<TestSubject<EmptyString>>().toBeNever()
})

it('should equal union if T extends ObjectAny', () => {
it('should equal union if T extends ObjectCurly', () => {
expectTypeOf<TestSubject<Author>>().toEqualTypeOf<keyof Author>()
expectTypeOf<TestSubject<Error>>().toEqualTypeOf<keyof Error>()
expectTypeOf<TestSubject<RangeError>>().toEqualTypeOf<keyof RangeError>()
Expand Down
4 changes: 2 additions & 2 deletions src/types/__tests__/merge-defaults.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('unit-d:types/MergeDefaults', () => {
expectTypeOf<TestSubject<Author>>().toEqualTypeOf<Author>()
})

it('should merge defaults into T if U extends ObjectAny', () => {
it('should merge defaults into T if U extends ObjectCurly', () => {
// Arrange
type U = { email: Lowercase<string>; first_name?: string }
type Expected = Merge<Author, Omit<U, 'first_name'>>
Expand All @@ -26,7 +26,7 @@ describe('unit-d:types/MergeDefaults', () => {
expectTypeOf<TestSubject<Author, U>>().toEqualTypeOf<Expected>()
})

it('should merge defaults into T if U extends readonly ObjectAny[]', () => {
it('should merge defaults into T if U extends readonly ObjectCurly[]', () => {
// Arrange
type U1 = { display_name: string; first_name?: string }[]
type U2 = [{ display_name: string }, { first_name?: string }]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* @file Type Tests - ObjectAny
* @module tutils/types/tests/unit-d/ObjectAny
* @file Type Tests - ObjectCurly
* @module tutils/types/tests/unit-d/ObjectCurly
*/

import type Person from '#fixtures/person.interface'
import type Fn from '../fn'
import type TestSubject from '../object-any'
import type TestSubject from '../object-curly'
import type ObjectPlain from '../object-plain'
import type Primitive from '../primitive'

describe('unit-d:types/ObjectAny', () => {
describe('unit-d:types/ObjectCurly', () => {
it('should match class instance objects', () => {
expectTypeOf(new Date()).toMatchTypeOf<TestSubject>()
expectTypeOf(new Map()).toMatchTypeOf<TestSubject>()
Expand All @@ -17,17 +18,21 @@ describe('unit-d:types/ObjectAny', () => {

it('should match pojos', () => {
expectTypeOf<ObjectPlain>().toMatchTypeOf<TestSubject>()
expectTypeOf<Person>().toMatchTypeOf<TestSubject>()
})

it('should not match arrays', () => {
expectTypeOf<unknown[]>().not.toMatchTypeOf<TestSubject>()
expectTypeOf<readonly unknown[]>().not.toMatchTypeOf<TestSubject>()
})

it('should not match functions', () => {
expectTypeOf<Fn>().not.toMatchTypeOf<TestSubject>()
expectTypeOf<Readonly<Fn>>().not.toMatchTypeOf<TestSubject>()
})

it('should not match primitives', () => {
expectTypeOf<Primitive>().not.toMatchTypeOf<TestSubject>()
expectTypeOf<Readonly<Primitive>>().not.toMatchTypeOf<TestSubject>()
})
})
2 changes: 1 addition & 1 deletion src/types/__tests__/path.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('unit-d:types/Path', () => {
expectTypeOf<TestSubject<EmptyString>>().toBeNever()
})

it('should equal union if T extends ObjectAny', () => {
it('should equal union if T extends ObjectCurly', () => {
// Arrange
type T = Omit<Book, 'publisher'> & {
authors: Author[]
Expand Down
10 changes: 5 additions & 5 deletions src/types/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type EmptyArray from './empty-array'
import type EmptyObject from './empty-object'
import type Head from './head'
import type Merge from './merge'
import type ObjectAny from './object-any'
import type ObjectCurly from './object-curly'
import type OneOrMany from './one-or-many'
import type Simplify from './simplify'

Expand All @@ -21,14 +21,14 @@ import type Simplify from './simplify'
* @template U - Source object or source object array
*/
type Assign<
T extends ObjectAny,
U extends OneOrMany<ObjectAny> = EmptyObject
T extends ObjectCurly,
U extends OneOrMany<ObjectCurly> = EmptyObject
> = U extends EmptyArray | EmptyObject
? T
: U extends ObjectAny
: U extends ObjectCurly
? Merge<T, U>
: Simplify<
U extends [infer H, ...infer Rest extends readonly ObjectAny[]]
U extends [infer H, ...infer Rest extends readonly ObjectCurly[]]
? Assign<H & Omit<T, keyof H>, Rest>
: Assign<T, Head<U>>
>
Expand Down
4 changes: 2 additions & 2 deletions src/types/exact-optional-property-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module tutils/types/ExactOptionalPropertyTypes
*/

import type ObjectAny from './object-any'
import type ObjectCurly from './object-curly'
import type Simplify from './simplify'

/**
Expand All @@ -15,7 +15,7 @@ import type Simplify from './simplify'
*
* @template T - Type to evaluate
*/
type ExactOptionalPropertyTypes<T extends ObjectAny> = Simplify<{
type ExactOptionalPropertyTypes<T extends ObjectCurly> = Simplify<{
[K in keyof T]: Exclude<T[K], undefined> extends never
? T[K]
: Exclude<T[K], undefined>
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export type { default as Nullable } from './nullable'
export type { default as NumberLike } from './number-like'
export type { default as NumberString } from './number-string'
export type { default as Numeric } from './numeric'
export type { default as ObjectAny } from './object-any'
export type { default as ObjectCurly } from './object-curly'
export type { default as ObjectPlain } from './object-plain'
export type { default as Omit } from './omit'
export type { default as OmitNative } from './omit-native'
Expand Down
8 changes: 4 additions & 4 deletions src/types/merge-defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type EmptyObject from './empty-object'
import type Head from './head'
import type IfNever from './if-never'
import type Merge from './merge'
import type ObjectAny from './object-any'
import type ObjectCurly from './object-curly'
import type OneOrMany from './one-or-many'

/**
Expand All @@ -23,13 +23,13 @@ import type OneOrMany from './one-or-many'
* @template U - Source object or source object array
*/
type MergeDefaults<
T extends ObjectAny,
T extends ObjectCurly,
U extends OneOrMany<Partial<T>> = EmptyObject
> = U extends EmptyArray | EmptyObject
? T
: U extends Partial<T>
? MergeDefaults<T, [U]>
: U extends [infer H, ...infer Rest extends readonly ObjectAny[]]
: U extends [infer H, ...infer Rest extends readonly ObjectCurly[]]
? Merge<
T,
{
Expand All @@ -41,7 +41,7 @@ type MergeDefaults<
>
: never
}
> extends infer V extends ObjectAny
> extends infer V extends ObjectCurly
? Rest extends readonly Partial<V>[]
? MergeDefaults<V, Rest>
: never
Expand Down
6 changes: 3 additions & 3 deletions src/types/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import type EmptyObject from './empty-object'
import type ObjectAny from './object-any'
import type ObjectCurly from './object-curly'
import type Simplify from './simplify'

/**
Expand All @@ -16,8 +16,8 @@ import type Simplify from './simplify'
* @template U - Source object
*/
type Merge<
T extends ObjectAny,
U extends ObjectAny = EmptyObject
T extends ObjectCurly,
U extends ObjectCurly = EmptyObject
> = U extends EmptyObject ? T : Simplify<Omit<T, keyof U> & U>

export type { Merge as default }
13 changes: 0 additions & 13 deletions src/types/object-any.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/types/object-curly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Type Definitions - ObjectCurly
* @module tutils/types/ObjectCurly
*/

import type PropertyKey from './property-key'
import type Simplify from './simplify'

/**
* A curly-braced object.
*
* Curly-braced objects are `object` types that are **not** arrays or functions
* (e.g. instance objects, pojos).
*
* **Note**: Curly-braced object types **cannot** have `call` or `concat`
* properties.
*/
type ObjectCurly = Simplify<
{ [K in PropertyKey]?: any } & { call?: never; concat?: never }
>

export type { ObjectCurly as default }
4 changes: 2 additions & 2 deletions src/types/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type Head from './head'
import type Indices from './indices'
import type Keys from './keys'
import type NumberString from './number-string'
import type ObjectAny from './object-any'
import type ObjectCurly from './object-curly'
import type Primitive from './primitive'
import type PropertyKey from './property-key'

Expand Down Expand Up @@ -58,7 +58,7 @@ type Path<T, K extends PropertyKey = Keys<T>> = T extends
? NonNullable<Get<T, K>> extends infer U
? K | `${K}.${Path<Omit<NonNullable<U>, keyof Fn>>}`
: never
: NonNullable<Get<T, K>> extends ObjectAny
: NonNullable<Get<T, K>> extends ObjectCurly
? NonNullable<Get<T, K>> extends infer U
? K | `${K}.${Path<NonNullable<U>>}`
: never
Expand Down
13 changes: 0 additions & 13 deletions src/utils/__tests__/is-object-any.spec-d.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/utils/__tests__/is-object-any.spec.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/utils/__tests__/is-object-curly.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - isObjectCurly
* @module tutils/utils/tests/unit-d/isObjectCurly
*/

import type { ObjectCurly } from '#src/types'
import type testSubject from '../is-object-curly'

describe('unit-d:utils/isObjectCurly', () => {
it('should guard ObjectCurly', () => {
expectTypeOf<typeof testSubject>().guards.toEqualTypeOf<ObjectCurly>()
})
})
31 changes: 31 additions & 0 deletions src/utils/__tests__/is-object-curly.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @file Unit Tests - isObjectCurly
* @module tutils/utils/tests/unit/isObjectCurly
*/

import testSubject from '../is-object-curly'

describe('unit:utils/isObjectCurly', () => {
it('should return false if value is not curly-braced object', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]],
[faker.string.hexadecimal({ length: 24 })],
[vi.fn()]
]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.false)
})

it('should return true if value is curly-braced object', () => {
// Arrange
const cases: Parameters<typeof testSubject>[] = [
[faker.date.anytime()],
[{ email: faker.internet.email() }]
]

// Act + Expect
cases.forEach(([value]) => expect(testSubject(value)).to.be.true)
})
})
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export { default as isNull } from './is-null'
export { default as isNumber } from './is-number'
export { default as isNumeric } from './is-numeric'
export { default as isObject } from './is-object'
export { default as isObjectAny } from './is-object-any'
export { default as isObjectCurly } from './is-object-curly'
export { default as isObjectPlain } from './is-object-plain'
export { default as isPrimitive } from './is-primitive'
export { default as isRegExp } from './is-reg-exp'
Expand Down
24 changes: 0 additions & 24 deletions src/utils/is-object-any.ts

This file was deleted.

Loading

0 comments on commit c0e8981

Please sign in to comment.