diff --git a/src/utils/isInternalEntity.ts b/src/utils/isInternalEntity.ts index 4d0ad173..d120e7fd 100644 --- a/src/utils/isInternalEntity.ts +++ b/src/utils/isInternalEntity.ts @@ -1,13 +1,14 @@ import { InternalEntity, InternalEntityProperty } from '../glossary' +import { isObject } from './isObject' /** - * Determines if the given object is an internal entity. + * Returns true if the given value is an internal entity object. */ export function isInternalEntity( value: Record, ): value is InternalEntity { return ( - value && + isObject(value) && InternalEntityProperty.type in value && InternalEntityProperty.primaryKey in value ) diff --git a/src/utils/isObject.ts b/src/utils/isObject.ts new file mode 100644 index 00000000..5e25a91b --- /dev/null +++ b/src/utils/isObject.ts @@ -0,0 +1,6 @@ +/** + * Returns true if the given value is a plain Object. + */ +export function isObject(value: any): value is Record { + return value != null && typeof value === 'object' && !Array.isArray(value) +} diff --git a/src/utils/removeInternalProperties.ts b/src/utils/removeInternalProperties.ts index 73bba5bf..b5fa0ba2 100644 --- a/src/utils/removeInternalProperties.ts +++ b/src/utils/removeInternalProperties.ts @@ -13,7 +13,7 @@ function isOneOfRelation< >( value: Value, ): value is InternalEntity { - return typeof value === 'object' && isInternalEntity(value) + return isInternalEntity(value) } function isManyOfRelation( diff --git a/test/utils/isObject.test.ts b/test/utils/isObject.test.ts new file mode 100644 index 00000000..8fdcd8ce --- /dev/null +++ b/test/utils/isObject.test.ts @@ -0,0 +1,20 @@ +import { isObject } from '../../src/utils/isObject' + +it('returns true given an empty object', () => { + expect(isObject({})).toEqual(true) +}) + +it('returns true given an object with values', () => { + expect(isObject({ a: 1, b: ['foo'] })).toEqual(true) +}) + +it('returns false given falsy values', () => { + expect(isObject(undefined)).toEqual(false) + expect(isObject(null)).toEqual(false) + expect(isObject(false)).toEqual(false) +}) + +it('returns false given an array', () => { + expect(isObject([])).toEqual(false) + expect(isObject([{ a: 1 }])).toEqual(false) +})