bb-plane-fork/packages/utils/src/common.ts
Prateek Shourya 1f9222065e
[WEB-3788] improvement: enhance project properties related components modularity (#6882)
* improvement: work item modal data preload and parent work item details

* improvement: collapsible button title

* improvement: project creation form and modal

* improvement: emoji helper

* improvement: enhance labels component modularity

* improvement: enable state group and state list components modularity

* improvement: project settings feature list

* improvement: common utils
2025-04-09 14:50:43 +05:30

60 lines
1.9 KiB
TypeScript

import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import { CompleteOrEmpty } from "@plane/types";
// Support email can be configured by the application
export const getSupportEmail = (defaultEmail: string = ""): string => defaultEmail;
export const cn = (...inputs: ClassValue[]) => twMerge(clsx(inputs));
/**
* Extracts IDs from an array of objects with ID property
*/
export const extractIds = <T extends { id: string }>(items: T[]): string[] => items.map((item) => item.id);
/**
* Checks if an ID exists and is valid within the provided list
*/
export const isValidId = (id: string | null | undefined, validIds: string[]): boolean => !!id && validIds.includes(id);
/**
* Filters an array to only include valid IDs
*/
export const filterValidIds = (ids: string[], validIds: string[]): string[] =>
ids.filter((id) => validIds.includes(id));
/**
* Filters an array to include only valid IDs, returning both valid and invalid IDs
*/
export const partitionValidIds = (ids: string[], validIds: string[]): { valid: string[]; invalid: string[] } => {
const valid: string[] = [];
const invalid: string[] = [];
ids.forEach((id) => {
if (validIds.includes(id)) {
valid.push(id);
} else {
invalid.push(id);
}
});
return { valid, invalid };
};
/**
* Checks if an object is complete (has properties) rather than empty.
* This helps TypeScript narrow the type from CompleteOrEmpty<T> to T.
*
* @param obj The object to check, typed as CompleteOrEmpty<T>
* @returns A boolean indicating if the object is complete (true) or empty (false)
*/
export const isComplete = <T>(obj: CompleteOrEmpty<T>): obj is T => {
// Check if object is not null or undefined
if (obj == null) return false;
// Check if it's an object
if (typeof obj !== "object") return false;
// Check if it has any own properties
return Object.keys(obj).length > 0;
};