计算文本超过3行
export const computeEleLineHeightByLines = (
el: Element,
lines: number = 3
): boolean => {
const styleObj: CSSStyleDeclaration = window.getComputedStyle(el);
const lineHeight: number = Number(styleObj?.lineHeight?.replace(/px$/, ''));
return (
Math.abs((el as HTMLElement).offsetHeight - lineHeight * lines) < lineHeight
);
};
数组对象去重
export const filterRepeatObjectInArrayByKey = <T, K extends keyof T>(
sourceArr: T[],
key: K
): T[] => {
if (!key) return sourceArr;
if (Array.isArray(sourceArr) && sourceArr?.length === 0) return sourceArr;
const tempObj: any = {};
return sourceArr?.reduce((arr: T[], next: T) => {
!!tempObj[next[key]]
? undefined
: ((tempObj[next[key]] as boolean) = true) && arr.push(next);
return arr;
}, []);
};