ATC_SIMPLE/FRONTEND/src/untils/helper.ts

29 lines
829 B
TypeScript

export const numberOnly = (value: string): string => {
const matched = value.match(/[\d.]+/g);
return matched ? matched.join("") : "";
};
export const passwordRegex =
/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&#])[A-Za-z\d@$!%*?&#]{8,}$/;
export const emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
export function isJsonString(str: string | null) {
if (typeof str !== "string") return false;
try {
const parsed = JSON.parse(str);
// Kiểm tra xem parsed có phải là object hoặc array thật sự
return parsed !== null && typeof parsed === "object";
} catch (e) {
console.log("Error isJsonString", e);
return false;
}
}
export function mergeArray(array: any[], key: string) {
return array
.map((el) => el[key])
.flat()
.filter((el) => Object.keys(el).length > 0);
}