(value: any, type: T)
| 10 | & DrizzleEntity; |
| 11 | |
| 12 | export function is<T extends DrizzleEntityClass<any>>(value: any, type: T): value is InstanceType<T> { |
| 13 | if (!value || typeof value !== 'object') { |
| 14 | return false; |
| 15 | } |
| 16 | |
| 17 | if (value instanceof type) { // eslint-disable-line no-instanceof/no-instanceof |
| 18 | return true; |
| 19 | } |
| 20 | |
| 21 | if (!Object.prototype.hasOwnProperty.call(type, entityKind)) { |
| 22 | throw new Error( |
| 23 | `Class "${ |
| 24 | type.name ?? '<unknown>' |
| 25 | }" doesn't look like a Drizzle entity. If this is incorrect and the class is provided by Drizzle, please report this as a bug.`, |
| 26 | ); |
| 27 | } |
| 28 | |
| 29 | let cls = Object.getPrototypeOf(value).constructor; |
| 30 | if (cls) { |
| 31 | // Traverse the prototype chain to find the entityKind |
| 32 | while (cls) { |
| 33 | if (entityKind in cls && cls[entityKind] === type[entityKind]) { |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | cls = Object.getPrototypeOf(cls); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return false; |
| 42 | } |
no outgoing calls
no test coverage detected