UNPKG

object-shape-tester

Version:
14 lines (13 loc) 744 B
export function haveEqualTypes({ subject, shape }) { const shapeConstructor = shape?.constructor; const subjectPrototype = subject?.constructor?.prototype; const constructorsEqual = subject?.constructor === shapeConstructor; const constructorsInstanceOf = shapeConstructor && subjectPrototype ? subjectPrototype instanceof shapeConstructor : false; const constructorsMatch = constructorsEqual || constructorsInstanceOf || isNullObjectComparison({ shapeConstructor, subject }); return typeof subject === typeof shape && constructorsMatch; } function isNullObjectComparison({ shapeConstructor, subject, }) { return !!subject && Object.getPrototypeOf(subject) === null && shapeConstructor === Object; }