Skip to content

Commit

Permalink
feat(utils): isObjectAny
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed May 16, 2023
1 parent cf64ff4 commit 84650de
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 19 deletions.
10 changes: 0 additions & 10 deletions src/types/__tests__/object-any.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,12 @@
* @module tutils/types/tests/unit-d/ObjectAny
*/

import type Class from '../class'
import type AbstractClass from '../class-abstract'
import type Fn from '../fn'
import type TestSubject from '../object-any'
import type ObjectPlain from '../object-plain'
import type Primitive from '../primitive'

describe('unit-d:types/ObjectAny', () => {
it('should extract AbstractClass<any>', () => {
expectTypeOf<TestSubject>().extract<AbstractClass<any>>().not.toBeNever()
})

it('should extract Class<any>', () => {
expectTypeOf<TestSubject>().extract<Class<any>>().not.toBeNever()
})

it('should match class instance objects', () => {
expectTypeOf(new Date()).toMatchTypeOf<TestSubject>()
expectTypeOf(new Map()).toMatchTypeOf<TestSubject>()
Expand Down
11 changes: 2 additions & 9 deletions src/types/object-any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
* @module tutils/types/ObjectAny
*/

import type Class from './class'
import type AbstractClass from './class-abstract'

/**
* Type representing any object that is **not** an array or function (e.g. class
* objects, instance objects, pojos).
* Any non-null object that is **not** an array (e.g. instance objects, pojos).
*
* **Note**: The object **cannot** have a `length` property.
*/
type ObjectAny =
| AbstractClass<any>
| Class<any>
| ({ [K in string | symbol]?: any } & { length?: never })
type ObjectAny = { [K in string | symbol]?: any } & { length?: never }

export type { ObjectAny as default }
13 changes: 13 additions & 0 deletions src/utils/__tests__/is-object-any.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @file Type Tests - isObjectAny
* @module tutils/utils/tests/unit-d/isObjectAny
*/

import type { ObjectAny } from '#src/types'
import type testSubject from '../is-object-any'

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

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

describe('unit:utils/isObjectAny', () => {
it('should return false if value is not instance object or pojo', () => {
expect(testSubject(faker.string.hexadecimal({ length: 24 }))).to.be.false
})

it('should return true if value is instance object', () => {
expect(testSubject(faker.date.anytime())).to.be.true
})

it('should return true if value is pojo', () => {
expect(testSubject({ email: faker.internet.email() })).to.be.true
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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 isObjectPlain } from './is-object-plain'
export { default as isPrimitive } from './is-primitive'
export { default as isString } from './is-string'
Expand Down
24 changes: 24 additions & 0 deletions src/utils/is-object-any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Utilities - isObjectAny
* @module tutils/utils/isObjectAny
*/

import type { ObjectAny } from '#src/types'
import isArray from './is-array'
import isObject from './is-object'
import isObjectPlain from './is-object-plain'

/**
* Checks if `value` is a non-null object that is not an array (e.g. instance
* objects, pojos).
*
* @see {@linkcode ObjectAny}
*
* @param {unknown} value - Value to check
* @return {value is ObjectAny} `true` if `value` is pojo or instance object
*/
const isObjectAny = (value: unknown): value is ObjectAny => {
return isObjectPlain(value) || (isObject(value) && !isArray(value))
}

export default isObjectAny

0 comments on commit 84650de

Please sign in to comment.