File "utils.ts"

Full Path: /var/www/html/gitep_front/src/pages/main/utils.ts
File size: 1.12 KB
MIME-type: text/plain
Charset: utf-8

export const safeFind = (collection: any, predicate: (it: any) => boolean) => {
  if (!collection) return undefined;
  if (Array.isArray(collection)) {
    let found = collection.find(predicate);
    if (found) return found;
    for (const group of collection) {
      if (group && Array.isArray(group.projects)) {
        found = group.projects.find(predicate);
        if (found) return found;
      }
      if (group && Array.isArray(group.items)) {
        found = group.items.find(predicate);
        if (found) return found;
      }
    }
    return undefined;
  }
  if (typeof collection === 'object')
    return Object.values(collection).find(predicate);
  return undefined;
};

export const belongsToProject = (articleObj: any, projId: any) => {
  if (!articleObj) return false;
  const projList = articleObj?.article_project_links;
  if (Array.isArray(projList) && projList.length > 0) {
    return projList.some((p: any) => String(p.project_id) === String(projId));
  }
  if (articleObj.project_id !== undefined && articleObj.project_id !== null) {
    return String(articleObj.project_id) === String(projId);
  }
  return false;
};