diff --git a/src/types/__tests__/object-any.spec-d.ts b/src/types/__tests__/object-any.spec-d.ts index aa1005d5..e4b9a1ba 100644 --- a/src/types/__tests__/object-any.spec-d.ts +++ b/src/types/__tests__/object-any.spec-d.ts @@ -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', () => { - expectTypeOf().extract>().not.toBeNever() - }) - - it('should extract Class', () => { - expectTypeOf().extract>().not.toBeNever() - }) - it('should match class instance objects', () => { expectTypeOf(new Date()).toMatchTypeOf() expectTypeOf(new Map()).toMatchTypeOf() diff --git a/src/types/object-any.ts b/src/types/object-any.ts index 40f0d3d0..21a69a62 100644 --- a/src/types/object-any.ts +++ b/src/types/object-any.ts @@ -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 - | Class - | ({ [K in string | symbol]?: any } & { length?: never }) +type ObjectAny = { [K in string | symbol]?: any } & { length?: never } export type { ObjectAny as default } diff --git a/src/utils/__tests__/is-object-any.spec-d.ts b/src/utils/__tests__/is-object-any.spec-d.ts new file mode 100644 index 00000000..fb2d1880 --- /dev/null +++ b/src/utils/__tests__/is-object-any.spec-d.ts @@ -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().guards.toEqualTypeOf() + }) +}) diff --git a/src/utils/__tests__/is-object-any.spec.ts b/src/utils/__tests__/is-object-any.spec.ts new file mode 100644 index 00000000..b635a80c --- /dev/null +++ b/src/utils/__tests__/is-object-any.spec.ts @@ -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 + }) +}) diff --git a/src/utils/index.ts b/src/utils/index.ts index c21d949e..934e6527 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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' diff --git a/src/utils/is-object-any.ts b/src/utils/is-object-any.ts new file mode 100644 index 00000000..9cf9dbe3 --- /dev/null +++ b/src/utils/is-object-any.ts @@ -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