function find()

in packages/extensions/modelerfour/src/flattener/flattener.ts [338:388]


function find(instance: any, visitor: (item: any) => boolean, visited: WeakSet<object>): boolean {
  if (instance === null || instance === undefined || visited.has(instance)) {
    return false;
  }
  visited.add(instance);

  if (instance instanceof Set || Array.isArray(instance)) {
    for (const each of instance) {
      if (typeof each === "object") {
        if (find(each, visitor, visited)) {
          return true;
        }
      }
      if (visitor(each)) {
        return true;
      }
    }
    return false;
  }

  if (instance instanceof Map) {
    // walk thru map members.
    for (const [key, value] of instance.entries()) {
      if (typeof value === "object") {
        if (find(value, visitor, visited)) {
          return true;
        }
      }
      // yield the member after visiting children
      if (visitor(value)) {
        return true;
      }
    }
    return false;
  }

  // objects
  for (const key of Object.keys(instance)) {
    const value = instance[key];
    if (typeof value === "object") {
      if (find(value, visitor, visited)) {
        return true;
      }
    }
    // yield the member after visiting children
    if (visitor(value)) {
      return true;
    }
  }
  return false;
}