Skip to content

Commit

Permalink
fix(types)!: [ObjectPlain] match pojos only
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed May 13, 2023
1 parent f320496 commit 0ef70e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
39 changes: 34 additions & 5 deletions src/types/__tests__/object-plain.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,44 @@
* @module tutils/types/tests/unit-d/ObjectPlain
*/

import type IndexSignature from '../index-signature'
import type Class from '../class'
import type Fn from '../fn'
import type TestSubject from '../object-plain'
import type Primitive from '../primitive'

describe('unit-d:types/ObjectPlain', () => {
it('should have keys of type IndexSignature', () => {
expectTypeOf<keyof TestSubject>().toEqualTypeOf<IndexSignature>()
it('should match [[x: string]: unknown]', () => {
expectTypeOf<TestSubject[string]>().toBeUnknown()
})

it('should have properties of type any', () => {
expectTypeOf<TestSubject[IndexSignature]>().toBeAny()
it('should match [[x: symbol]: unknown]', () => {
expectTypeOf<TestSubject[symbol]>().toBeUnknown()
})

it('should match pojos', () => {
expectTypeOf({ 5: null }).toMatchTypeOf<TestSubject>()
expectTypeOf({ email: faker.internet.email() }).toMatchTypeOf<TestSubject>()
})

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

it('should not match class objects', () => {
expectTypeOf<Class<any>>().not.toMatchTypeOf<TestSubject>()
})

it('should not match class instance objects', () => {
expectTypeOf(new Date()).not.toMatchTypeOf<TestSubject>()
expectTypeOf(new Map()).not.toMatchTypeOf<TestSubject>()
expectTypeOf(new Set()).not.toMatchTypeOf<TestSubject>()
})

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

it('should not match primitives', () => {
expectTypeOf<Primitive>().not.toMatchTypeOf<TestSubject>()
})
})
8 changes: 4 additions & 4 deletions src/types/object-plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* @module tutils/types/ObjectPlain
*/

import type IndexSignature from './index-signature'

/**
* A plain object.
* Type representing a plain old JavaScript object (POJO).
*
* @see https://masteringjs.io/tutorials/fundamentals/pojo
*/
type ObjectPlain = { [Key in IndexSignature]?: any }
type ObjectPlain = { [K in string | symbol]?: unknown }

export type { ObjectPlain as default }
9 changes: 6 additions & 3 deletions src/utils/is-object-plain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import type { ObjectPlain } from '#src/types'
import isNull from './is-null'

/**
* Checks if the given `value` is a plain object.
* Checks if the given `value` is a plain object (i.e. [POJO][1]).
*
* A plain object is an object created by the [`Object`][1] constructor or an
* A plain object is an object created by the [`Object`][2] constructor or an
* object with a `[[Prototype]]` of `null`.
*
* [1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
* [1]: https://masteringjs.io/tutorials/fundamentals/pojo
* [2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
*
* @see {@linkcode ObjectPlain}
*
* @param {unknown} value - Value to evaluate
* @return {value is ObjectPlain} `true` if `value` is plain object
Expand Down

0 comments on commit 0ef70e1

Please sign in to comment.