@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
75 lines (71 loc) • 2.39 kB
text/typescript
import {arrayIsEqual} from './ArrayUtils';
import {isBoolean,isNumber,isString,isObject} from './Type';
import cloneDeep from 'lodash-es/cloneDeep';
import clone from 'lodash-es/clone';
export function getObjectMethodNames(obj: any): string[] {
let properties = new Set();
let currentObj = obj;
do {
Object.getOwnPropertyNames(currentObj).map((item) => properties.add(item));
} while ((currentObj = Object.getPrototypeOf(currentObj)));
return [...properties.keys()].filter((item) => typeof (obj as any)[item as string] === 'function') as string[];
}
export function objectIsEqual(object0: any, object1: any): boolean {
if (isBoolean(object0) && isBoolean(object1)) {
return object0 == object1;
}
if (isNumber(object0) && isNumber(object1)) {
return object0 == object1;
}
if (isString(object0) && isString(object1)) {
return object0 == object1;
}
if (isObject(object0) && isObject(object1)) {
const keys0 = Object.keys(object0);
const keys1 = Object.keys(object1);
if (!arrayIsEqual(keys0, keys1)) {
return false;
}
return JSON.stringify(object0) == JSON.stringify(object1);
}
return false;
}
export function objectMerge(object0: object, object1: object): object {
return Object.assign(object0, object1);
}
export function objectClone<T extends Array<any> | object | undefined>(value: T): T {
// return this.cloneDeep(value);
return clone(value);
// if (value) {
// if (isArray(value)) {
// const newValues: Array<any> = value.map((v) => v);
// return newValues as T;
// } else {
// return {...value};
// }
// }
// return value;
}
export function objectCloneDeep<T extends object | number | string | boolean | undefined>(value: T): T {
// https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore/issues/121
// let target = {};
// for (const prop in src) {
// if (src.hasOwnProperty(prop)) {
// if ((src as any)[prop] != null && typeof (src as any)[prop] === 'object') {
// (target as any)[prop] = this.cloneDeep((src as any)[prop]);
// } else {
// (target as any)[prop] = (src as any)[prop];
// }
// }
// }
// return target as T;
return cloneDeep(value);
// if (isString(value) || isNumber(value) || isBoolean(value)) {
// return value;
// }
// if (this.isObject(value)) {
// be careful, as this does not clone functions
// return JSON.parse(JSON.stringify(value));
// }
// return value;
}