You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.1 KiB
71 lines
2.1 KiB
export function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
return Object.prototype.toString.call(value) === '[object Object]';
|
|
}
|
|
|
|
/** 查找对象内等于指定 UUID 的顶层属性名(用于删除引用) */
|
|
export function findUuidPropertyKeys(object: unknown, uuid: string): string[] {
|
|
const keys: string[] = [];
|
|
if (!isPlainObject(object)) {
|
|
return keys;
|
|
}
|
|
for (const key of Object.keys(object)) {
|
|
const val = object[key];
|
|
if (val === uuid) {
|
|
keys.push(key);
|
|
continue;
|
|
}
|
|
if (isPlainObject(val) && val.__uuid__ === uuid) {
|
|
keys.push(key);
|
|
}
|
|
}
|
|
return keys;
|
|
}
|
|
|
|
/** 对象树中是否包含指定 UUID */
|
|
export function containsUuid(object: unknown, uuid: string): boolean {
|
|
if (object === uuid) {
|
|
return true;
|
|
}
|
|
if (isPlainObject(object)) {
|
|
for (const key of Object.keys(object)) {
|
|
if (containsUuid(object[key], uuid)) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
if (Array.isArray(object)) {
|
|
for (const item of object) {
|
|
if (containsUuid(item, uuid)) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/** 递归收集 JSON 内所有静态 __uuid__ 引用 */
|
|
export function collectJsonUuidRefs(
|
|
object: unknown,
|
|
onRef: (uuid: string, propertyKey: string | null) => void,
|
|
path: string | null = null,
|
|
): void {
|
|
if (isPlainObject(object)) {
|
|
if (typeof object.__uuid__ === 'string') {
|
|
onRef(object.__uuid__, path);
|
|
}
|
|
for (const key of Object.keys(object)) {
|
|
if (key === '__uuid__') {
|
|
continue;
|
|
}
|
|
const nextPath = path ? `${path}.${key}` : key;
|
|
collectJsonUuidRefs(object[key], onRef, nextPath);
|
|
}
|
|
return;
|
|
}
|
|
if (Array.isArray(object)) {
|
|
for (let i = 0; i < object.length; i++) {
|
|
collectJsonUuidRefs(object[i], onRef, path ? `${path}[${i}]` : `[${i}]`);
|
|
}
|
|
}
|
|
}
|
|
|