|
| 1 | +import { ElementRef } from '@angular/core'; |
| 2 | +import * as THREE from 'three'; |
| 3 | +import type { NgtAnyRecord, NgtEquConfig, NgtInstanceNode } from '../types'; |
| 4 | + |
| 5 | +export const is = { |
| 6 | + obj: (a: unknown): a is object => a === Object(a) && !Array.isArray(a) && typeof a !== 'function', |
| 7 | + material: (a: unknown): a is THREE.Material => !!a && (a as THREE.Material).isMaterial, |
| 8 | + geometry: (a: unknown): a is THREE.BufferGeometry => !!a && (a as THREE.BufferGeometry).isBufferGeometry, |
| 9 | + orthographicCamera: (a: unknown): a is THREE.OrthographicCamera => |
| 10 | + !!a && (a as THREE.OrthographicCamera).isOrthographicCamera, |
| 11 | + perspectiveCamera: (a: unknown): a is THREE.PerspectiveCamera => |
| 12 | + !!a && (a as THREE.PerspectiveCamera).isPerspectiveCamera, |
| 13 | + camera: (a: unknown): a is THREE.Camera => !!a && (a as THREE.Camera).isCamera, |
| 14 | + renderer: (a: unknown): a is THREE.WebGLRenderer => !!a && a instanceof THREE.WebGLRenderer, |
| 15 | + scene: (a: unknown): a is THREE.Scene => !!a && (a as THREE.Scene).isScene, |
| 16 | + object3D: (a: unknown): a is THREE.Object3D => !!a && (a as THREE.Object3D).isObject3D, |
| 17 | + instance: (a: unknown): a is NgtInstanceNode => !!a && !!(a as NgtAnyRecord)['__ngt__'], |
| 18 | + ref: (a: unknown): a is ElementRef => a instanceof ElementRef, |
| 19 | + equ(a: any, b: any, { arrays = 'shallow', objects = 'reference', strict = true }: NgtEquConfig = {}) { |
| 20 | + // Wrong type or one of the two undefined, doesn't match |
| 21 | + if (typeof a !== typeof b || !!a !== !!b) return false; |
| 22 | + // Atomic, just compare a against b |
| 23 | + if (typeof a === 'string' || typeof a === 'number') return a === b; |
| 24 | + const isObj = is.obj(a); |
| 25 | + if (isObj && objects === 'reference') return a === b; |
| 26 | + const isArr = Array.isArray(a); |
| 27 | + if (isArr && arrays === 'reference') return a === b; |
| 28 | + // Array or Object, shallow compare first to see if it's a match |
| 29 | + if ((isArr || isObj) && a === b) return true; |
| 30 | + // Last resort, go through keys |
| 31 | + let i; |
| 32 | + for (i in a) if (!(i in b)) return false; |
| 33 | + for (i in strict ? b : a) if (a[i] !== b[i]) return false; |
| 34 | + if (i === void 0) { |
| 35 | + if (isArr && a.length === 0 && b.length === 0) return true; |
| 36 | + if (isObj && Object.keys(a).length === 0 && Object.keys(b).length === 0) return true; |
| 37 | + if (a !== b) return false; |
| 38 | + } |
| 39 | + return true; |
| 40 | + }, |
| 41 | +}; |
0 commit comments